KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dotcom > DeliveryServer


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 DeliveryServer that receives an
31  * OrderMessage from CustomerServer through queueDelivery.
32  * <br><br>
33  * This code must be executed before WebServer.
34  *
35  * @author Maistre Frederic
36  *
37  * @see Admin
38  * @see DeliveryTreatment
39  */

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

73 class DeliveryTreatment implements Runnable JavaDoc, Servers {
74   static Context ictx = null;
75   /** GUI displaying incoming OrderMessages. */
76   GUI deliveryGUI ;
77   
78   /**
79    * Method called when starting the thread.
80    */

81   public void run() {
82     // creating the GUI representing the DeliveryServer
83
deliveryGUI = new GUI("Delivery Server", "To be delivered", this, 300, 600);
84         
85     try {
86       // getting initial context
87
ictx = new InitialContext();
88       // connecting to agent agDeliveryConnQ
89
QueueConnectionFactory qcf;
90       qcf = (QueueConnectionFactory) ictx.lookup("qcf");
91       // connecting to queueDelivery
92
Queue queueDelivery ;
93       queueDelivery = (Queue) ictx.lookup("qDelivery");
94       ictx.close();
95
96       // creating a QueueConnection
97
QueueConnection qc = qcf.createQueueConnection("delivery", "delivery");
98       // creating a QueueSession
99
QueueSession qsession ;
100       qsession = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
101       // creating a QueueReceiver
102
QueueReceiver qr = qsession.createReceiver(queueDelivery);
103       // starting the connection
104
qc.start() ;
105       
106       System.out.println("DeliveryServer is ready.") ;
107       
108       while (true) {
109         // receiving an ObjectMessage from queueDelivery
110
ObjectMessage msg = (ObjectMessage) qr.receive() ;
111     
112         // if msg encapsulates a QuitMessage, close session and connection
113
if (msg.getObject() instanceof QuitMessage) {
114           // commiting the reception
115
qsession.commit() ;
116             
117           // closing session and connection
118
qsession.close() ;
119           qc.close() ;
120         
121           System.out.println("Session and connection closed by DeliveryServer.");
122           System.exit(0) ;
123         }
124         
125         // if msg encapsulates an OrderMessage, treat it
126
else if (msg.getObject() instanceof OrderMessage) {
127           // commiting the reception
128
qsession.commit() ;
129             
130           // get OrderMessage
131
OrderMessage orderMsg = (OrderMessage) msg.getObject() ;
132         
133           System.out.println("Message received by DeliveryServer from CustomerServer: " + orderMsg.id) ;
134         
135           // updating and activating the GUI
136
deliveryGUI.updateId(orderMsg.id) ;
137           deliveryGUI.updateItem(orderMsg.item) ;
138           deliveryGUI.setVisible(true) ;
139         }
140       }
141     } catch (Exception JavaDoc exc) {
142       System.out.println("Exception caught in DeliveryServer thread: " + exc) ;
143       exc.printStackTrace() ;
144     }
145   }
146   
147   /**
148    * Method called when pressing closeButton.
149    */

150   public void closeMethod() {
151      deliveryGUI.setVisible(false) ;
152   }
153   
154   /**
155    * Method inherited from the Servers interface, not implemented.
156    */

157   public void choiceMethod(String JavaDoc choice) {}
158   /**
159    * Method inherited from the Servers interface, not implemented.
160    */

161   public void otherMethod() {}
162   /**
163    * Method inherited from the Servers interface, not implemented.
164    */

165   public void sendMethod() {}
166   /**
167    * Method inherited from the Servers interface, not implemented.
168    */

169   public void cancelMethod() {}
170   /**
171    * Method inherited from the Servers interface, not implemented.
172    */

173   public void quitMethod() {}
174   /**
175    * Method inherited from the Servers interface, not implemented.
176    */

177   public void okMethod() {}
178   /**
179    * Method inherited from the Servers interface, not implemented.
180    */

181   public void noMethod() {}
182 }
183
Popular Tags