KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dotcom > BillingServer


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

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

102 class BillingTreatment implements Runnable JavaDoc {
103   static Context ictx = null;
104   /** TopicConnection created by BillingServer, to be closed in thread. */
105   TopicConnection tc ;
106   /** TopicSession created by BillingServer, to be closed in thread. */
107   TopicSession tsession ;
108   /** FifoQueue holding OrderMessages received from topicOrders. */
109   fr.dyade.aaa.util.Queue queue ;
110   
111   /**
112    * Creates the thread.
113    *
114    * @param queue FifoQueue in which OrderMessages are held.
115    * @param tc TopicConnection created by BillingServer.
116    * @param tsession TopicSession created by BillingServer.
117    */

118   BillingTreatment(fr.dyade.aaa.util.Queue queue, TopicConnection tc, TopicSession tsession) {
119     this.queue = queue ;
120     this.tc = tc ;
121     this.tsession = tsession ;
122   }
123   
124   /**
125    * Method called when starting the thread.
126    */

127   public void run() {
128     try {
129       // getting initial context
130
ictx = new InitialContext();
131       // connecting to agent agBillingConnQ
132
QueueConnectionFactory qcf ;
133       qcf = (QueueConnectionFactory) ictx.lookup("qcf");
134       // connecting to queueCheck, queueChecked and queueBills
135
Queue queueCheck ;
136       queueCheck = (Queue) ictx.lookup("qCheck");
137       Queue queueChecked ;
138       queueChecked = (Queue) ictx.lookup("qChecked");
139       Queue queueBills ;
140       queueBills = (Queue) ictx.lookup("qBills");
141       ictx.close();
142
143       // creating a QueueConnection
144
QueueConnection qc = qcf.createQueueConnection("billing", "billing");
145       // creating a QueueSession
146
QueueSession qsession = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
147       // creating a QueueSender (sending to queueCheck)
148
QueueSender qsCheck = qsession.createSender(queueCheck);
149       // creating a QueueReceiver (receiving from queueChecked)
150
QueueReceiver qr = qsession.createReceiver(queueChecked);
151       // creating a QueueSender (sending to queueBills)
152
QueueSender qsBills = qsession.createSender(queueBills);
153       // starting the QueueConnection
154
qc.start() ;
155       
156       System.out.println("BillingServer is ready.");
157       
158       while (true) {
159         // ObjectMessage got from the FifoQueue
160
ObjectMessage msg ;
161         
162         // waiting for the FifoQueue to be filled
163
msg = (ObjectMessage) queue.get() ;
164       
165         // poping out FifoQueue's first ObjectMessage
166
msg = (ObjectMessage) queue.pop();
167         
168         // if msg encapsulates a QuitMessage, close sessions and connections
169
if (msg.getObject() instanceof QuitMessage) {
170           // getting the QuitMessage
171
QuitMessage quitMsg = (QuitMessage) msg.getObject() ;
172           // creating an ObjectMessage and encapsulating the QuitMessage
173
ObjectMessage msgSent = qsession.createObjectMessage();
174           msgSent.setObject(quitMsg) ;
175           // forwarding the QuitMessage to ControlServer
176
qsCheck.send(msgSent) ;
177           qsession.commit() ;
178           
179           // closing sessions and connections
180
tsession.close() ;
181           tc.close() ;
182           qsession.close() ;
183           qc.close() ;
184           
185           System.out.println("Sessions and connections closed by BillingServer.");
186           System.exit(0) ;
187         }
188         
189         // if msg encapsulates an OrderMessage, treat it
190
else if (msg.getObject() instanceof OrderMessage) {
191           // get OrderMessage
192
OrderMessage orderMsg = (OrderMessage) msg.getObject() ;
193         
194           System.out.println("Message received by BillingServer from WebServer: " + orderMsg.id) ;
195          
196           // creating an ObjectMessage and encapsulating the OrderMessage
197
ObjectMessage msgSent = qsession.createObjectMessage();
198           msgSent.setObject(orderMsg) ;
199           // sending the ObjectMessage to topicCheck
200
qsCheck.send(msgSent) ;
201           // commiting the sending
202
qsession.commit() ;
203     
204           // receiving an ObjectMessage from queueChecked
205
ObjectMessage msgRec = (ObjectMessage) qr.receive() ;
206           // getting encapsulated OkMessage
207
OkMessage okMsg = (OkMessage)(msgRec.getObject()) ;
208     
209           System.out.println("Message received by BillingServer from ControlServer: " + okMsg.id) ;
210      
211           // creating an ObjectMessage and encapsulating the OkMessage
212
msgSent = qsession.createObjectMessage() ;
213           msgSent.setObject(okMsg) ;
214           // sending the ObjectMessage to queueBills
215
qsBills.send(msgSent);
216     
217           // commiting reception from queueChecked and sending to queueBills
218
qsession.commit() ;
219         }
220       }
221     } catch (Exception JavaDoc exc) {
222       System.out.println("Exception caught in BillingServer thread: " + exc) ;
223       exc.printStackTrace() ;
224     }
225   }
226 }
227
Popular Tags