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:example.wildcard.Client.java

public static void main(String[] args) {
    String url = BROKER_URL;
    if (args.length > 0) {
        url = args[0].trim();// ww w  .j a  v a  2s .  c o m
    }
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url);
    Connection connection = null;

    try {
        Topic senderTopic = new ActiveMQTopic(System.getProperty("topicName"));

        connection = connectionFactory.createConnection("admin", "password");

        Session senderSession = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        MessageProducer sender = senderSession.createProducer(senderTopic);

        Session receiverSession = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);

        String policyType = System.getProperty("wildcard", ".*");
        String receiverTopicName = senderTopic.getTopicName() + policyType;
        Topic receiverTopic = receiverSession.createTopic(receiverTopicName);

        MessageConsumer receiver = receiverSession.createConsumer(receiverTopic);
        receiver.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                try {
                    if (message instanceof TextMessage) {
                        String text = ((TextMessage) message).getText();
                        System.out.println("We received a new message: " + text);
                    }
                } catch (JMSException e) {
                    System.out.println("Could not read the receiver's topic because of a JMSException");
                }
            }
        });

        connection.start();
        System.out.println("Listening on '" + receiverTopicName + "'");
        System.out.println("Enter a message to send: ");

        Scanner inputReader = new Scanner(System.in);

        while (true) {
            String line = inputReader.nextLine();
            if (line == null) {
                System.out.println("Done!");
                break;
            } else if (line.length() > 0) {
                try {
                    TextMessage message = senderSession.createTextMessage();
                    message.setText(line);
                    System.out.println("Sending a message: " + message.getText());
                    sender.send(message);
                } catch (JMSException e) {
                    System.out.println("Exception during publishing a message: ");
                }
            }
        }

        receiver.close();
        receiverSession.close();
        sender.close();
        senderSession.close();

    } catch (Exception e) {
        System.out.println("Caught exception!");
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                System.out.println("When trying to close connection: ");
            }
        }
    }

}

From source file:example.composite.dest.Consumer.java

public static void main(String[] args) {
    String url = BROKER_URL;
    if (args.length > 0) {
        url = args[0].trim();/*w w  w .ja va  2s  .  co m*/
    }
    System.out.println("\nWaiting to receive messages... will timeout after " + TIMEOUT / 1000 + "s");
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url);
    Connection connection = null;

    try {

        connection = connectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue("test-queue");
        Destination destinationFoo = session.createQueue("test-queue-foo");
        Destination destinationBar = session.createQueue("test-queue-bar");
        Destination destinationTopicFoo = session.createTopic("test-topic-foo");

        MessageConsumer consumer = session.createConsumer(destination);
        MessageConsumer consumerFoo = session.createConsumer(destinationFoo);
        MessageConsumer consumerBar = session.createConsumer(destinationBar);
        MessageConsumer consumerTopicFoo = session.createConsumer(destinationTopicFoo);

        int i = 0;
        while (true) {
            Message message = consumer.receive(TIMEOUT);

            if (message != null) {
                if (message instanceof TextMessage) {
                    String text = ((TextMessage) message).getText();
                    System.out.println("Got " + i++ + ". message on test-queue: " + text);
                }
            } else {
                break;
            }

            message = consumerFoo.receive(TIMEOUT);

            if (message != null) {
                if (message instanceof TextMessage) {
                    String text = ((TextMessage) message).getText();
                    System.out.println("Got " + i++ + ". message on test-queue-foo: " + text);
                }
            } else {
                break;
            }

            message = consumerBar.receive(TIMEOUT);

            if (message != null) {
                if (message instanceof TextMessage) {
                    String text = ((TextMessage) message).getText();
                    System.out.println("Got " + i++ + ". message on test-queue-bar: " + text);
                }
            } else {
                break;
            }

            message = consumerTopicFoo.receive(TIMEOUT);

            if (message != null) {
                if (message instanceof TextMessage) {
                    String text = ((TextMessage) message).getText();
                    System.out.println("Got " + i++ + ". message on test-topic-bar: " + text);
                }
            } else {
                break;
            }

        }

        consumer.close();
        session.close();

    } catch (Exception e) {
        System.out.println("Caught exception!");
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                System.out.println("Could not close an open connection...");
            }
        }
    }
}

From source file:com.mirth.connect.connectors.jms.JmsUtils.java

public static void close(Session session) throws JMSException {
    if (session != null) {
        session.close();
    }/*  w  w w.  ja  va 2s.com*/
}

From source file:com.mirth.connect.connectors.jms.JmsUtils.java

public static void closeQuietly(Session session) {
    if (session != null) {
        try {//from   www .j  a va2s  .c o  m
            session.close();
        } catch (JMSException e) {
            logger.error("Failed to close jms session consumer", e);
        }
    }
}

From source file:org.dawnsci.commandserver.core.util.JSONUtils.java

/**
 * Generic way of sending a topic notification
 * @param connection - does not get closed afterwards nust be started before.
 * @param message//from w  w w . j  ava  2s . c o m
 * @param topicName
 * @param uri
 * @throws Exception
 */
private static final void sendTopic(Connection connection, Object message, String topicName, URI uri)
        throws Exception {

    // JMS messages are sent and received using a Session. We will
    // create here a non-transactional session object. If you want
    // to use transactions you should set the first parameter to 'true'
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    try {
        Topic topic = session.createTopic(topicName);

        MessageProducer producer = session.createProducer(topic);

        final ObjectMapper mapper = new ObjectMapper();

        // Here we are sending the message out to the topic
        TextMessage temp = session.createTextMessage(mapper.writeValueAsString(message));
        producer.send(temp, DeliveryMode.NON_PERSISTENT, 1, 5000);

    } finally {
        session.close();
    }
}

From source file:org.fusesource.stompjms.JmsTestSupport.java

protected void safeClose(Session s) {
    try {
        s.close();
    } catch (Throwable e) {
    }
}

From source file:org.fusesource.stompjms.JmsTestSupport.java

protected void sendMessages(Connection connection, Destination destination, int count) throws JMSException {
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    sendMessages(session, destination, count);
    session.close();
}

From source file:org.soitoolkit.commons.studio.components.logger.impl.DefaultLogEventSender.java

protected void dispatchEvent(String queue, String msg) {
    try {//  w w  w  .  j  av a  2 s  .  co m

        Session s = null;
        try {
            s = getSession();
            sendOneTextMessage(s, queue, msg);
        } finally {
            if (s != null)
                s.close();
        }

    } catch (JMSException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.linagora.obm.sync.TestQueueManager.java

private void shutdownQueueManager() {
    for (Session s : sessions) {
        try {// ww w .ja v  a 2 s  .  co m
            s.close();
        } catch (JMSException e) {
            logger.error(e.getMessage(), e);
        }
    }
    for (Connection c : connections) {
        try {
            c.stop();
        } catch (JMSException e) {
            logger.error(e.getMessage(), e);
        }
        try {
            c.close();
        } catch (JMSException e) {
            logger.error(e.getMessage(), e);
        }
    }
    try {
        queueManager.stop();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:com.nesscomputing.jms.activemq.ServiceDiscoveryTransportFactoryTest.java

private void sendTestMessage(final Connection directConnection) throws Exception {
    final Session session = directConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    final MessageProducer producer = session.createProducer(session.createQueue(QNAME));
    producer.send(session.createTextMessage(uniqueId));
    session.close();
}