KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sampleappli > SampleAppliClient


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): ____________________________________.
22  * Contributor(s): ______________________________________.
23  *
24  * --------------------------------------------------------------------------
25  * $Id: SampleAppliClient.java,v 1.8 2004/04/19 06:39:30 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 // SampleAppliClient.java
30
//
31

32 package sampleappli;
33
34 import java.rmi.RemoteException;
35 import java.util.Enumeration;
36 import java.io.DataInputStream;
37 import java.io.BufferedInputStream;
38 import java.io.File;
39 import java.io.FileInputStream;
40 import java.io.BufferedReader;
41 import java.io.InputStreamReader;
42 import javax.jms.*;
43 import javax.naming.Context;
44 import javax.naming.InitialContext;
45 import javax.naming.NamingException;
46 import javax.rmi.PortableRemoteObject;
47
48 /**
49  *
50  */

51 public class SampleAppliClient {
52
53     static Context initialContext = null;
54
55     static EnvHome home = null;
56
57     // Number of second to wait before the checking process
58
static int sleepSecond = 10;
59
60     public static void main(String[] args) {
61
62         // Initialize the number of second to wait before the checking process
63
if (args.length > 0) {
64             sleepSecond = (new Integer(args[0])).intValue();
65         }
66
67         // Get InitialContext
68
try {
69             initialContext = new InitialContext();
70         } catch (NamingException e) {
71             e.printStackTrace();
72             System.exit(2);
73         }
74
75         // Lookup bean home
76
String beanName = "EnvHome";
77         try {
78             home = (EnvHome) PortableRemoteObject.narrow(initialContext.lookup(beanName), EnvHome.class);
79         } catch (Exception e) {
80             e.printStackTrace();
81             System.exit(2);
82         }
83
84         // Init the environment for the application
85
Env env = null;
86         try {
87             env = home.create();
88             env.init();
89         } catch (Exception ex) {
90             System.err.println("Cannot init Environment: " + ex);
91             System.exit(2);
92         }
93
94         TopicConnectionFactory tcf = null;
95         TopicPublisher tp = null;
96         Topic topic = null;
97         // JNDI name of the Topics
98
String topicName = "StockHandlerTopic";
99         // JNDI name of the connection factory
100
String conFactName = "JTCF";
101
102         try {
103             // lookup the TopicConnectionFactory through its JNDI name
104
tcf = (TopicConnectionFactory) initialContext.lookup(conFactName);
105             // lookup the Topic through its JNDI name
106
topic = (Topic) initialContext.lookup(topicName);
107         } catch (NamingException e) {
108             e.printStackTrace();
109             System.exit(2);
110         }
111
112         TopicConnection tc = null;
113         TopicSession session = null;
114         try {
115             tc = tcf.createTopicConnection();
116             session = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
117             tp = session.createPublisher(topic);
118         } catch (Exception e) {
119             e.printStackTrace();
120             System.exit(2);
121         }
122
123         MapMessage mess = null;
124         int nbmess = 0;
125         // We send several messages that will update the database and the file
126
// order
127
try {
128             mess = session.createMapMessage();
129             mess.setString("CustomerId", "customer10");
130             mess.setString("ProductId", "00003");
131             mess.setInt("Quantity", 3);
132             tp.publish(mess);
133             nbmess++;
134
135             mess = session.createMapMessage();
136             mess.setString("CustomerId", "customer1");
137             mess.setString("ProductId", "00001");
138             mess.setInt("Quantity", 5);
139             tp.publish(mess);
140             nbmess++;
141
142             mess = session.createMapMessage();
143             mess.setString("CustomerId", "customer3");
144             mess.setString("ProductId", "00002");
145             mess.setInt("Quantity", 2);
146             tp.publish(mess);
147             nbmess++;
148
149             mess = session.createMapMessage();
150             mess.setString("CustomerId", "customer2");
151             mess.setString("ProductId", "00004");
152             mess.setInt("Quantity", 6);
153             tp.publish(mess);
154             nbmess++;
155
156             mess = session.createMapMessage();
157             mess.setString("CustomerId", "customer2");
158             mess.setString("ProductId", "00003");
159             mess.setInt("Quantity", 10);
160             tp.publish(mess);
161             nbmess++;
162
163             mess = session.createMapMessage();
164             mess.setString("CustomerId", "customer3");
165             mess.setString("ProductId", "00001");
166             mess.setInt("Quantity", 10);
167             tp.publish(mess);
168             nbmess++;
169
170             mess = session.createMapMessage();
171             mess.setString("CustomerId", "customer1");
172             mess.setString("ProductId", "00002");
173             mess.setInt("Quantity", 5);
174             tp.publish(mess);
175             nbmess++;
176
177             mess = session.createMapMessage();
178             mess.setString("CustomerId", "customer10");
179             mess.setString("ProductId", "00004");
180             mess.setInt("Quantity", 3);
181             tp.publish(mess);
182             nbmess++;
183
184             mess = session.createMapMessage();
185             mess.setString("CustomerId", "customer122");
186             mess.setString("ProductId", "00003");
187             mess.setInt("Quantity", 6);
188             tp.publish(mess);
189             nbmess++;
190
191             mess = session.createMapMessage();
192             mess.setString("CustomerId", "customer45");
193             mess.setString("ProductId", "00001");
194             mess.setInt("Quantity", 4);
195             tp.publish(mess);
196             nbmess++;
197
198             mess = session.createMapMessage();
199             mess.setString("CustomerId", "customer7");
200             mess.setString("ProductId", "00002");
201             mess.setInt("Quantity", 2);
202             tp.publish(mess);
203             nbmess++;
204
205             // Here we send a message that will force the rollback of the
206
// transaction
207
// in the onMessage of StockHandler MessageDrivenBean
208
mess = session.createMapMessage();
209             mess.setString("CustomerId", "customer00");
210             mess.setString("ProductId", "00000");
211             mess.setInt("Quantity", 1000);
212             tp.publish(mess);
213
214         } catch (Exception ex) {
215             System.err.println("Exception caught when sending messages: " + ex);
216             System.exit(2);
217         }
218
219         // Before the checking process we wait a little ....
220
try {
221             Thread.currentThread().sleep(1000 * sleepSecond);
222         } catch (InterruptedException e) {
223             System.err.println("InterruptedException");
224             System.exit(2);
225         }
226
227         // We have to check that nbmess have actually sent to the Message driven
228
// bean Order
229
// this is done by reading the order file (order.txt), it must have
230
// nbmess lines
231
BufferedReader inorderbr = null;
232         try {
233             File f = new File(System.getProperty("java.io.tmpdir") + File.separator + System.getProperty("user.name")
234                     + "_order.txt");
235             DataInputStream inorder = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));
236             inorderbr = new BufferedReader(new InputStreamReader(inorder));
237         } catch (Exception ex) {
238             System.err.println("Cannot open the order file: " + ex);
239             System.exit(2);
240         }
241
242         String s = null;
243         int n = 0;
244         try {
245             while ((s = inorderbr.readLine()) != null) {
246                 n++;
247                 System.out.println(s);
248             }
249             inorderbr.close();
250         } catch (java.io.IOException ex) {
251             System.err.println("cannot read the order file: " + ex);
252         }
253         if (n == nbmess) {
254             System.out.println("Nb messages sent and received OK");
255         } else {
256             System.out.println("Problem: nb messages sent = " + nbmess + ", nb messages received = " + n);
257             System.out.println(" (re-run SampleAppliClent with an argument greater than " + sleepSecond + ")");
258         }
259
260         // Lookup bean home
261
String stockbeanName = "StockHome";
262         StockHome shome = null;
263         Enumeration listOfStocks = null;
264         try {
265             shome = (StockHome) PortableRemoteObject.narrow(initialContext.lookup(stockbeanName), StockHome.class);
266         } catch (Exception e) {
267             e.printStackTrace();
268             System.exit(2);
269         }
270
271         try {
272             listOfStocks = shome.findAllStocks();
273         } catch (Exception e) {
274             System.err.println("Cannot findAllStocks: " + e);
275         }
276         Stock stock = null;
277         try {
278             while (listOfStocks.hasMoreElements()) {
279                 stock = (Stock) listOfStocks.nextElement();
280                 String id = stock.getId();
281                 int qty = stock.getQuantity();
282                 System.out.println("StockId = " + id + " Quantity = " + qty);
283                 if (id.equals("00000")) {
284                     if (qty != 10) {
285                         System.err.println("Problem: Stock id 00000 must be set to 10");
286                         System.out.println(" (re-run SampleAppliClent with an argument greater than "
287                                 + sleepSecond + ")");
288                         System.exit(2);
289                     }
290                 } else {
291                     // for the other id the stock quantity must be equals to 1
292
if (qty != 1) {
293                         System.err.println("Problem: Stock id " + id + " must be set to 1");
294                         System.out.println(" (re-run SampleAppliClent with an argument greater than "
295                                 + sleepSecond + ")");
296                         System.exit(2);
297                     }
298                 }
299             }
300         } catch (Exception ex) {
301             System.err.println("Exception caught while reading stocks: " + ex);
302         }
303
304         System.out.println("SampleApplicationClient OK");
305
306     }
307 }
Popular Tags