1 package sample.jms.StockExample; 2 3 4 5 54 import java.util.HashMap ; 55 import java.util.Iterator ; 56 57 import javax.jms.*; 58 59 67 public class StockStatus extends Thread implements javax.jms.MessageListener { 68 private static final int MESSAGE_TTL = 6000000; 69 70 private javax.jms.Connection con = null; 71 private javax.jms.Session pubSession = null; 72 private javax.jms.Session subSession = null; 73 private javax.jms.MessageProducer publisher = null; 74 private javax.jms.Queue queryConsumer=null; 75 private javax.jms.Topic stockProducer=null; 76 private HashMap stocksMap=null; 78 79 public StockStatus(){ 81 super(); 82 stocksMap=new HashMap (); 83 } 84 85 89 public void run(){ 90 while(true){ 92 Iterator companiesIterator = stocksMap.keySet().iterator(); 93 while(companiesIterator.hasNext()){ 95 String company = (String )companiesIterator.next(); 96 Integer value=(Integer )stocksMap.get(company); 97 int makeValue=0; 98 makeValue=greaterValue(value.intValue()); 99 value = new Integer (makeValue); 100 stocksMap.put(company,value); 101 publish(company,makeValue); 103 } try { 105 Thread.sleep(3000); 106 } catch (InterruptedException e) { 107 e.printStackTrace(); 108 } 109 } } 112 115 public void createDestinations(){ 116 try 118 { 119 con = StockPublisherExample.conFactory.createConnection(); 121 pubSession = con.createSession(false,Session.AUTO_ACKNOWLEDGE); 123 subSession = con.createSession(false,Session.AUTO_ACKNOWLEDGE); 124 } 125 catch (javax.jms.JMSException jmse) 126 { 127 jmse.printStackTrace(); 128 System.exit(1); 129 } 130 try { 131 queryConsumer = subSession.createQueue("q1"); 133 MessageConsumer consumer = subSession.createConsumer(queryConsumer); 134 consumer.setMessageListener(this); 135 136 stockProducer = pubSession.createTopic("SampleTopic1"); 138 publisher = (MessageProducer)pubSession.createProducer(stockProducer); 139 con.start(); 140 } catch (JMSException e) { 141 e.printStackTrace(); 142 } } 145 150 public void publish(String company,int newValue) { 151 try{ 152 javax.jms.TextMessage msg = pubSession.createTextMessage(); 154 msg.setText(company+StockPublisherExample.companyValueSeperator+newValue); 155 publisher.send( msg, 158 javax.jms.DeliveryMode.NON_PERSISTENT, 159 javax.jms.Message.DEFAULT_PRIORITY, 160 MESSAGE_TTL); 161 } 162 catch ( javax.jms.JMSException jmse ) 163 { 164 jmse.printStackTrace(); 165 } 166 } 168 169 172 public void onMessage(Message message) { 173 javax.jms.TextMessage textMessage = (javax.jms.TextMessage ) message; 175 String company=null; 176 try 178 { 179 company = textMessage.getText(); 180 }catch (javax.jms.JMSException jmse){ 181 jmse.printStackTrace(); 182 } 183 Integer value=(Integer )stocksMap.get(company); 185 int makeValue=0; 186 if (value==null){ 188 makeValue=(int)Math.round(Math.random()*10); 189 value = new Integer (makeValue); 190 stocksMap.put(company,value); 191 } 192 else { 194 makeValue=greaterValue(value.intValue()); 195 value = new Integer (makeValue); 196 stocksMap.put(company,value); 197 } 198 this.publish(company,makeValue); 199 } 201 private int greaterValue(int value){ 202 return value+(int)Math.round(Math.random()*5); 203 } 205 } | Popular Tags |