KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JMSClient


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
18
19 import org.apache.activemq.ActiveMQConnectionFactory;
20 import org.apache.activemq.command.ActiveMQQueue;
21 import org.apache.geronimo.connector.work.GeronimoWorkManager;
22 import org.apache.geronimo.transaction.ExtendedTransactionManager;
23 import org.apache.geronimo.transaction.context.TransactionContextManager;
24 import org.jencks.factory.TransactionContextManagerFactoryBean;
25 import org.jencks.factory.TransactionManagerFactoryBean;
26 import org.jencks.factory.WorkManagerFactoryBean;
27 import org.logicblaze.lingo.jms.Requestor;
28 import org.logicblaze.lingo.jms.impl.MultiplexingRequestor;
29
30 import javax.jms.ConnectionFactory JavaDoc;
31 import javax.jms.Destination JavaDoc;
32 import javax.jms.Message JavaDoc;
33 import javax.resource.spi.work.Work JavaDoc;
34
35 /**
36  * @version $Revision: 442121 $
37  */

38 public class JMSClient implements Work JavaDoc {
39     
40     private static ConnectionFactory factory;
41     private static CountDownLatch latch;
42     private static Requestor requestor;
43     
44     /**
45      * main ...
46      *
47      * @param args
48      * @throws Exception
49      */

50     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
51         System.out.println("Connecting to JMS server.");
52         factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
53         Destination JavaDoc inQueue = new ActiveMQQueue("demo.org.servicemix.source");
54         Destination JavaDoc outQueue = new ActiveMQQueue("demo.org.servicemix.output");
55         requestor = MultiplexingRequestor.newInstance(factory, inQueue, outQueue);
56         
57         if (args.length == 0) {
58             new JMSClient().run();
59         } else {
60             int nb = Integer.parseInt(args[0]);
61             int th = 30;
62             if (args.length > 1) {
63                 th = Integer.parseInt(args[1]);
64             }
65             GeronimoWorkManager wm = createWorkManager(th);
66             latch = new CountDownLatch(nb);
67             for (int i = 0; i < nb; i++) {
68                 wm.scheduleWork(new JMSClient());
69             }
70             latch.await();
71             wm.doStop();
72         }
73         System.out.println("Closing.");
74         requestor.close();
75     }
76     
77     protected static GeronimoWorkManager createWorkManager(int poolSize) throws Exception JavaDoc {
78         TransactionManagerFactoryBean tmfb = new TransactionManagerFactoryBean();
79         tmfb.afterPropertiesSet();
80         TransactionContextManagerFactoryBean tcmfb = new TransactionContextManagerFactoryBean();
81         tcmfb.setTransactionManager((ExtendedTransactionManager) tmfb.getObject());
82         tcmfb.afterPropertiesSet();
83         WorkManagerFactoryBean wmfb = new WorkManagerFactoryBean();
84         wmfb.setTransactionContextManager((TransactionContextManager) tcmfb.getObject());
85         wmfb.setThreadPoolSize(poolSize);
86         wmfb.afterPropertiesSet();
87         GeronimoWorkManager wm = wmfb.getWorkManager();
88         return wm;
89     }
90
91     public void run() {
92         try {
93             System.out.println("Sending request.");
94             Message JavaDoc out = requestor.getSession().createMapMessage();
95             out.setStringProperty("ssn", "012-24532-53254");
96             out.setDoubleProperty("amount", Math.random() * 100000);
97             out.setIntProperty("duration", (int) Math.random() * 48);
98             Message JavaDoc in = requestor.request(null, out);
99             if (in == null) {
100                 System.out.println("Response timed out.");
101             }
102             else {
103                 System.out.println("Response was: " + in.getDoubleProperty("rate") + " from " + in.getStringProperty("bank"));
104             }
105         } catch (Exception JavaDoc e) {
106             e.printStackTrace();
107         } finally {
108             if (latch != null) {
109                 latch.countDown();
110             }
111         }
112     }
113
114     public void release() {
115     }
116 }
117
Popular Tags