Example usage for javax.jms QueueSession close

List of usage examples for javax.jms QueueSession close

Introduction

In this page you can find the example usage for javax.jms QueueSession close.

Prototype


void close() throws JMSException;

Source Link

Document

Closes the session.

Usage

From source file:org.smartfrog.avalanche.shared.jms.MessageListener.java

public MonitoringEvent receive() throws Exception {
    MonitoringEvent event = null;//from  w  w  w  . j  a  va 2 s .  c  om
    // TODO : no need to open a new session every time .. fix it
    QueueSession qs = null;
    QueueReceiver qr = null;
    try {
        qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        qr = qs.createReceiver(queue);
        // TODO : Fix message timeout
        log.info("MonitoringEvent.receive() : Checking for new message on Queue");
        MapMessage mm = (MapMessage) qr.receive(jmsTimeout);

        if (mm != null) {
            log.info("Message received");
            event = new MonitoringEventDefaultImpl();
            event.setHost(mm.getString(MonitoringEvent.HOST));
            event.setInstanceName(mm.getString(MonitoringEvent.INSTANCE_NAME));
            event.setModuleId(mm.getString(MonitoringEvent.MODULEID));
            event.setModuleState(mm.getString(MonitoringEvent.MODULE_STATE));
            event.setMsg(mm.getString(MonitoringEvent.MODULE_STATE));
            event.setMessageType(mm.getInt(MonitoringEvent.MESSAGE_TYPE));
            log.info("MessageListener.receive() - " + event);

        } else {
            log.info("No message found in queue");
        }
        return event;
    } finally {
        qr.close();
        qs.close();
    }
}

From source file:org.wso2.carbon.andes.core.QueueManagerServiceImpl.java

/**
 * Publish message to given JMS queue/*from   ww  w  .ja va2 s .  c o m*/
 *
 * @param nameOfQueue queue name
 * @param userName username
 * @param accessKey access key
 * @param jmsType jms type
 * @param jmsCorrelationID message correlation id
 * @param numberOfMessages number of messages to publish
 * @param message message body
 * @param deliveryMode delivery mode
 * @param priority message priority
 * @param expireTime message expire time
 * @throws QueueManagerException
 */
private void send(String nameOfQueue, String userName, String accessKey, String jmsType,
        String jmsCorrelationID, int numberOfMessages, String message, int deliveryMode, int priority,
        long expireTime) throws QueueManagerException {
    QueueConnectionFactory connFactory;
    QueueConnection queueConnection = null;
    QueueSession queueSession = null;
    QueueSender queueSender = null;
    try {
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, ANDES_ICF);
        properties.put(CF_NAME_PREFIX + CF_NAME, Utils.getTCPConnectionURL(userName, accessKey));
        properties.put(QUEUE_NAME_PREFIX + nameOfQueue, nameOfQueue);
        properties.put(CarbonConstants.REQUEST_BASE_CONTEXT, "true");
        InitialContext ctx = new InitialContext(properties);
        connFactory = (QueueConnectionFactory) ctx.lookup(CF_NAME);
        queueConnection = connFactory.createQueueConnection();
        Queue queue = (Queue) ctx.lookup(nameOfQueue);
        queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queueSender = queueSession.createSender(queue);
        queueConnection.start();
        TextMessage textMessage = queueSession.createTextMessage();
        if (queueSender != null && textMessage != null) {
            if (jmsType != null) {
                textMessage.setJMSType(jmsType);
            }
            if (jmsCorrelationID != null) {
                textMessage.setJMSCorrelationID(jmsCorrelationID);
            }

            if (message != null) {
                textMessage.setText(message);
            } else {
                textMessage.setText("Type message here..");
            }

            for (int i = 0; i < numberOfMessages; i++) {
                queueSender.send(textMessage, deliveryMode, priority, expireTime);
            }
        }
    } catch (FileNotFoundException | NamingException | UnknownHostException | XMLStreamException
            | JMSException e) {
        throw new QueueManagerException("Unable to send message.", e);
    } finally {
        try {
            if (queueConnection != null) {
                queueConnection.close();
            }
        } catch (JMSException e) {
            log.error("Unable to close queue connection", e);
        }
        try {
            if (queueSession != null) {
                queueSession.close();
            }
        } catch (JMSException e) {
            log.error("Unable to close queue session", e);
        }
        try {
            if (queueSender != null) {
                queueSender.close();
            }
        } catch (JMSException e) {
            log.error("Unable to close queue sender", e);
        }
    }
}

From source file:org.wso2.carbon.oc.agent.publisher.mb.MBPublisher.java

/**
 * @param queueName   - String mb queue name
 * @param jsonMessage - String mb queue message json string
 */// w  w w  .  j a  v  a  2 s  . c  om
public void sendMessages(String queueName, String jsonMessage) {

    try {
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, QPID_ICF);
        properties.put(CF_NAME_PREFIX + CF_NAME, getTCPConnectionURL(username, password));
        properties.put(QUEUE_NAME_PREFIX + queueName, queueName);
        InitialContext ctx = new InitialContext(properties);
        // lookup connection factory
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(CF_NAME);
        QueueConnection queueConnection = connFactory.createQueueConnection();
        queueConnection.start();
        QueueSession queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        // send message
        Queue queue = (Queue) ctx.lookup(queueName);
        // create the message to send

        TextMessage textMessage = queueSession.createTextMessage(jsonMessage);
        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.send(textMessage);
        queueSender.close();
        queueSession.close();
        queueConnection.close();
    } catch (JMSException e) {
        logger.error("MBPublisher connection down", e);
    } catch (NamingException e) {
        logger.error("Naming error", e);
    }

}