KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > jms > StockExample > StockStatus


1 package sample.jms.StockExample;
2
3
4
5 /*
6  * Copyright 2002 by
7  * <a HREF="http://www.coridan.com">Coridan</a>
8  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
9  *
10  * The contents of this file are subject to the Mozilla Public License Version
11  * 1.1 (the "License"); you may not use this file except in compliance with the
12  * License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS" basis,
16  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17  * for the specific language governing rights and limitations under the
18  * License.
19  *
20  * The Original Code is "MantaRay" (TM).
21  *
22  * The Initial Developer of the Original Code is lital kasif.
23  * Portions created by the Initial Developer are Copyright (C) 2006
24  * Coridan Inc. All Rights Reserved.
25  *
26  * Contributor(s): all the names of the contributors are added in the source
27  * code where applicable.
28  *
29  * Alternatively, the contents of this file may be used under the terms of the
30  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
31  * provisions of LGPL are applicable instead of those above. If you wish to
32  * allow use of your version of this file only under the terms of the LGPL
33  * License and not to allow others to use your version of this file under
34  * the MPL, indicate your decision by deleting the provisions above and
35  * replace them with the notice and other provisions required by the LGPL.
36  * If you do not delete the provisions above, a recipient may use your version
37  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
38  
39  *
40  * This library is free software; you can redistribute it and/or modify it
41  * under the terms of the MPL as stated above or under the terms of the GNU
42  * Lesser General Public License as published by the Free Software Foundation;
43  * either version 2.1 of the License, or any later version.
44  *
45  * This library is distributed in the hope that it will be useful, but WITHOUT
46  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
47  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
48  * License for more details.
49  *
50  * Created on Dec 30, 2004
51  *
52  * Coridan LTD
53  */

54 import java.util.HashMap JavaDoc;
55 import java.util.Iterator JavaDoc;
56
57 import javax.jms.*;
58
59 /**
60  * 'StockStatus' is part of the StockPublisherExample. It is a listener to the query queue that makes quereis with
61  * companies names, and as a result publish these companies stocks values in the stock topic.
62  * A company that was queried once would get fluent value updates published a the topic every 3 seconds.
63  *
64  * @author lital kasif
65  *
66  */

67 public class StockStatus extends Thread JavaDoc implements javax.jms.MessageListener JavaDoc {
68     private static final int MESSAGE_TTL = 6000000;
69
70     private javax.jms.Connection JavaDoc con = null;
71     private javax.jms.Session JavaDoc pubSession = null;
72     private javax.jms.Session JavaDoc subSession = null;
73     private javax.jms.MessageProducer JavaDoc publisher = null;
74     private javax.jms.Queue JavaDoc queryConsumer=null;
75     private javax.jms.Topic JavaDoc stockProducer=null;
76     //holds the current stock values of all the companies that were queried at the query queue
77
private HashMap JavaDoc stocksMap=null;
78
79     //constractor
80
public StockStatus(){
81         super();
82         stocksMap=new HashMap JavaDoc();
83     }
84     
85     /**
86      * This Thread runs through the stocks map once every three seconds, greater all the companies' stock values
87      * and publish on the stock topic the new updated values.
88      */

89     public void run(){
90         //publish new stock values for all known companies every three seconds
91
while(true){
92             Iterator JavaDoc companiesIterator = stocksMap.keySet().iterator();
93             //runs over all known companies in the stocks map
94
while(companiesIterator.hasNext()){
95                 String JavaDoc company = (String JavaDoc)companiesIterator.next();
96                 Integer JavaDoc value=(Integer JavaDoc)stocksMap.get(company);
97                 int makeValue=0;
98                 makeValue=greaterValue(value.intValue());
99                 value = new Integer JavaDoc(makeValue);
100                 stocksMap.put(company,value);
101                 //publish the new value of the stock in the stock topic
102
publish(company,makeValue);
103             }//while
104
try {
105                 Thread.sleep(3000);
106             } catch (InterruptedException JavaDoc e) {
107                 e.printStackTrace();
108             }
109         }//while(true)
110
}//run
111

112     /**
113      * creates a consumer for the 'stock query' queue and a publisher to the 'stock status' topic
114      */

115     public void createDestinations(){
116         // Create a connection and two ways sessions.
117
try
118         {
119             //Set up the JMS objects needed
120
con = StockPublisherExample.conFactory.createConnection();
121             //Both sessions are not transacted
122
pubSession = con.createSession(false,Session.AUTO_ACKNOWLEDGE);
123             subSession = con.createSession(false,Session.AUTO_ACKNOWLEDGE);
124         }
125         catch (javax.jms.JMSException JavaDoc jmse)
126         {
127             jmse.printStackTrace();
128             System.exit(1);
129         }
130         try {
131             //create a consumer to the 'query stocks' queue
132
queryConsumer = subSession.createQueue("q1");
133             MessageConsumer consumer = subSession.createConsumer(queryConsumer);
134             consumer.setMessageListener(this);
135             
136             //create a producer to the 'stocks status' topic
137
stockProducer = pubSession.createTopic("SampleTopic1");
138             publisher = (MessageProducer)pubSession.createProducer(stockProducer);
139             con.start();
140         } catch (JMSException e) {
141             e.printStackTrace();
142         }//catch
143
}//createDestinations()
144

145     /**
146      * publishes new stock values on the stock topic
147      * @param company the companies name
148      * @param newValue the new stock value
149      */

150     public void publish(String JavaDoc company,int newValue) {
151         try{
152             //publish a message with the company's current stock value
153
javax.jms.TextMessage JavaDoc msg = pubSession.createTextMessage();
154             msg.setText(company+StockPublisherExample.companyValueSeperator+newValue);
155             // for PERSISTENT see ReliableTalk (hint: use PERSISTENT flag)
156
// Hold messages for MESSAGE_TTL millisecs.
157
publisher.send( msg,
158                    javax.jms.DeliveryMode.NON_PERSISTENT,
159                    javax.jms.Message.DEFAULT_PRIORITY,
160                    MESSAGE_TTL);
161         }
162         catch ( javax.jms.JMSException JavaDoc jmse )
163         {
164             jmse.printStackTrace();
165         }
166     }//sendQuery
167

168     
169     /* (non-Javadoc)
170      * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
171      */

172     public void onMessage(Message message) {
173         //Cast the message as a text message
174
javax.jms.TextMessage JavaDoc textMessage = (javax.jms.TextMessage JavaDoc) message;
175         String JavaDoc company=null;
176         //gets the text out of the message
177
try
178         {
179             company = textMessage.getText();
180         }catch (javax.jms.JMSException JavaDoc jmse){
181                 jmse.printStackTrace();
182         }
183         //get the existing value of the company from the stocks map
184
Integer JavaDoc value=(Integer JavaDoc)stocksMap.get(company);
185         int makeValue=0;
186         //if the company does not have a value yet, initiate the value to be a low value
187
if (value==null){
188             makeValue=(int)Math.round(Math.random()*10);
189             value = new Integer JavaDoc(makeValue);
190             stocksMap.put(company,value);
191         }
192         //if the company already has a value, greater it
193
else {
194             makeValue=greaterValue(value.intValue());
195             value = new Integer JavaDoc(makeValue);
196             stocksMap.put(company,value);
197         }
198         this.publish(company,makeValue);
199     }//onMessage
200

201     private int greaterValue(int value){
202         return value+(int)Math.round(Math.random()*5);
203     }//greaterValue
204

205 }//StockStatus
206
Popular Tags