1 24 package dotcom; 25 26 import javax.jms.*; 27 import javax.naming.*; 28 29 42 public class InventoryServer { 43 static Context ictx = null; 44 45 public static void main (String argv[]) throws Exception { 46 47 try { 48 javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 51 52 ictx = new InitialContext(); 54 TopicConnectionFactory tcf; 56 tcf = (TopicConnectionFactory) ictx.lookup("tcf"); 57 Topic topicOrders; 59 topicOrders = (Topic) ictx.lookup("tOrders"); 60 ictx.close(); 61 62 TopicConnection tc = tcf.createTopicConnection("inventory", "inventory"); 64 TopicSession tsession; 66 tsession = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE); 67 TopicSubscriber ts = tsession.createSubscriber(topicOrders); 69 70 fr.dyade.aaa.util.Queue queue ; 72 queue = new fr.dyade.aaa.util.Queue() ; 73 74 TopicListener inventoryListener = new TopicListener(tsession, queue); 76 ts.setMessageListener(inventoryListener); 77 78 InventoryTreatment inventoryTreatment = new InventoryTreatment(queue, tc, tsession) ; 80 java.lang.Thread inventoryThread = new java.lang.Thread (inventoryTreatment) ; 81 inventoryThread.start() ; 82 83 tc.start(); 85 86 } catch (Exception exc) { 87 System.out.println("Exception caught in InventoryServer: " + exc); 88 exc.printStackTrace(); 89 } 90 } 91 } 92 93 94 106 class InventoryTreatment implements Runnable , Servers { 107 static Context ictx = null; 108 109 TopicConnection tc ; 110 111 TopicSession tsession ; 112 113 QueueSession qsession ; 114 115 QueueSender qs ; 116 117 fr.dyade.aaa.util.Queue queue ; 118 119 OrderMessage orderMsg ; 120 121 Object lock ; 122 123 GUI stockGUI ; 124 125 132 InventoryTreatment(fr.dyade.aaa.util.Queue queue, TopicConnection tc, TopicSession tsession) { 133 this.queue = queue ; 134 this.tc = tc ; 135 this.tsession = tsession ; 136 } 137 138 141 public void run() { 142 stockGUI = new GUI("Inventory Server", "Validate", "Don't validate", this, 700, 600) ; 144 145 try { 146 ictx = new InitialContext(); 148 149 QueueConnectionFactory qcf; 151 qcf = (QueueConnectionFactory) ictx.lookup("qcf"); 152 Queue queueItems; 154 queueItems = (Queue) ictx.lookup("qItems"); 155 ictx.close(); 156 157 QueueConnection qc = qcf.createQueueConnection("inventory", "inventory"); 159 qsession = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE); 161 qs = qsession.createSender(queueItems); 163 164 System.out.println("InventoryServer is ready.") ; 165 166 lock = new Object () ; 168 169 while (true) { 170 ObjectMessage msg ; 172 173 msg = (ObjectMessage) queue.get() ; 175 176 msg = (ObjectMessage) queue.pop(); 178 179 if (msg.getObject() instanceof QuitMessage) { 181 tsession.close() ; 182 tc.close() ; 183 qsession.close() ; 184 qc.close() ; 185 186 System.out.println("Sessions and connections closed by InventoryServer."); 187 System.exit(0) ; 188 } 189 190 else if (msg.getObject() instanceof OrderMessage) { 192 orderMsg = (OrderMessage) msg.getObject() ; 194 195 System.out.println("Message received by InventoryServer from WebServer: " + orderMsg.id) ; 196 197 stockGUI.updateId(orderMsg.id) ; 199 stockGUI.updateItem(orderMsg.item) ; 200 stockGUI.setVisible(true) ; 201 202 synchronized(lock) { 204 lock.wait() ; 205 } 206 } 207 } 208 } catch (Exception exc) { 209 System.out.println("Exception caught in InventoryServer thread: " + exc); 210 exc.printStackTrace() ; 211 } 212 } 213 214 217 public void okMethod() { 218 try { 219 stockGUI.setVisible(false) ; 221 222 OkMessage okMsg = new OkMessage(orderMsg.id, orderMsg.item, true) ; 224 ObjectMessage msgSent = qsession.createObjectMessage() ; 226 msgSent.setObject(okMsg) ; 227 qs.send(msgSent); 229 230 qsession.commit() ; 232 233 synchronized(lock) { 235 lock.notify() ; 236 } 237 238 } catch (Exception exc) { 239 System.out.println("Exception caught in InventoryServer okMethod: " + exc); 240 exc.printStackTrace() ; 241 } 242 } 243 244 245 248 public void noMethod() { 249 try { 250 stockGUI.setVisible(false) ; 252 253 OkMessage okMsg = new OkMessage(orderMsg.id, orderMsg.item, false) ; 255 ObjectMessage msgSent = qsession.createObjectMessage() ; 257 msgSent.setObject(okMsg) ; 258 qs.send(msgSent); 260 261 qsession.commit() ; 263 264 synchronized(lock) { 266 lock.notify() ; 267 } 268 269 } catch (Exception exc) { 270 System.out.println("Exception caught in InventoryServer noMethod: " + exc); 271 exc.printStackTrace() ; 272 } 273 } 274 275 278 public void choiceMethod(String choice) {} 279 282 public void otherMethod() {} 283 286 public void sendMethod() {} 287 290 public void cancelMethod() {} 291 294 public void quitMethod() {} 295 298 public void closeMethod() {} 299 } 300 | Popular Tags |