MsgServerFS.java.

Java To Go!
JMS in action
by Anthony Patton
Listing 2. MsgServerFS.java.


//MsgServerFS.java
import javax.jms.*;
import javax.naming.*;
    import java.io.*;
    import java.io.InputStreamReader;
    import java.util.Properties;
    import java.util.Hashtable;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    import org.apache.xerces.parsers.SAXParser;
    public class MsgServerFS implements javax.jms.MessageListener{
1. public final static
        String 	JNDI_FACTORY=
        “weblogic.jndi.WLInitialContextFactory”;
        public final static int NUM_COMMODITIES=3;
2. private TopicConnectionFactory conFactory;
        private TopicSession pubSession;
        private TopicSession subSession;
        private TopicPublisher publisher;
        private TopicConnection connection;
        private String userName;
        private TopicSession session, sessionTX;
    public MsgServerFS(String msgName,
                       String username,
                       String password) throws Exception {
        Properties env = new Properties();
        env.put(“BROKER”, “t3://192.168.0.1:7001”);
3. getInitialContext(“t3://192.168.0.1:7001”);
        conFactory =
            (TopicConnectionFactory) jndi.lookup(JMS_FACTORY);
        TopicConnection connection =
            conFactory.createTopicConnection();
4. TopicSession pubSession =
        connection.createTopicSession(false,
        Session.AUTO_ACKNOWLEDGE);
    Topic msgTopic;
5. msgTopic = pubSession.createTopic(TOPIC);
6. jndi.bind(TOPIC, topic);
7. TopicPublisher publisher =
        pubSession.createPublisher(msgTopic);
     set(connection,
           pubSession, subSession, publisher, username);
     connection.start();  }
     public void set(TopicConnection con,
                     TopicSession pubSess,
                     TopicSession subSess,
                     TopicPublisher pub,
                     String username) {
        this.connection = con;
        this.pubSession = pubSess;
        this.subSession = subSess;
        this.publisher = pub;
        this.userName = username;  }
8. protected void writeMessage(String text)
            throws JMSException {
        TextMessage message = pubSession.createTextMessage();
        message.setText(text);
9. publisher.publish(message);  }
        public void close() throws JMSException {
            connection.close();  }
        public static void main(String [] args){
            long delay=5000;
            long now=0;
            long next=0;
            String commodities[] = new String[3];
            commodities[0] = “esh01”;
            commodities[1] = “cn1”;
            commodities[2] = “cz1”;
            StringBuffer msg;
            try{
                if(args.length!=3)
                    System.out.println(“Topic or username missing”);
10. MsgServerFS msgQueue =
            new MsgServerFS(args[0],args[1],args[2]);
            SAXParser parser = new SAXParser();
11. MsgSAX nStr =
                new MsgSAX(NUM_COMMODITIES);
            parser.setDocumentHandler(nStr);
            next = System.currentTimeMillis()
            String pURL = “http://futuresource.com/quote?”;
            String sym = “symbols= esh01,cn1,cz1&”
            String login = “uname=test&pwd=test”;
12. while (true) {
                parser.parse(pURL + sym + login);
                Hashtable vht = nStr.getValues();
                java.util.Enumeration ekeys = vht.elements();
                msg = new StringBuffer(“”);
                msg.append(“ - “ + commodities[0] + “ : “); 
                msg.append((String)vht.get(commodities[0]));
                msg.append(“ - “ + commodities[1] + “ : “);
                msg.append((String)vht.get(commodities[1]));
                msg.append(“ - “ + commodities[2] + “ : “);
                msg.append((String)vht.get(commodities[2]));
13. chat.writeMessage(msg.toString());
                next += delay; // desired delay
                while (next > ((now = System.currentTimeMillis()))) {
                try {
14. Thread.sleep(next - now);
                } catch (InterruptedException e)  {} }}
        }catch(Exception e){
                e.printStackTrace(); } }
    public void onMessage(Message message) { }
    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);
    }} //: end of MsgServerFS.java

Explanation of Listing 2
  1. JNDI is used on the WebLogic server.
  2. JMS objects are declared.
  3. The context is retrieved from the WebLogic server, and JMS objects are initialized.
  4. The JMS session is created.
  5. A new topic is made for the publishing session.
  6. New topic is registered via JNDI. This name is used to access data.
  7. The JMS publishing object is created.
  8. writeMessage method is called when a new message is published.
  9. The method publish sends the message.
  10. A new object is created using command line arguments (message queue, username, and password).
  11. New instance of MsgSAX is created to parse incoming XML documents.
  12. A while loop runs continuously. Quotes are repeatedly requested, parsed, and published.
  13. The data is published by way of the writeMessage method.
  14. A five-second delay is created using the Thread class sleep method.

Featured

Upcoming Events

AppTrends

Sign up for our newsletter.

I agree to this site's Privacy Policy.