1 24 package dotcom; 25 26 import javax.jms.*; 27 import javax.naming.*; 28 29 42 public class ControlServer { 43 44 public static void main (String argv[]) throws Exception { 45 46 try { 47 javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 50 51 ControlTreatment controlTreatment = new ControlTreatment() ; 53 java.lang.Thread controlThread = new java.lang.Thread (controlTreatment) ; 54 controlThread.start() ; 55 56 } catch (Exception exc) { 57 System.out.println("Exception caught in ControlServer: " + exc) ; 58 exc.printStackTrace(); 59 } 60 } 61 } 62 63 64 76 class ControlTreatment implements Runnable , Servers { 77 static Context ictx = null; 78 79 QueueSession qsession; 80 81 QueueSender qs; 82 83 OrderMessage orderMsg ; 84 85 Object lock ; 86 87 GUI controlGUI ; 88 89 92 public void run() { 93 controlGUI = new GUI("Control Server", "Validate", "Don't validate", this, 700, 300) ; 95 96 try { 97 ictx = new InitialContext(); 99 QueueConnectionFactory qcf; 101 qcf = (QueueConnectionFactory) ictx.lookup("qcf"); 102 Queue queueCheck ; 104 queueCheck = (Queue) ictx.lookup("qCheck"); 105 Queue queueChecked; 106 queueChecked = (Queue) ictx.lookup("qChecked"); 107 ictx.close(); 108 109 QueueConnection qc = qcf.createQueueConnection("control", "control"); 111 qsession = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE); 113 QueueReceiver qr = qsession.createReceiver(queueCheck); 115 qs = qsession.createSender(queueChecked); 116 qc.start() ; 118 119 lock = new Object () ; 121 122 System.out.println("ControlServer is ready.") ; 123 124 while (true) { 125 ObjectMessage msg = (ObjectMessage) qr.receive() ; 127 128 if (msg.getObject() instanceof QuitMessage) { 130 qsession.commit() ; 132 133 qsession.close() ; 135 qc.close() ; 136 137 System.out.println("Session and connection closed by ControlServer."); 138 System.exit(0) ; 139 } 140 141 else if (msg.getObject() instanceof OrderMessage) { 143 orderMsg = (OrderMessage) msg.getObject() ; 145 146 System.out.println("Message received by ControlServer from BillingServer: " + orderMsg.id); 147 148 controlGUI.updateId(orderMsg.id) ; 150 controlGUI.updateItem(orderMsg.item) ; 151 controlGUI.setVisible(true) ; 152 153 synchronized(lock) { 155 lock.wait() ; 156 } 157 } 158 } 159 } catch (Exception exc) { 160 System.out.println("Exception caught in ControlServer thread: " + exc) ; 161 exc.printStackTrace() ; 162 } 163 } 164 165 168 public void okMethod() { 169 try { 170 controlGUI.setVisible(false) ; 172 173 OkMessage okMsg = new OkMessage(orderMsg.id, orderMsg.item, true); 175 ObjectMessage msgSent = qsession.createObjectMessage() ; 177 msgSent.setObject(okMsg) ; 178 qs.send(msgSent); 180 181 qsession.commit() ; 183 184 synchronized(lock) { 186 lock.notify() ; 187 } 188 189 } catch (Exception exc) { 190 System.out.println("Exception caught in ControlServer okMethod: " + exc); 191 exc.printStackTrace() ; 192 } 193 } 194 195 198 public void noMethod() { 199 try { 200 controlGUI.setVisible(false) ; 202 203 OkMessage okMsg = new OkMessage(orderMsg.id, orderMsg.item, false); 205 ObjectMessage msgSent = qsession.createObjectMessage() ; 207 msgSent.setObject(okMsg) ; 208 qs.send(msgSent); 210 211 qsession.commit() ; 213 214 synchronized(lock) { 216 lock.notify() ; 217 } 218 219 } catch (Exception exc) { 220 System.out.println("Exception caught in ControlServer noMethod: " + exc); 221 exc.printStackTrace() ; 222 } 223 } 224 225 228 public void choiceMethod(String choice) {} 229 232 public void otherMethod() {} 233 236 public void sendMethod() {} 237 240 public void cancelMethod() {} 241 244 public void quitMethod() {} 245 248 public void closeMethod() {} 249 } 250 | Popular Tags |