001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): ____________________________________.
022: * Contributor(s): ______________________________________.
023: *
024: * --------------------------------------------------------------------------
025: * $Id: SampleAppliClient.java 9483 2006-08-31 13:54:46Z coqp $
026: * --------------------------------------------------------------------------
027: */package sampleappli;
028:
029: import java.rmi.RemoteException;
030: import java.util.Enumeration;
031: import java.io.DataInputStream;
032: import java.io.BufferedInputStream;
033: import java.io.File;
034: import java.io.FileInputStream;
035: import java.io.BufferedReader;
036: import java.io.InputStreamReader;
037: import javax.jms.*;
038: import javax.naming.Context;
039: import javax.naming.InitialContext;
040: import javax.naming.NamingException;
041: import javax.rmi.PortableRemoteObject;
042:
043: /**
044: *
045: */
046: public class SampleAppliClient {
047:
048: static Context initialContext = null;
049:
050: static EnvHome home = null;
051:
052: // Number of second to wait before the checking process
053: static int sleepSecond = 10;
054:
055: public static void main(String[] args) {
056:
057: // Initialize the number of second to wait before the checking process
058: if (args.length > 0) {
059: sleepSecond = (new Integer(args[0])).intValue();
060: }
061:
062: // Get InitialContext
063: try {
064: initialContext = new InitialContext();
065: } catch (NamingException e) {
066: e.printStackTrace();
067: System.exit(2);
068: }
069:
070: // Lookup bean home
071: String beanName = "EnvHome";
072: try {
073: home = (EnvHome) PortableRemoteObject.narrow(initialContext
074: .lookup(beanName), EnvHome.class);
075: } catch (Exception e) {
076: e.printStackTrace();
077: System.exit(2);
078: }
079:
080: // Init the environment for the application
081: Env env = null;
082: try {
083: env = home.create();
084: env.init();
085: } catch (Exception ex) {
086: System.err.println("Cannot init Environment: " + ex);
087: System.exit(2);
088: }
089:
090: TopicConnectionFactory tcf = null;
091: TopicPublisher tp = null;
092: Topic topic = null;
093: // JNDI name of the Topics
094: String topicName = "StockHandlerTopic";
095: // JNDI name of the connection factory
096: String conFactName = "JTCF";
097:
098: try {
099: // lookup the TopicConnectionFactory through its JNDI name
100: tcf = (TopicConnectionFactory) initialContext
101: .lookup(conFactName);
102: // lookup the Topic through its JNDI name
103: topic = (Topic) initialContext.lookup(topicName);
104: } catch (NamingException e) {
105: e.printStackTrace();
106: System.exit(2);
107: }
108:
109: TopicConnection tc = null;
110: TopicSession session = null;
111: try {
112: tc = tcf.createTopicConnection();
113: session = tc.createTopicSession(false,
114: Session.AUTO_ACKNOWLEDGE);
115: tp = session.createPublisher(topic);
116: } catch (Exception e) {
117: e.printStackTrace();
118: System.exit(2);
119: }
120:
121: MapMessage mess = null;
122: int nbmess = 0;
123: // We send several messages that will update the database and the file
124: // order
125: try {
126: mess = session.createMapMessage();
127: mess.setString("CustomerId", "customer10");
128: mess.setString("ProductId", "00003");
129: mess.setInt("Quantity", 3);
130: tp.publish(mess);
131: nbmess++;
132:
133: mess = session.createMapMessage();
134: mess.setString("CustomerId", "customer1");
135: mess.setString("ProductId", "00001");
136: mess.setInt("Quantity", 5);
137: tp.publish(mess);
138: nbmess++;
139:
140: mess = session.createMapMessage();
141: mess.setString("CustomerId", "customer3");
142: mess.setString("ProductId", "00002");
143: mess.setInt("Quantity", 2);
144: tp.publish(mess);
145: nbmess++;
146:
147: mess = session.createMapMessage();
148: mess.setString("CustomerId", "customer2");
149: mess.setString("ProductId", "00004");
150: mess.setInt("Quantity", 6);
151: tp.publish(mess);
152: nbmess++;
153:
154: mess = session.createMapMessage();
155: mess.setString("CustomerId", "customer2");
156: mess.setString("ProductId", "00003");
157: mess.setInt("Quantity", 10);
158: tp.publish(mess);
159: nbmess++;
160:
161: mess = session.createMapMessage();
162: mess.setString("CustomerId", "customer3");
163: mess.setString("ProductId", "00001");
164: mess.setInt("Quantity", 10);
165: tp.publish(mess);
166: nbmess++;
167:
168: mess = session.createMapMessage();
169: mess.setString("CustomerId", "customer1");
170: mess.setString("ProductId", "00002");
171: mess.setInt("Quantity", 5);
172: tp.publish(mess);
173: nbmess++;
174:
175: mess = session.createMapMessage();
176: mess.setString("CustomerId", "customer10");
177: mess.setString("ProductId", "00004");
178: mess.setInt("Quantity", 3);
179: tp.publish(mess);
180: nbmess++;
181:
182: mess = session.createMapMessage();
183: mess.setString("CustomerId", "customer122");
184: mess.setString("ProductId", "00003");
185: mess.setInt("Quantity", 6);
186: tp.publish(mess);
187: nbmess++;
188:
189: mess = session.createMapMessage();
190: mess.setString("CustomerId", "customer45");
191: mess.setString("ProductId", "00001");
192: mess.setInt("Quantity", 4);
193: tp.publish(mess);
194: nbmess++;
195:
196: mess = session.createMapMessage();
197: mess.setString("CustomerId", "customer7");
198: mess.setString("ProductId", "00002");
199: mess.setInt("Quantity", 2);
200: tp.publish(mess);
201: nbmess++;
202:
203: // Here we send a message that will force the rollback of the
204: // transaction
205: // in the onMessage of StockHandler MessageDrivenBean
206: mess = session.createMapMessage();
207: mess.setString("CustomerId", "customer00");
208: mess.setString("ProductId", "00000");
209: mess.setInt("Quantity", 1000);
210: tp.publish(mess);
211: session.close();
212: tc.close();
213: } catch (Exception ex) {
214: System.err
215: .println("Exception caught when sending messages: "
216: + ex);
217: System.exit(2);
218: }
219:
220: // Before the checking process we wait a little ....
221: try {
222: Thread.currentThread().sleep(1000 * sleepSecond);
223: } catch (InterruptedException e) {
224: System.err.println("InterruptedException");
225: System.exit(2);
226: }
227:
228: // We have to check that nbmess have actually sent to the Message driven
229: // bean Order
230: // this is done by reading the order file (order.txt), it must have
231: // nbmess lines
232: BufferedReader inorderbr = null;
233: try {
234: File f = new File(System.getProperty("java.io.tmpdir")
235: + File.separator + System.getProperty("user.name")
236: + "_order.txt");
237: DataInputStream inorder = new DataInputStream(
238: new BufferedInputStream(new FileInputStream(f)));
239: inorderbr = new BufferedReader(new InputStreamReader(
240: inorder));
241: } catch (Exception ex) {
242: System.err.println("Cannot open the order file: " + ex);
243: System.exit(2);
244: }
245:
246: String s = null;
247: int n = 0;
248: try {
249: while ((s = inorderbr.readLine()) != null) {
250: n++;
251: System.out.println(s);
252: }
253: inorderbr.close();
254: } catch (java.io.IOException ex) {
255: System.err.println("cannot read the order file: " + ex);
256: }
257: if (n == nbmess) {
258: System.out.println("Nb messages sent and received OK");
259: } else {
260: System.out.println("Problem: nb messages sent = " + nbmess
261: + ", nb messages received = " + n);
262: System.out
263: .println(" (re-run SampleAppliClent with an argument greater than "
264: + sleepSecond + ")");
265: }
266:
267: // Lookup bean home
268: String stockbeanName = "StockHome";
269: StockHome shome = null;
270: Enumeration listOfStocks = null;
271: try {
272: shome = (StockHome) PortableRemoteObject.narrow(
273: initialContext.lookup(stockbeanName),
274: StockHome.class);
275: } catch (Exception e) {
276: e.printStackTrace();
277: System.exit(2);
278: }
279:
280: try {
281: listOfStocks = shome.findAllStocks();
282: } catch (Exception e) {
283: System.err.println("Cannot findAllStocks: " + e);
284: }
285: Stock stock = null;
286: try {
287: while (listOfStocks.hasMoreElements()) {
288: stock = (Stock) listOfStocks.nextElement();
289: String id = stock.getId();
290: int qty = stock.getQuantity();
291: System.out.println("StockId = " + id + " Quantity = "
292: + qty);
293: if (id.equals("00000")) {
294: if (qty != 10) {
295: System.err
296: .println("Problem: Stock id 00000 must be set to 10");
297: System.out
298: .println(" (re-run SampleAppliClent with an argument greater than "
299: + sleepSecond + ")");
300: System.exit(2);
301: }
302: } else {
303: // for the other id the stock quantity must be equals to 1
304: if (qty != 1) {
305: System.err.println("Problem: Stock id " + id
306: + " must be set to 1");
307: System.out
308: .println(" (re-run SampleAppliClent with an argument greater than "
309: + sleepSecond + ")");
310: System.exit(2);
311: }
312: }
313: }
314: } catch (Exception ex) {
315: System.err
316: .println("Exception caught while reading stocks: "
317: + ex);
318: }
319:
320: System.out.println("SampleApplicationClient OK");
321:
322: }
323: }
|