KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > jms > topics > GuiChat


1 package sample.jms.topics.GuiChat;
2
3 /*
4  * Copyright 2002 by
5  * <a HREF="http://www.coridan.com">Coridan</a>
6  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
7  *
8  * The contents of this file are subject to the Mozilla Public License Version
9  * 1.1 (the "License"); you may not use this file except in compliance with the
10  * License. You may obtain a copy of the License at
11  * http://www.mozilla.org/MPL/
12  *
13  * Software distributed under the License is distributed on an "AS IS" basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
15  * for the specific language governing rights and limitations under the
16  * License.
17  *
18  * The Original Code is "MantaRay" (TM).
19  *
20  * The Initial Developer of the Original Code is lital kasif.
21  * Portions created by the Initial Developer are Copyright (C) 2006
22  * Coridan Inc. All Rights Reserved.
23  *
24  * Contributor(s): all the names of the contributors are added in the source
25  * code where applicable.
26  *
27  * Alternatively, the contents of this file may be used under the terms of the
28  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
29  * provisions of LGPL are applicable instead of those above. If you wish to
30  * allow use of your version of this file only under the terms of the LGPL
31  * License and not to allow others to use your version of this file under
32  * the MPL, indicate your decision by deleting the provisions above and
33  * replace them with the notice and other provisions required by the LGPL.
34  * If you do not delete the provisions above, a recipient may use your version
35  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
36  
37  *
38  * This library is free software; you can redistribute it and/or modify it
39  * under the terms of the MPL as stated above or under the terms of the GNU
40  * Lesser General Public License as published by the Free Software Foundation;
41  * either version 2.1 of the License, or any later version.
42  *
43  * This library is distributed in the hope that it will be useful, but WITHOUT
44  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
46  * License for more details.
47  *
48  * Created on Jan 09, 2005
49  *
50  * Coridan LTD
51  */

52
53 /**
54  * this class creates a actual MantaRay chat based on the JMS API.
55  * the GuiChat is created by the GuiContiner class, which includes that main that
56  * runs this sample.
57  * @author lital kasif
58  */

59 import javax.jms.*;
60 import org.mr.api.jms.MantaConnectionFactory;
61
62 public class GuiChat implements javax.jms.MessageListener JavaDoc
63 {
64     private static final int MESSAGE_TTL = 6000000;
65     //the connection and session variables
66
private Connection connect = null;
67     private Session pubSession = null;
68     private Session subSession = null;
69     private MessageProducer publisher = null;
70     //the GUIContiner that created this chat
71
GuiContiner GuiDisplay=null;
72
73     //constractor
74
public GuiChat(GuiContiner gui){
75         //the GUIContiner that created this chat
76
GuiDisplay=gui;
77         //Create a connection.
78
try
79         {
80             ConnectionFactory factory;
81             factory = new MantaConnectionFactory();
82             connect = factory.createConnection();
83             pubSession = connect.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
84             subSession = connect.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
85         }
86         catch (javax.jms.JMSException JavaDoc jmse)
87         {
88             System.err.println("error while creating connection - " + jmse.toString());
89             jmse.printStackTrace();
90             System.exit(1);
91         }
92         //creat the chat's main room
93
createRoom("MAIN_ROOM");
94     }//constractor
95

96     /**
97      * creates a topic for a specific chat room
98      * @param chatTopic the name of the new chat room
99      */

100     public void createRoom(String JavaDoc chatTopic) {
101         // Create Publisher and Subscriber to 'chatTopic' topic
102
try
103         {
104             javax.jms.Topic JavaDoc topic = pubSession.createTopic(chatTopic);
105             javax.jms.MessageConsumer JavaDoc subscriber = subSession.createConsumer(topic);
106             subscriber.setMessageListener(this);
107             publisher = pubSession.createProducer(topic);
108             // Now that setup is complete, start the Connection
109
connect.start();
110         }
111         catch (javax.jms.JMSException JavaDoc jmse)
112         {
113             jmse.printStackTrace();
114         }
115     }//createRoom
116

117     /**
118      * publish messages to the 'main room' topic
119      */

120     public void chatter(String JavaDoc username, String JavaDoc message)
121     {
122         try{
123             //publish the message that was inputed to the topic
124
javax.jms.TextMessage JavaDoc msg = pubSession.createTextMessage();
125             msg.setText(username+"> "+message);
126             // Hold messages for MESSAGE_TTL millisecs.
127
publisher.send( msg,
128                    javax.jms.DeliveryMode.NON_PERSISTENT,
129                    javax.jms.Message.DEFAULT_PRIORITY,
130                    MESSAGE_TTL);
131         }
132         catch ( javax.jms.JMSException JavaDoc jmse )
133         {
134             jmse.printStackTrace();
135         }
136     }//chatter
137

138     /**
139      * Handle the message
140      * (as specified in the javax.jms.MessageListener interface).
141      */

142     public void onMessage( javax.jms.Message JavaDoc aMessage)
143     {
144         try
145         {
146             // Cast the message as a text message
147
javax.jms.TextMessage JavaDoc textMessage = (javax.jms.TextMessage JavaDoc) aMessage;
148             //passes the received message to the GUI for display in the chat room
149
try
150             {
151                 String JavaDoc message = textMessage.getText();
152                 GuiDisplay.doChat(message);
153             }
154             catch (javax.jms.JMSException JavaDoc jmse)
155             {
156                 jmse.printStackTrace();
157             }
158         }
159         catch (java.lang.RuntimeException JavaDoc rte)
160         {
161             rte.printStackTrace();
162         }
163     }//onMessage
164

165 }//GuiChat
166

167
Popular Tags