users@glassfish.java.net

Simple JMS Client doesn't quit

From: <glassfish_at_javadesktop.org>
Date: Sun, 24 Feb 2008 22:29:08 PST

I have a very simple JMS client. It seems to work fine.

The only issue is that the process doesn't quit.

[code]
    public static void main(String[] args) {
        // TODO code application logic here
        Main m = new Main();
        m.sendMessage("world");
    }
    
    private String sendMessage(String text) {
        Connection connection = null;
        Session session = null;
        ConnectionFactory newMessageFactory;
        Queue newMessage;
        
        try {
            try {
                InitialContext ic;
                ic = new InitialContext();
                newMessageFactory = (ConnectionFactory)ic.lookup("jms/NewMessageFactory");
                newMessage = (Queue)ic.lookup("jms/NewMessage");
                
                System.out.println("Sending " + text);
                connection = newMessageFactory.createConnection();
                session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
                TemporaryQueue replyQ = session.createTemporaryQueue();
                
                MessageProducer messageProducer = session.createProducer(newMessage);
                TextMessage tm = session.createTextMessage();
                tm.setText(text);
                tm.setJMSReplyTo(replyQ);
                messageProducer.send(tm);
                System.out.println("Sent " + text);
                MessageConsumer recv = session.createConsumer(replyQ);
                connection.start();
                Message m = recv.receive();
                tm = (TextMessage)m;
                System.out.println(tm.getText());
                
            } finally {
                if (session != null) {
                    session.close();
                }
                if (connection != null) {
                    connection.close();
                }
            }
        } catch (JMSException ex) {
            ex.printStackTrace();
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
        return null;
    }
[/code]

I think it's clear that there's an internal thread still running, but I thought I was closing everything I needed to close.

Any hints appreciated.
[Message sent by forum member 'whartung' (whartung)]

http://forums.java.net/jive/thread.jspa?messageID=260661