Sample Client Application.

Java To Go!
JMS in action
by Anthony Patton
Listing 3. Sample Client Application.


// MsgClientFS.java
import javax.jms.*;
import javax.naming.*;
import java.io.*;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.Hashtable;
public class MsgClientFS implements javax.jms.MessageListener{
     public final static String
        JNDI_FACTORY=”weblogic.jndi.WLInitialContextFactory”;
     public final static String
        JMS_FACTORY=”javax.jms.TopicConnectionFactory”;
     public final static String TOPIC=”javax.jms.exampleTopic”;
     private TopicConnectionFactory conFactory;
     private TopicSession subSession;
     private TopicConnection connection;
     private String userName;
     private Topic topic;
1. public MsgClientFS(String topicName,
                      String username,
                      String password)
             throws Exception {
        Properties env = new Properties();
        env.put(“BROKER”, “t3://192.168.0.1:7001”);
2. InitialContext jndi =
            getInitialContext(“t3://192.168.0.1:7001”);
        conFactory =
            (TopicConnectionFactory) jndi.lookup(JMS_FACTORY);
        TopicConnection connection =
            conFactory.createTopicConnection();
        TopicSession subSession =
            connection.createTopicSession(false,
                                   Session.AUTO_ACKNOWLEDGE);
        Topic msg;
        try {
            msg = (Topic) jndi.lookup(TOPIC);
        } catch (NamingException ne) {
            msg = subSession.createTopic(TOPIC);
            jndi.bind(TOPIC, topic);  }
            TopicSubscriber subscriber =
                subSession.createSubscriber(msg);
            subscriber.setMessageListener(this);
            set(connection, subSession, username);
3.        connection.start();     }
     public void set(TopicConnection con,
                     TopicSession subSess,
                     String username) {
            this.connection = con;
            this.subSession = subSess;
            this.userName = username; }
4. public void onMessage(Message message) {
            try {
5.        TextMessage textMessage =
                (TextMessage) message;
6.            String text = textMessage.getText();
                System.out.println(text);
            } catch(JMSException jmse){
                jmse.printStackTrace(); }}
        public void close() throws JMSException {
            connection.close();    }
        public static void main(String [] args){
            try{
                if(args.length!=3)
                    System.out.println(“Topic or username missing”);
                 MsgClientFS readQueue =
                    new MsgClientFS(args[0],args[1],args[2]);
                BufferedReader commandLine =
                    new java.io.BufferedReader(
                    new InputStreamReader(System.in));
            }catch(Exception e){
                e.printStackTrace(); } }
        private static InitialContext getInitialContext(String url)
                    throws NamingException  {
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY,
                        JNDI_FACTORY);
            env.put(Context.PROVIDER_URL, url);
            return new InitialContext(env);  } }
Explanation
1. The constructor accepts three arguments: message queue, username, and session.
2. The WebLogic server IP address is used.
3. The message service is started. Messages are received.
4. The onMessage method is fired upon message receipt.
5. The message is converted to a TextMessage object.
6. A String object is created with the message text.