1 24 package dotcom; 25 26 import javax.jms.*; 27 import javax.naming.*; 28 29 40 public class WebServer 41 { 42 public static void main (String argv[]) throws Exception 43 { 44 javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 47 48 WebOrdering webOrdering = new WebOrdering() ; 51 java.lang.Thread webThread = new java.lang.Thread (webOrdering) ; 52 webThread.start() ; 53 } 54 } 55 56 57 68 class WebOrdering implements Runnable , Servers 69 { 70 static Context ictx = null; 71 72 TopicConnection tc ; 73 74 TopicSession tsession ; 75 76 TopicPublisher tp ; 77 78 int orderId ; 79 80 String choice ; 81 82 GUI webGUI ; 83 84 87 public void run() 88 { 89 try { 90 ictx = new InitialContext(); 92 TopicConnectionFactory tcf = (TopicConnectionFactory) ictx.lookup("tcf"); 94 Topic topicOrders = (Topic) ictx.lookup("tOrders"); 96 ictx.close(); 97 98 tc = tcf.createTopicConnection("web", "web"); 100 tsession = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE); 102 tp = tsession.createPublisher(topicOrders); 104 105 orderId = 1 ; 107 choice = "Shoes" ; 108 109 webGUI = new GUI("WebServer" , this, 50, 300) ; 111 webGUI.setVisible(true) ; 112 113 System.out.println("WebServer is ready.") ; 114 115 } catch (Exception exc) { 116 System.out.println("Exception caught in WebServer thread: " + exc); 117 exc.printStackTrace(); 118 } 119 } 120 121 124 public void choiceMethod(String choice) { 125 this.choice = choice ; 127 } 128 129 132 public void otherMethod() { 133 try { 134 webGUI.setVisible(false) ; 136 137 OrderMessage orderMsg = new OrderMessage(orderId, choice) ; 139 ObjectMessage msgSent = tsession.createObjectMessage() ; 141 msgSent.setObject(orderMsg) ; 142 tp.publish(msgSent); 144 145 System.out.println("Message sent by WebServer to topicOrders: "); 146 System.out.println("Id: " + orderMsg.id); 147 System.out.println("Item: " + orderMsg.item) ; 148 149 orderId ++ ; 151 152 webGUI.updateId(orderId) ; 154 webGUI.setVisible(true) ; 155 156 } catch (Exception exc) { 157 System.out.println("Exception caught in WebServer otherMethod: " + exc); 158 exc.printStackTrace(); 159 } 160 } 161 162 165 public void sendMethod() { 166 try { 167 webGUI.setVisible(false) ; 169 170 OrderMessage orderMsg = new OrderMessage(orderId, choice) ; 172 ObjectMessage msgSent = tsession.createObjectMessage() ; 174 msgSent.setObject(orderMsg) ; 175 tp.publish(msgSent); 177 178 tsession.commit() ; 180 181 System.out.println("Message sent by WebServer to topicOrders: "); 182 System.out.println("Id: " + orderMsg.id); 183 System.out.println("Item: " + orderMsg.item) ; 184 System.out.println("Sending(s) commited!") ; 185 System.out.println() ; 186 187 orderId = 1 ; 189 190 webGUI.updateId(orderId) ; 192 webGUI.setVisible(true) ; 193 194 } catch (Exception exc) { 195 System.out.println("Exception caught in WebServer sendMethod: " + exc); 196 exc.printStackTrace(); 197 } 198 } 199 200 203 public void cancelMethod() { 204 try { 205 webGUI.setVisible(false) ; 207 208 tsession.rollback() ; 210 211 orderId = 1 ; 213 214 webGUI.updateId(orderId) ; 216 webGUI.setVisible(true) ; 217 218 System.out.println("Sending(s) rolledback!") ; 219 System.out.println() ; 220 221 } catch (Exception exc) { 222 System.out.println("Exception caught in WebServer cancelMethod: " + exc); 223 exc.printStackTrace(); 224 } 225 } 226 227 230 public void quitMethod() { 231 try { 232 webGUI.setVisible(false) ; 234 235 QuitMessage quitMsg = new QuitMessage() ; 237 ObjectMessage msgSent = tsession.createObjectMessage() ; 239 msgSent.setObject(quitMsg) ; 240 tp.publish(msgSent); 242 243 tsession.commit() ; 245 246 tsession.close() ; 248 tc.close() ; 249 250 System.out.println("Session and connection closed by WebServer.") ; 251 System.exit(0) ; 252 253 } catch (Exception exc) { 254 System.out.println("Exception caught in WebServer quitMethod: " + exc); 255 exc.printStackTrace(); 256 } 257 } 258 259 262 public void okMethod() {} 263 266 public void noMethod() {} 267 270 public void closeMethod() {} 271 } 272 | Popular Tags |