KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dotcom > ControlServer


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 ControlServer that
31  * receives an OrderMessage from BillingServer through queueCheck,
32  * creates an OkMessage to confirm the order and sends the OkMessage to
33  * BillingServer through queueChecked.
34  * <br><br>
35  * This code must be executed before WebServer.
36  *
37  * @author Maistre Frederic
38  *
39  * @see Admin
40  * @see ControlTreatment
41  */

42 public class ControlServer {
43   
44   public static void main (String JavaDoc argv[]) throws Exception JavaDoc {
45     
46     try {
47       // setting LAF in order to avoid the following exception :
48
// java.lang.Error: can't load javax.swing.plaf.metal.MetalLookAndFeel
49
javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
50       
51       // creating a thread to receive and treat the messages from queueCheck
52
ControlTreatment controlTreatment = new ControlTreatment() ;
53       java.lang.Thread JavaDoc controlThread = new java.lang.Thread JavaDoc(controlTreatment) ;
54       controlThread.start() ;
55        
56     } catch (Exception JavaDoc exc) {
57       System.out.println("Exception caught in ControlServer: " + exc) ;
58       exc.printStackTrace();
59     }
60   }
61 }
62
63
64 /**
65  * Thread launched by the main of ControlServer.
66  *
67  * @author Maistre Frederic
68  *
69  * @see Admin
70  * @see ControlServer
71  * @see Servers
72  * @see OrderMessage
73  * @see OkMessage
74  * @see GUI
75  */

76 class ControlTreatment implements Runnable JavaDoc, Servers {
77   static Context ictx = null;
78   /** QueueSession for receiving and sending messages. */
79   QueueSession qsession;
80   /** QueueSender sending OkMessages. */
81   QueueSender qs;
82   /** OrderMessage got from queueCheck. */
83   OrderMessage orderMsg ;
84   /** Lock to wait for graphical interaction. */
85   Object JavaDoc lock ;
86   /** GUI for validating the OrderMessages. */
87   GUI controlGUI ;
88   
89   /**
90    * Method called when starting the thread
91    */

92   public void run() {
93     // creating the GUI representing the ControlServer
94
controlGUI = new GUI("Control Server", "Validate", "Don't validate", this, 700, 300) ;
95     
96     try {
97       // getting initial context
98
ictx = new InitialContext();
99       // connecting to agent agControlConnQ
100
QueueConnectionFactory qcf;
101       qcf = (QueueConnectionFactory) ictx.lookup("qcf");
102       // connecting to queueCheck and queueChecked
103
Queue queueCheck ;
104       queueCheck = (Queue) ictx.lookup("qCheck");
105       Queue queueChecked;
106       queueChecked = (Queue) ictx.lookup("qChecked");
107       ictx.close();
108
109       // creating a QueueConnection
110
QueueConnection qc = qcf.createQueueConnection("control", "control");
111       // creating a QueueSession
112
qsession = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
113       // creating a QueueReceiver (receiving from queueCheck) and a QueueSender (sending to queueChecked)
114
QueueReceiver qr = qsession.createReceiver(queueCheck);
115       qs = qsession.createSender(queueChecked);
116       // starting the connection
117
qc.start() ;
118       
119       // creating the lock object
120
lock = new Object JavaDoc() ;
121       
122       System.out.println("ControlServer is ready.") ;
123          
124       while (true) {
125         // receiving an ObjectMessage from queueCheck
126
ObjectMessage msg = (ObjectMessage) qr.receive() ;
127     
128         // if msg encapsulates a QuitMessage
129
if (msg.getObject() instanceof QuitMessage) {
130           // commiting the reception
131
qsession.commit() ;
132         
133           // closing session and connection
134
qsession.close() ;
135           qc.close() ;
136           
137           System.out.println("Session and connection closed by ControlServer.");
138           System.exit(0) ;
139         }
140         
141         // if msg encapsulates an OrderMessage, treat it
142
else if (msg.getObject() instanceof OrderMessage) {
143           // get OrderMessage
144
orderMsg = (OrderMessage) msg.getObject() ;
145         
146           System.out.println("Message received by ControlServer from BillingServer: " + orderMsg.id);
147         
148           // updating and activating GUI
149
controlGUI.updateId(orderMsg.id) ;
150           controlGUI.updateItem(orderMsg.item) ;
151           controlGUI.setVisible(true) ;
152         
153           // waiting for GUI interaction
154
synchronized(lock) {
155             lock.wait() ;
156           }
157         }
158       }
159     } catch (Exception JavaDoc exc) {
160       System.out.println("Exception caught in ControlServer thread: " + exc) ;
161       exc.printStackTrace() ;
162     }
163   }
164   
165   /**
166    * Method called when pressing GUI's okButton.
167    */

168   public void okMethod() {
169     try {
170       // deactivating the GUI
171
controlGUI.setVisible(false) ;
172       
173       // creating the OkMessage to be sent
174
OkMessage okMsg = new OkMessage(orderMsg.id, orderMsg.item, true);
175       // creating an ObjectMessage and encapsulating the OkMessage
176
ObjectMessage msgSent = qsession.createObjectMessage() ;
177       msgSent.setObject(okMsg) ;
178       // sending the ObjectMessage to queueChecked
179
qs.send(msgSent);
180       
181       // commiting receiving and sending
182
qsession.commit() ;
183       
184       // unlocking
185
synchronized(lock) {
186         lock.notify() ;
187       }
188       
189     } catch (Exception JavaDoc exc) {
190       System.out.println("Exception caught in ControlServer okMethod: " + exc);
191       exc.printStackTrace() ;
192     }
193   }
194
195   /**
196    * Method called when pressing GUI's noButton.
197    */

198   public void noMethod() {
199     try {
200       // deactivating the GUI
201
controlGUI.setVisible(false) ;
202       
203       // creating the OkMessage to be sent
204
OkMessage okMsg = new OkMessage(orderMsg.id, orderMsg.item, false);
205       // creating an ObjectMessage and encapsulating the OkMessage
206
ObjectMessage msgSent = qsession.createObjectMessage() ;
207       msgSent.setObject(okMsg) ;
208       // sending the OkMessage to queueChecked
209
qs.send(msgSent);
210       
211       // commiting receiving and sending
212
qsession.commit() ;
213       
214       // unlocking
215
synchronized(lock) {
216         lock.notify() ;
217       }
218       
219     } catch (Exception JavaDoc exc) {
220       System.out.println("Exception caught in ControlServer noMethod: " + exc);
221       exc.printStackTrace() ;
222     }
223   }
224   
225   /**
226    * Method inherited from the Servers interface, not implemented.
227    */

228   public void choiceMethod(String JavaDoc choice) {}
229   /**
230    * Method inherited from the Servers interface, not implemented.
231    */

232   public void otherMethod() {}
233   /**
234    * Method inherited from the Servers interface, not implemented.
235    */

236   public void sendMethod() {}
237   /**
238    * Method inherited from the Servers interface, not implemented.
239    */

240   public void cancelMethod() {}
241   /**
242    * Method inherited from the Servers interface, not implemented.
243    */

244   public void quitMethod() {}
245   /**
246    * Method inherited from the Servers interface, not implemented.
247    */

248   public void closeMethod() {}
249 }
250
Popular Tags