1 24 package dotcom; 25 26 import javax.jms.*; 27 import javax.naming.*; 28 29 43 public class CustomerServer { 44 static Context ictx = null; 45 46 public static void main (String argv[]) throws Exception { 47 48 try { 49 javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 52 53 ictx = new InitialContext(); 55 TopicConnectionFactory tcf; 57 tcf = (TopicConnectionFactory) ictx.lookup("tcf"); 58 Topic topicOrders; 60 topicOrders = (Topic) ictx.lookup("tOrders"); 61 ictx.close(); 62 63 TopicConnection tc = tcf.createTopicConnection("customer", "customer"); 65 TopicSession tsession; 67 tsession = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE); 68 TopicSubscriber ts = tsession.createSubscriber(topicOrders); 70 71 fr.dyade.aaa.util.Queue queue ; 73 queue = new fr.dyade.aaa.util.Queue() ; 74 75 TopicListener customerListener = new TopicListener(tsession, queue); 77 ts.setMessageListener(customerListener); 78 79 CustomerTreatment customerTreatment = new CustomerTreatment(queue, tc, tsession) ; 81 java.lang.Thread customerThread = new java.lang.Thread (customerTreatment) ; 82 customerThread.start() ; 83 84 tc.start(); 86 87 } catch (Exception exc) { 88 System.out.println("Exception caught in CustomerServer: " + exc); 89 exc.printStackTrace(); 90 } 91 } 92 } 93 94 95 107 class CustomerTreatment implements Runnable , Servers { 108 static Context ictx = null; 109 110 TopicConnection tc ; 111 112 TopicSession tsession ; 113 114 QueueConnection qc ; 115 116 QueueSession qsession ; 117 118 QueueSender qs ; 119 120 fr.dyade.aaa.util.Queue queue ; 121 122 OrderMessage orderMsg ; 123 124 Object lock ; 125 126 GUI customerGUI1 ; 127 128 GUI customerGUI2 ; 129 130 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 146 public void run() { 147 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 ictx = new InitialContext(); 154 QueueConnectionFactory qcf; 156 qcf = (QueueConnectionFactory) ictx.lookup("qcf"); 157 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 QueueConnection qc = qcf.createQueueConnection("customer", "customer"); 168 qsession = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE); 170 QueueReceiver qrItems = qsession.createReceiver(queueItems); 172 QueueReceiver qrBills = qsession.createReceiver(queueBills); 174 qs = qsession.createSender(queueDelivery) ; 176 qc.start() ; 178 179 lock = new Object () ; 181 182 System.out.println("CustomerServer is ready.") ; 183 184 while (true) { 185 ObjectMessage msg ; 187 188 msg = (ObjectMessage) queue.get() ; 190 191 msg = (ObjectMessage) queue.pop(); 193 194 if (msg.getObject() instanceof QuitMessage) { 196 QuitMessage quitMsg = (QuitMessage) msg.getObject() ; 198 ObjectMessage msgSent = qsession.createObjectMessage(); 200 msgSent.setObject(quitMsg) ; 201 qs.send(msgSent) ; 203 qsession.commit() ; 204 205 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 else if (msg.getObject() instanceof OrderMessage) { 217 orderMsg = (OrderMessage) msg.getObject() ; 219 220 System.out.println("Message received by CustomerServer from WebServer: " + orderMsg.id); 221 222 ObjectMessage msgRec = (ObjectMessage) qrItems.receive() ; 224 OkMessage okMsg = (OkMessage)(msgRec.getObject()) ; 226 227 System.out.println("Message received by CustomerServer from InventoryServer: " + okMsg.id); 228 229 orderMsg.inventoryOK = okMsg.ok ; 231 232 msgRec = (ObjectMessage) qrBills.receive() ; 234 okMsg = (OkMessage)(msgRec.getObject()) ; 236 237 System.out.println("Message received by CustomerServer from BillingServer: " + okMsg.id); 238 239 orderMsg.billingOK = okMsg.ok ; 241 242 if (orderMsg.billingOK && orderMsg.inventoryOK) { 243 customerGUI1.updateId(okMsg.id) ; 245 customerGUI1.updateItem(okMsg.item) ; 246 customerGUI1.setVisible(true) ; 247 } 248 else { 249 customerGUI2.updateId(okMsg.id) ; 251 customerGUI2.updateItem(okMsg.item) ; 252 customerGUI2.setVisible(true) ; 253 } 254 255 synchronized(lock) { 257 lock.wait() ; 258 } 259 } 260 } 261 } catch (Exception exc) { 262 System.out.println("Exception caught in CustomerServer thread: " + exc); 263 exc.printStackTrace() ; 264 } 265 } 266 267 270 public void okMethod() { 271 try { 272 customerGUI1.setVisible(false) ; 274 275 ObjectMessage msgSent = qsession.createObjectMessage(); 277 msgSent.setObject(orderMsg); 278 qs.send(msgSent) ; 280 281 qsession.commit() ; 283 284 System.out.println("Message sent to DeliveryServer: " + orderMsg.id) ; 285 286 synchronized(lock) { 288 lock.notify() ; 289 } 290 291 } catch (Exception exc) { 292 System.out.println("Exception caught in CustomerServer okMethod: " + exc); 293 exc.printStackTrace() ; 294 } 295 } 296 297 300 public void noMethod() { 301 try { 302 customerGUI1.setVisible(false) ; 304 305 qsession.commit() ; 307 308 synchronized(lock) { 310 lock.notify() ; 311 } 312 } catch (Exception exc) { 313 System.out.println("Exception caught in CustomerServer noMethod: " + exc); 314 exc.printStackTrace() ; 315 } 316 } 317 318 321 public void closeMethod() { 322 try { 323 customerGUI2.setVisible(false) ; 325 326 qsession.commit() ; 328 329 synchronized(lock) { 331 lock.notify() ; 332 } 333 } catch (Exception exc) { 334 System.out.println("Exception caught in CustomerServer closeMethod: " + exc); 335 exc.printStackTrace() ; 336 } 337 } 338 339 342 public void choiceMethod(String choice) {} 343 346 public void otherMethod() {} 347 350 public void sendMethod() {} 351 354 public void cancelMethod() {} 355 358 public void quitMethod() {} 359 } 360 | Popular Tags |