FileDocCategorySizeDatePackage
Chat.javaAPI DocExample4463Wed Dec 06 18:55:04 GMT 2000chap2.chat

Chat

public class Chat extends Object implements javax.jms.MessageListener

Fields Summary
private TopicSession
pubSession
private TopicSession
subSession
private TopicPublisher
publisher
private TopicConnection
connection
private String
userName
Constructors Summary
public Chat(String topicName, String username, String password)


        // Obtain a JNDI connection
        Properties env = new Properties();
        // ... specify the JNDI properties specific to the vendor
        env.put("BROKER", "localhost");

        InitialContext jndi = new InitialContext(env);

        // Lookup a JMS connection factory
        TopicConnectionFactory conFactory =
            (TopicConnectionFactory)jndi.lookup("TopicConnectionFactory");

        // Create a JMS connection
        TopicConnection connection =
            conFactory.createTopicConnection(username,password);

        // Create a JMS session object
        TopicSession pubSession =
            connection.createTopicSession(false,
                                      Session.AUTO_ACKNOWLEDGE);
        TopicSession subSession =
            connection.createTopicSession(false,
                                      Session.AUTO_ACKNOWLEDGE);

        // Lookup a JMS topic
        Topic chatTopic = (Topic)jndi.lookup(topicName);

        // Create a JMS publisher and subscriber
        TopicPublisher publisher =
            pubSession.createPublisher(chatTopic);
        TopicSubscriber subscriber =
            subSession.createSubscriber(chatTopic);

        // Set a JMS message listener
        subscriber.setMessageListener(this);

        // Intialize the Chat application
        set(connection, pubSession, subSession, publisher, username);

        // Start the JMS connection; allows messages to be delivered
        connection.start();

    
Methods Summary
public voidclose()

        connection.close();
    
public static voidmain(java.lang.String[] args)

        try{
            if(args.length!=3)
                System.out.println("Topic or username missing");

            // args[0]=topicName; args[1]=username; args[2]=password
            Chat chat = new Chat(args[0],args[1],args[2]);

            // read from command line
            BufferedReader commandLine = new
              java.io.BufferedReader(new
                                     InputStreamReader(System.in));

            // loop until the word "exit" is typed
            while(true){
                String s = commandLine.readLine();
                if(s.equalsIgnoreCase("exit")){
                    chat.close(); // close down connection
                    System.exit(0);// exit program
                }else
                    chat.writeMessage(s);
            }
        }catch(Exception e){ e.printStackTrace(); }
    
public voidonMessage(Message message)

        try {
            TextMessage textMessage = (TextMessage) message;
            String text = textMessage.getText();
            System.out.println(text);
        } catch(JMSException jmse){ jmse.printStackTrace(); }
    
public voidset(TopicConnection con, TopicSession pubSess, TopicSession subSess, TopicPublisher pub, java.lang.String username)

        this.connection = con;
        this.pubSession = pubSess;
        this.subSession = subSess;
        this.publisher = pub;
        this.userName = username;
    
protected voidwriteMessage(java.lang.String text)

        TextMessage message = pubSession.createTextMessage();
        message.setText(userName+" : "+text);
        publisher.publish(message);