KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dotcom > CustomerServer


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 CustomerServer that
31  * receives an OrderMessage from WebServer through topicOrders,
32  * waits for BillingServer to confirm the order with an OkMessage through queueChecked,
33  * and waits for InventoryServer to confirm the order with an OkMessage through queueItems.
34  * When confirmed, sends the order to DeliveryServer through queueDelivery.
35  * <br><br>
36  * This code must be executed before WebServer.
37  *
38  * @author Maistre Frederic
39  *
40  * @see Admin
41  * @see CustomerTreatment
42  */

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

107 class CustomerTreatment implements Runnable JavaDoc, Servers {
108   static Context ictx = null;
109   /** TopicConnection created by CustomerServer, to be closed in thread. */
110   TopicConnection tc ;
111   /** TopicSession created by CustomerServer, to be closed in thread. */
112   TopicSession tsession ;
113   /** QueueConnection connecting to queueDelivery. */
114   QueueConnection qc ;
115   /** QueueSession publishing OrderMessages. */
116   QueueSession qsession ;
117   /** QueueSender sending messages. */
118   QueueSender qs ;
119   /** FifoQueue holding OrderMessages received from topicOrders. */
120   fr.dyade.aaa.util.Queue queue ;
121   /** OrderMessage got from the FifoQueue. */
122   OrderMessage orderMsg ;
123   /** Lock to wait for graphical interaction. */
124   Object JavaDoc lock ;
125   /** GUI for validating OrderMessages. */
126   GUI customerGUI1 ;
127   /** GUI for displaying incoming non validated OrderMessages. */
128   GUI customerGUI2 ;
129                  
130   /**
131    * Creates the thread.
132    *
133    * @param queue fifo queue in which OrderMessages are held.
134    * @param tc TopicConnection created in CustomerServer.
135    * @param tsession TopicSession created in CustomerServer.
136    */

137   CustomerTreatment(fr.dyade.aaa.util.Queue queue, TopicConnection tc, TopicSession tsession) {
138     this.queue = queue ;
139     this.tc = tc ;
140     this.tsession = tsession ;
141   }
142   
143   /**
144    * Method called when starting the thread.
145    */

146   public void run() {
147     // creating the GUIs representing the CustomerServer
148
customerGUI1 = new GUI("Customer Server", "Deliver", "Don't deliver", this, 300, 100) ;
149     customerGUI2 = new GUI("Customer Server", "Not validated by StockServer and/or BillingServer", this, 320, 120) ;
150     
151     try {
152       // getting initial context
153
ictx = new InitialContext();
154       // connecting to agent agCustomerConnQ
155
QueueConnectionFactory qcf;
156       qcf = (QueueConnectionFactory) ictx.lookup("qcf");
157       // connecting to queueItems, queueBills and queueDelivery
158
Queue queueItems;
159       queueItems = (Queue) ictx.lookup("qItems");
160       Queue queueBills ;
161       queueBills = (Queue) ictx.lookup("qBills");
162       Queue queueDelivery ;
163       queueDelivery = (Queue) ictx.lookup("qDelivery");
164       ictx.close();
165
166       // creating a QueueConnection
167
QueueConnection qc = qcf.createQueueConnection("customer", "customer");
168       // creating a QueueSession
169
qsession = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
170       // creating a QueueReceiver (receiving from queueItems)
171
QueueReceiver qrItems = qsession.createReceiver(queueItems);
172       // creating a QueueReceiver (receiving from queueBills)
173
QueueReceiver qrBills = qsession.createReceiver(queueBills);
174       // creating a QueueSender (sending to queueDelivery)
175
qs = qsession.createSender(queueDelivery) ;
176       // starting the QueueConnection
177
qc.start() ;
178     
179       // creating lock object
180
lock = new Object JavaDoc() ;
181       
182       System.out.println("CustomerServer is ready.") ;
183       
184       while (true) {
185         // ObjectMessage got from the FifoQueue
186
ObjectMessage msg ;
187         
188         // waiting for the FifoQueue to be filled
189
msg = (ObjectMessage) queue.get() ;
190      
191         // poping out FifoQueue's first ObjectMessage
192
msg = (ObjectMessage) queue.pop();
193         
194         // if msg encapsulates a QuitMessage
195
if (msg.getObject() instanceof QuitMessage) {
196           // getting the QuitMessage
197
QuitMessage quitMsg = (QuitMessage) msg.getObject() ;
198           // creating an ObjectMessage and encapsulating the QuitMessage
199
ObjectMessage msgSent = qsession.createObjectMessage();
200           msgSent.setObject(quitMsg) ;
201           // forwarding the QuitMessage to DeliveryServer
202
qs.send(msgSent) ;
203           qsession.commit() ;
204           
205           // closing sessions and connections
206
tsession.close() ;
207           tc.close() ;
208           qsession.close() ;
209           qc.close() ;
210           
211           System.out.println("Sessions and connections closed by CustomerServer.");
212           System.exit(0) ;
213         }
214         
215         // if msg encapsulates an OrderMessage, treat it
216
else if (msg.getObject() instanceof OrderMessage) {
217           // get encapsulated OrderMessage
218
orderMsg = (OrderMessage) msg.getObject() ;
219         
220           System.out.println("Message received by CustomerServer from WebServer: " + orderMsg.id);
221         
222       // waiting for an ObjectMessage from queueItems
223
ObjectMessage msgRec = (ObjectMessage) qrItems.receive() ;
224       // getting encapsulated OkMessage
225
OkMessage okMsg = (OkMessage)(msgRec.getObject()) ;
226     
227       System.out.println("Message received by CustomerServer from InventoryServer: " + okMsg.id);
228     
229       // updating ok attribute in current OrderMessage
230
orderMsg.inventoryOK = okMsg.ok ;
231
232       // waiting for an ObjectMessage from queueBills
233
msgRec = (ObjectMessage) qrBills.receive() ;
234       // getting encapsulated OkMessage
235
okMsg = (OkMessage)(msgRec.getObject()) ;
236         
237       System.out.println("Message received by CustomerServer from BillingServer: " + okMsg.id);
238       
239       // updating corresponding attribute in current OrderMessage
240
orderMsg.billingOK = okMsg.ok ;
241
242       if (orderMsg.billingOK && orderMsg.inventoryOK) {
243         // updating and activating GUI1
244
customerGUI1.updateId(okMsg.id) ;
245         customerGUI1.updateItem(okMsg.item) ;
246             customerGUI1.setVisible(true) ;
247           }
248       else {
249         // updating and activating GUI2
250
customerGUI2.updateId(okMsg.id) ;
251         customerGUI2.updateItem(okMsg.item) ;
252             customerGUI2.setVisible(true) ;
253           }
254         
255           // waiting for GUI interaction
256
synchronized(lock) {
257             lock.wait() ;
258           }
259         }
260       }
261     } catch (Exception JavaDoc exc) {
262       System.out.println("Exception caught in CustomerServer thread: " + exc);
263       exc.printStackTrace() ;
264     }
265   }
266   
267   /**
268    * Method called when pressing GUI1's okButton.
269    */

270   public void okMethod() {
271     try {
272       // desactivate GUI1
273
customerGUI1.setVisible(false) ;
274
275       // creating an ObjectMessage and encapsulating the OrderMessage
276
ObjectMessage msgSent = qsession.createObjectMessage();
277       msgSent.setObject(orderMsg);
278       // sending the ObjectMessage to DeliveryServer
279
qs.send(msgSent) ;
280       
281       // commiting receivings and sending
282
qsession.commit() ;
283       
284       System.out.println("Message sent to DeliveryServer: " + orderMsg.id) ;
285       
286       // unlocking
287
synchronized(lock) {
288         lock.notify() ;
289       }
290       
291     } catch (Exception JavaDoc exc) {
292       System.out.println("Exception caught in CustomerServer okMethod: " + exc);
293       exc.printStackTrace() ;
294     }
295   }
296
297   /**
298    * Method called when pressing GUI1's noButton.
299    */

300   public void noMethod() {
301     try {
302       // desactivate GUI1
303
customerGUI1.setVisible(false) ;
304     
305       // commiting receivings
306
qsession.commit() ;
307     
308       // unlocking
309
synchronized(lock) {
310         lock.notify() ;
311       }
312     } catch (Exception JavaDoc exc) {
313       System.out.println("Exception caught in CustomerServer noMethod: " + exc);
314       exc.printStackTrace() ;
315     }
316   }
317   
318   /**
319    * Method called when pressing GUI2's closeButton.
320    */

321   public void closeMethod() {
322     try {
323       // desactivate GUI1
324
customerGUI2.setVisible(false) ;
325     
326       // commiting receivings
327
qsession.commit() ;
328         
329       // unlocking
330
synchronized(lock) {
331         lock.notify() ;
332       }
333     } catch (Exception JavaDoc exc) {
334       System.out.println("Exception caught in CustomerServer closeMethod: " + exc);
335       exc.printStackTrace() ;
336     }
337   }
338   
339   /**
340    * Method inherited from the Servers interface, not implemented.
341    */

342   public void choiceMethod(String JavaDoc choice) {}
343   /**
344    * Method inherited from the Servers interface, not implemented.
345    */

346   public void otherMethod() {}
347   /**
348    * Method inherited from the Servers interface, not implemented.
349    */

350   public void sendMethod() {}
351   /**
352    * Method inherited from the Servers interface, not implemented.
353    */

354   public void cancelMethod() {}
355   /**
356    * Method inherited from the Servers interface, not implemented.
357    */

358   public void quitMethod() {}
359 }
360
Popular Tags