KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dotcom > InventoryServer


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - 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): Frederic Maistre (INRIA)
22  * Contributor(s):
23  */

24 package dotcom;
25
26 import javax.jms.*;
27 import javax.naming.*;
28
29 /**
30  * Launching the InventoryServer that
31  * receives an OrderMessage from WebServer through topicOrders,
32  * creates an OkMessage confirming the order,
33  * and sends the OkMessage to CustomerServer through queueItems.
34  * <br><br>
35  * This code must be executed before WebServer.
36  *
37  * @author Maistre Frederic
38  *
39  * @see Admin
40  * @see InventoryTreatment
41  */

42 public class InventoryServer {
43   static Context ictx = null;
44
45   public static void main (String JavaDoc argv[]) throws Exception JavaDoc {
46
47     try {
48       // setting LAF in order to avoid the following exception :
49
// java.lang.Error: can't load javax.swing.plaf.metal.MetalLookAndFeel
50
javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
51          
52       // getting initial context
53
ictx = new InitialContext();
54       // connecting to agent agInventConnT
55
TopicConnectionFactory tcf;
56       tcf = (TopicConnectionFactory) ictx.lookup("tcf");
57       // connecting to topicOrders
58
Topic topicOrders;
59       topicOrders = (Topic) ictx.lookup("tOrders");
60       ictx.close();
61
62       // creating a TopicConnection
63
TopicConnection tc = tcf.createTopicConnection("inventory", "inventory");
64       // creating a TopicSession
65
TopicSession tsession;
66       tsession = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
67       // creating a TopicSubscriber (receiving from topicOrders)
68
TopicSubscriber ts = tsession.createSubscriber(topicOrders);
69       
70       // creating a FifoQueue to hold incoming messages from topicOrders
71
fr.dyade.aaa.util.Queue queue ;
72       queue = new fr.dyade.aaa.util.Queue() ;
73             
74       // arise the MessageListener
75
TopicListener inventoryListener = new TopicListener(tsession, queue);
76       ts.setMessageListener(inventoryListener);
77     
78       // creating a thread to treat the messages held in queue
79
InventoryTreatment inventoryTreatment = new InventoryTreatment(queue, tc, tsession) ;
80       java.lang.Thread JavaDoc inventoryThread = new java.lang.Thread JavaDoc(inventoryTreatment) ;
81       inventoryThread.start() ;
82             
83       // starting the TopicConnection
84
tc.start();
85        
86     } catch (Exception JavaDoc exc) {
87       System.out.println("Exception caught in InventoryServer: " + exc);
88       exc.printStackTrace();
89     }
90   }
91 }
92
93
94 /**
95  * Thread launched by the main of InventoryServer.
96  *
97  * @author Maistre Frederic
98  *
99  * @see Admin
100  * @see InventoryServer
101  * @see Servers
102  * @see OrderMessage
103  * @see OkMessage
104  * @see GUI
105  */

106 class InventoryTreatment implements Runnable JavaDoc, Servers {
107   static Context ictx = null;
108   /** TopicConnection created by InventoryServer, to be closed in thread. */
109   TopicConnection tc ;
110   /** TopicSession created by InventoryServer, to be closed in thread. */
111   TopicSession tsession ;
112   /** QueueSession sending messages to queueItems. */
113   QueueSession qsession ;
114   /** QueueSender sending OrderMessages. */
115   QueueSender qs ;
116   /** FifoQueue holding OrderMessages received from topicOrders. */
117   fr.dyade.aaa.util.Queue queue ;
118   /** OrderMessage hold by FifoQueue. */
119   OrderMessage orderMsg ;
120   /** Lock to wait for graphical interaction. */
121   Object JavaDoc lock ;
122   /** GUI for validating OrderMessages. */
123   GUI stockGUI ;
124     
125   /**
126    * Creates the thread.
127    *
128    * @param queue FifoQueue in which OrderMessages are held
129    * @param tc TopicConnection created by InventoryServer.
130    * @param tsession TopicSession created by InventoryServer.
131    */

132   InventoryTreatment(fr.dyade.aaa.util.Queue queue, TopicConnection tc, TopicSession tsession) {
133     this.queue = queue ;
134     this.tc = tc ;
135     this.tsession = tsession ;
136   }
137   
138   /**
139    * Method called when starting the thread.
140    */

141   public void run() {
142     // creating the GUI representing the InventoryServer
143
stockGUI = new GUI("Inventory Server", "Validate", "Don't validate", this, 700, 600) ;
144         
145     try {
146       // getting initial context
147
ictx = new InitialContext();
148       
149       // connecting to agent agInventConnQ
150
QueueConnectionFactory qcf;
151       qcf = (QueueConnectionFactory) ictx.lookup("qcf");
152       // connecting to queueItems
153
Queue queueItems;
154       queueItems = (Queue) ictx.lookup("qItems");
155       ictx.close();
156
157       // creating a QueueConnection
158
QueueConnection qc = qcf.createQueueConnection("inventory", "inventory");
159       // creating a QueueSession
160
qsession = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
161       // creating a QueueSender (sending to queueItems)
162
qs = qsession.createSender(queueItems);
163       
164       System.out.println("InventoryServer is ready.") ;
165       
166       // creating the lock object
167
lock = new Object JavaDoc() ;
168       
169       while (true) {
170         // ObjectMessage got from the FifoQueue
171
ObjectMessage msg ;
172         
173         // waiting for the FifoQueue to be filled
174
msg = (ObjectMessage) queue.get() ;
175       
176         // poping out FifoQueue's first OrderMessage
177
msg = (ObjectMessage) queue.pop();
178         
179         // if msg encapsulates a QuitMessage, close sessions and connections
180
if (msg.getObject() instanceof QuitMessage) {
181           tsession.close() ;
182           tc.close() ;
183           qsession.close() ;
184           qc.close() ;
185           
186           System.out.println("Sessions and connections closed by InventoryServer.");
187           System.exit(0) ;
188         }
189         
190         // if msg encapsulates an OrderMessage, treate it
191
else if (msg.getObject() instanceof OrderMessage) {
192           // get OrderMessage
193
orderMsg = (OrderMessage) msg.getObject() ;
194         
195           System.out.println("Message received by InventoryServer from WebServer: " + orderMsg.id) ;
196
197           // updating and activating GUI
198
stockGUI.updateId(orderMsg.id) ;
199           stockGUI.updateItem(orderMsg.item) ;
200           stockGUI.setVisible(true) ;
201
202           // waiting for GUI interaction
203
synchronized(lock) {
204             lock.wait() ;
205           }
206         }
207       }
208     } catch (Exception JavaDoc exc) {
209       System.out.println("Exception caught in InventoryServer thread: " + exc);
210       exc.printStackTrace() ;
211     }
212   }
213   
214   /**
215    * Method called when pressing GUI's okButton.
216    */

217   public void okMethod() {
218     try {
219       // desctivate the GUI
220
stockGUI.setVisible(false) ;
221       
222       // creating the OkMessage to be sent
223
OkMessage okMsg = new OkMessage(orderMsg.id, orderMsg.item, true) ;
224       // creating an ObjectMessage and encapsulating the OkMessage
225
ObjectMessage msgSent = qsession.createObjectMessage() ;
226       msgSent.setObject(okMsg) ;
227       // sending the ObjectMessage to queueItems
228
qs.send(msgSent);
229       
230       // commiting the sending
231
qsession.commit() ;
232       
233       // unlocking
234
synchronized(lock) {
235         lock.notify() ;
236       }
237       
238     } catch (Exception JavaDoc exc) {
239       System.out.println("Exception caught in InventoryServer okMethod: " + exc);
240       exc.printStackTrace() ;
241     }
242   }
243
244
245   /**
246    * Method called when pressing GUI's noButton.
247    */

248   public void noMethod() {
249     try {
250       // deactivate the GUI
251
stockGUI.setVisible(false) ;
252       
253       // creating the OkMessage to be sent
254
OkMessage okMsg = new OkMessage(orderMsg.id, orderMsg.item, false) ;
255       // creating an ObjectMessage and encapsulating the OkMessage
256
ObjectMessage msgSent = qsession.createObjectMessage() ;
257       msgSent.setObject(okMsg) ;
258       // sending the ObjectMessage to queueItems
259
qs.send(msgSent);
260       
261       // commiting the sending
262
qsession.commit() ;
263       
264       // unlocking
265
synchronized(lock) {
266         lock.notify() ;
267       }
268       
269     } catch (Exception JavaDoc exc) {
270       System.out.println("Exception caught in InventoryServer noMethod: " + exc);
271       exc.printStackTrace() ;
272     }
273   }
274   
275   /**
276    * Method inherited from the Servers interface, not implemented.
277    */

278    public void choiceMethod(String JavaDoc choice) {}
279    /**
280     * Method inherited from the Servers interface, not implemented.
281     */

282    public void otherMethod() {}
283    /**
284     * Method inherited from the Servers interface, not implemented.
285     */

286    public void sendMethod() {}
287    /**
288     * Method inherited from the Servers interface, not implemented.
289     */

290    public void cancelMethod() {}
291    /**
292     * Method inherited from the Servers interface, not implemented.
293     */

294    public void quitMethod() {}
295    /**
296     * Method inherited from the Servers interface, not implemented.
297     */

298    public void closeMethod() {}
299 }
300
Popular Tags