Example usage for javax.jms Session close

List of usage examples for javax.jms Session close

Introduction

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

Prototype


void close() throws JMSException;

Source Link

Document

Closes the session.

Usage

From source file:ConsumerTool.java

protected void consumeMessagesAndClose(Connection connection, Session session, MessageConsumer consumer,
        long timeout) throws JMSException, IOException {
    System.out.println(//from w ww .  j  a va2 s  .c  o m
            "[" + this.getName() + "] We will consume messages while they continue to be delivered within: "
                    + timeout + " ms, and then we will shutdown");

    Message message;
    while ((message = consumer.receive(timeout)) != null) {
        onMessage(message);
    }

    System.out.println("[" + this.getName() + "] Closing connection");
    consumer.close();
    session.close();
    connection.close();
    if (pauseBeforeShutdown) {
        System.out.println("[" + this.getName() + "] Press return to shut down");
        System.in.read();
    }
}

From source file:eu.domibus.submission.jms.BackendJMSImpl.java

private Boolean submitErrorMessage(String messageId) {
    Connection connection;//from w  ww. ja  va2s. c o  m
    MessageProducer producer;
    List<ErrorLogEntry> errors = this.errorLogDao.getUnnotifiedErrorsForMessage(messageId);

    try {
        connection = this.cf.createConnection();
        final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(this.receivingQueue);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        final MapMessage resMessage = session.createMapMessage();
        for (int i = 0; i < errors.size(); ++i) {
            resMessage.setString(String.valueOf(i), errors.get(i).toString());
            errors.get(i).setNotified(new Date());
            this.errorLogDao.update(errors.get(i));
        }

        producer.send(resMessage);
        producer.close();
        session.close();

        connection.close();
    } catch (JMSException e) {
        BackendJMSImpl.LOG.error("", e);
        return false;
    }
    return true;
}

From source file:com.moss.veracity.core.cluster.jms.UpdateTransmitterJMSImpl.java

private void sendMessage(Object o) {

    Session session = null;
    MessageProducer producer = null;/*  www  . j a v  a 2 s . com*/
    try {
        StringWriter writer = new StringWriter();
        Marshaller m = jaxbContext.createMarshaller();
        m.marshal(o, writer);
        String text = writer.getBuffer().toString();

        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(UpdateTopic.NAME);
        producer = session.createProducer(topic);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);

        TextMessage message = session.createTextMessage(text);
        producer.send(message);

        producer.close();
        session.close();
    } catch (Exception ex) {

        if (producer != null) {
            try {
                producer.close();
            } catch (JMSException e) {
                if (log.isErrorEnabled()) {
                    log.error("Failed to close producer after failure", e);
                } else {
                    ex.printStackTrace();
                }
            }
        }

        if (session != null) {
            try {
                session.close();
            } catch (JMSException e) {
                if (log.isErrorEnabled()) {
                    log.error("Failed to close session after failure", e);
                } else {
                    ex.printStackTrace();
                }
            }
        }

        throw new RuntimeException("Message transmission failed: " + o, ex);
    }
}

From source file:ConsumerTool.java

protected void consumeMessagesAndClose(Connection connection, Session session, MessageConsumer consumer)
        throws JMSException, IOException {
    System.out.println("[" + this.getName() + "] We are about to wait until we consume: " + maxiumMessages
            + " message(s) then we will shutdown");

    for (int i = 0; i < maxiumMessages && isRunning();) {
        Message message = consumer.receive(1000);
        if (message != null) {
            i++;// w w w  . j  av  a 2 s .  c  o m
            onMessage(message);
        }
    }
    System.out.println("[" + this.getName() + "] Closing connection");
    consumer.close();
    session.close();
    connection.close();
    if (pauseBeforeShutdown) {
        System.out.println("[" + this.getName() + "] Press return to shut down");
        System.in.read();
    }
}

From source file:com.chinamobile.bcbsp.comm.ConsumerTool.java

/** Comsume messages and close the connection. */
protected void consumeMessagesAndClose(Connection connection, Session session, MessageConsumer consumer,
        long timeout) throws JMSException, IOException {
    LOG.info("[" + this.getName() + "] consume messages while continue to be delivered within: " + timeout
            + " ms, and then we will shutdown");
    Message message;/*w w  w . j  a v a2s. co m*/
    while ((message = consumer.receive(timeout)) != null) {
        onMessage(message);
    }
    LOG.info("[" + this.getName() + "] Closing connection");
    consumer.close();
    session.close();
    connection.close();
}

From source file:org.openbaton.common.vnfm_sdk.amqp.VnfmSpringHelper.java

/**
 * This method should be used for receiving text message from EMS
 *
 * resp = { 'output': out, // the output of the command 'err': err, // the error outputs of the
 * commands 'status': status // the exit status of the command }
 *
 * @param queueName/*from  ww  w  . j  a  v  a  2 s  .c o  m*/
 * @return
 * @throws JMSException
 */
public String receiveTextFromQueue(String queueName)
        throws JMSException, ExecutionException, InterruptedException, VnfmSdkException {
    String res;

    Connection connection = connectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(session.createQueue(queueName));
    connection.start();
    String scriptMaxTime = properties.getProperty("script-max-time");
    if (scriptMaxTime != null) {
        TextMessage textMessage = (TextMessage) consumer.receive(Long.parseLong(scriptMaxTime));
        if (textMessage != null)
            res = textMessage.getText();
        else
            throw new VnfmSdkException("No message got from queue " + queueName + " after " + scriptMaxTime);
    } else
        res = ((TextMessage) consumer.receive()).getText();
    log.debug("Received Text from " + queueName + ": " + res);
    consumer.close();
    session.close();
    connection.close();
    return res;
}

From source file:com.microsoft.azure.servicebus.samples.jmstopicquickstart.JmsTopicQuickstart.java

private void receiveFromSubscription(ConnectionStringBuilder csb, Context context, ConnectionFactory cf,
        String name) throws NamingException, JMSException, InterruptedException {
    AtomicInteger totalReceived = new AtomicInteger(0);
    System.out.printf("Subscription %s: \n", name);

    Destination subscription = (Destination) context.lookup(name);
    // Create Connection
    Connection connection = cf.createConnection(csb.getSasKeyName(), csb.getSasKey());
    connection.start();/* www  .  j  a  v  a2 s  .  c  o m*/
    // Create Session, no transaction, client ack
    Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    // Create consumer
    MessageConsumer consumer = session.createConsumer(subscription);
    // Set callback listener. Gets called for each received message.
    consumer.setMessageListener(message -> {
        try {
            System.out.printf("Received message %d with sq#: %s\n", totalReceived.incrementAndGet(), // increments the counter
                    message.getJMSMessageID());
            message.acknowledge();
        } catch (Exception e) {
            System.out.printf("%s", e.toString());
        }
    });

    // wait on the main thread until all sent messages have been received
    while (totalReceived.get() < totalSend) {
        Thread.sleep(1000);
    }
    consumer.close();
    session.close();
    connection.stop();
    connection.close();
}

From source file:com.chinamobile.bcbsp.comm.ConsumerTool.java

/** Comsumer messages and close the connection.
 * @throws IOException,JMSException/*from   ww  w.j a v  a 2s  . com*/
 */
protected void consumeMessagesAndClose(Connection connection, Session session, MessageConsumer consumer)
        throws JMSException, IOException {
    LOG.info("[" + this.getName() + "] We are about to wait until we consume: " + maxiumMessages
            + " message(s) then we will shutdown");
    for (int i = 0; i < maxiumMessages && isRunning();) {
        Message message = consumer.receive(1000);
        if (message != null) {
            i++;
            onMessage(message);
        }
    }
    LOG.info("[" + this.getName() + "] Closing connection");
    consumer.close();
    session.close();
    connection.close();
    if (pauseBeforeShutdown) {
        LOG.info("[" + this.getName() + "] Press return to shut down");
        // System.in.read();
    }
}

From source file:eu.domibus.submission.jms.BackendJMSImpl.java

/**
 * This method is called when a message was received at the incoming queue
 *
 * @param message The incoming JMS Message
 */// w  w  w .jav  a  2  s .  c om
@Override
public void onMessage(final Message message) {
    final MapMessage map = (MapMessage) message;
    try {
        final Connection con = this.cf.createConnection();
        final Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final MapMessage res = session.createMapMessage();
        try {
            final String messageID = this.submit(map);
            res.setStringProperty("messageId", messageID);
        } catch (final TransformationException | ValidationException e) {
            BackendJMSImpl.LOG.error("Exception occurred: ", e);
            res.setString("ErrorMessage", e.getMessage());
        }

        res.setJMSCorrelationID(map.getJMSCorrelationID());
        final MessageProducer sender = session.createProducer(this.outQueue);
        sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        sender.send(res);
        sender.close();
        session.close();
        con.close();

    } catch (final JMSException ex) {
        BackendJMSImpl.LOG.error("Error while sending response to queue", ex);

    }
}

From source file:com.mothsoft.alexis.engine.retrieval.DocumentRetrievalTaskImpl.java

private String requestParse(final Long documentId, final String content) {
    Connection connection = null;
    Session session = null;
    MessageProducer producer = null;/*from w ww  .jav a2 s. c  o m*/

    // set up JMS connection, session, consumer, producer
    try {
        connection = this.connectionFactory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(this.requestQueue);

        logger.info("Sending parse request, document ID: " + documentId);
        final TextMessage textMessage = session.createTextMessage(content);
        textMessage.setJMSReplyTo(this.responseQueue);
        textMessage.setLongProperty(DOCUMENT_ID, documentId);

        producer.send(textMessage);
    } catch (JMSException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (producer != null) {
                producer.close();
            }
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (JMSException e) {
            throw new RuntimeException(e);
        }
    }

    return content;
}