users@glassfish.java.net

Re: Simple JMS Client doesn't quit

From: <glassfish_at_javadesktop.org>
Date: Mon, 25 Feb 2008 22:07:38 PST

First, are these properties exposed on the Admin GUI? I made changes there, but the names may be different.

I found some interesting results.

What happened is that my client still didn't quit when (apparently) the connection timed out. Rather, it continued to reconnect (you could see it in the log messages it was printing out).

I also tried to create a client listener, and I wanted to send several messages to it. With a single message, this listener works. But the client sends the second one and hangs after that.

At this point, I have my MDB undeployed, with just the queue and connection factory deployed on the app server.

I call this once with any argument to start the listener, and then in a another shell window I run it again to send the messages.

Any ideas?

[code]
/*
 * Main.java
 *
 * Created on February 24, 2008, 9:01 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jmstest;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TemporaryQueue;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Main {
    
    public Main() {
    }
    
    public static void main(String[] args) {
        // TODO code application logic here
        Main m = new Main();
        if (args.length == 0) {
            m.sendMessage("world");
        } else {
            m.recvMessage();
        }
    }
    
    private void recvMessage() {
        Connection connection = null;
        Session session = null;
        ConnectionFactory newMessageFactory;
        Queue newMessage;
        
        System.out.println("Receiving messages...");
        try {
            try {
                InitialContext ic;
                ic = new InitialContext();
                newMessageFactory = (ConnectionFactory)ic.lookup("jms/NewMessageFactory");
                newMessage = (Queue)ic.lookup("jms/NewMessage");
                
                connection = newMessageFactory.createConnection();
                session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
                MessageConsumer recv = session.createConsumer(newMessage);
                connection.start();
                while(true) {
                    Message m = recv.receive();
                    TextMessage tm = (TextMessage)m;
                    System.out.println(tm.getText());
                    
                    MessageProducer messageProducer = session.createProducer(tm.getJMSReplyTo());
                    String result = "ok";
                    TextMessage newtm = session.createTextMessage();
                    newtm.setText(result);
                    newtm.setJMSCorrelationID(tm.getJMSMessageID());
                    messageProducer.send(newtm);
                    System.out.println("Sent " + result);
                    messageProducer.close();
                }
            } finally {
                if (session != null) {
                    session.close();
                }
                if (connection != null) {
                    connection.close();
                }
            }
        } catch (JMSException ex) {
            ex.printStackTrace();
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }
    
    private String sendMessage(String text) {
        Connection connection = null;
        Session session = null;
        ConnectionFactory newMessageFactory;
        Queue newMessage;
        
        System.out.println("Sending " + text);
        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);
                for(int i = 0; i < 10; i++) {
                    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("" + i + " - " + 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]
[Message sent by forum member 'whartung' (whartung)]

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