KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > chat > SimpleChat


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2004 ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - 2000 Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Jose Carlos Waeny
22  * Contributor(s): ScalAgent Distributed Technologies
23  */

24 package chat;
25
26 import javax.jms.*;
27 import javax.naming.*;
28 import java.io.*;
29 import java.io.InputStreamReader JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 /**
33  * Launching Simple Chat:
34  * connecting to JORAM server, creating chat agents, creating topic
35  *
36  * @author JC Waeny
37  * @email jc@waeny.2y.net
38  * @version 1.0
39  */

40 public class SimpleChat implements javax.jms.MessageListener JavaDoc{
41     
42     private ConnectionFactory conFactory;
43     private Session pubSession;
44     private Session subSession;
45     private MessageProducer publisher;
46     private MessageConsumer subscriber;
47     private Connection connection;
48     private String JavaDoc userName;
49     
50     /* Constructor. Establish JMS publisher and subscriber */
51     public SimpleChat(String JavaDoc topicName, String JavaDoc username) throws Exception JavaDoc {
52         
53         InitialContext jndi = null;
54         
55         try {
56             jndi = new InitialContext();
57         } catch( Exception JavaDoc e) {
58             System.out.println( e.toString() );
59             System.exit(2);
60         }
61         
62         // Look up a JMS connection factory
63
conFactory = (ConnectionFactory)jndi.lookup("factoryChat");
64         
65         // Create a JMS connection
66
connection = conFactory.createConnection();
67         
68         // Create two JMS session objects
69
pubSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
70         subSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
71         
72         // Look up a JMS topic
73
Topic chatTopic = (Topic)jndi.lookup(topicName);
74         
75         // Create a JMS publisher and subscriber
76
publisher = pubSession.createProducer(chatTopic);
77         subscriber = subSession.createConsumer(chatTopic);
78         
79         // Set a JMS message listener
80
subscriber.setMessageListener(this);
81         
82         // Start the JMS connection; allows messages to be delivered
83
userName = username;
84         connection.start( );
85         
86     }
87     
88     /* Receive message from topic subscriber */
89     public void onMessage(Message message) {
90         try {
91             TextMessage textMessage = (TextMessage) message;
92             String JavaDoc text = textMessage.getText( );
93             System.out.println(textMessage.getStringProperty("User") + ": " + text);
94         } catch (JMSException jmse) {
95             jmse.printStackTrace( );
96         }
97     }
98     
99     /* Create and send message using topic publisher */
100     protected void writeMessage(String JavaDoc text) throws JMSException {
101         TextMessage message = pubSession.createTextMessage( );
102         message.setText(text);
103         message.setStringProperty("User", userName);
104         publisher.send(message);
105     }
106     
107     /* Close the JMS connection */
108     public void close( ) throws JMSException {
109         connection.close( );
110     }
111     
112     /* Run the Chat client */
113     public static void main(String JavaDoc [] args){
114         
115         SimpleChat chat = null;
116         String JavaDoc user = null;
117         
118         try {
119             user = args[0];
120         } catch(Exception JavaDoc e) {
121             user = "No name";
122         }
123          
124         try {
125             
126             chat = new SimpleChat("topicChat", user);
127             
128             // Read from command line
129
BufferedReader commandLine = new
130             java.io.BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
131             
132             // Loop until the word "exit" is typed
133

134             System.out.println("User: " + user + " connected !");
135             System.out.println("Type your phrases and press 'ENTER' ...");
136             System.out.println("Type 'exit' or 'quit' to abandon the chat.");
137             
138             while(true) {
139                 String JavaDoc s = commandLine.readLine( );
140                 if ( s.equalsIgnoreCase("exit") || s.equalsIgnoreCase("quit") ) {
141                     chat.close( ); // close down connection
142
System.exit(0);// exit program
143
} else
144                     chat.writeMessage(s);
145             }
146             
147         } catch(Exception JavaDoc e) {
148             System.out.println( e.toString());
149         }
150     }
151 }
152
Popular Tags