KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > jms > dii > JMSURLTest


1 package samples.jms.dii;
2
3 import org.apache.axis.AxisFault;
4 import org.apache.axis.client.Call;
5 import org.apache.axis.client.Service;
6 import org.apache.axis.configuration.XMLStringProvider;
7 import org.apache.axis.deployment.wsdd.WSDDConstants;
8 import org.apache.axis.encoding.XMLType;
9 import org.apache.axis.transport.jms.JMSConstants;
10 import org.apache.axis.transport.jms.JMSTransport;
11 import org.apache.axis.transport.jms.SimpleJMSListener;
12 import org.apache.axis.utils.Options;
13
14 import javax.xml.namespace.QName JavaDoc;
15 import javax.xml.rpc.ParameterMode JavaDoc;
16 import java.util.HashMap JavaDoc;
17
18 /**
19  * Demonstrates use of a JMS URL endpoint address to drive the JMS transport.
20  *
21  * The JMS listener is an intermediary that receives the JMS service request and
22  * invokes the actual stock quote service over HTTP.
23  *
24  * @author Ray Chun (rchun@sonicsoftware.com)
25  */

26
27 public class JMSURLTest {
28     static final String JavaDoc wsdd =
29             "<deployment xmlns=\"http://xml.apache.org/axis/wsdd/\" " +
30                   "xmlns:java=\"" + WSDDConstants.URI_WSDD_JAVA + "\">\n" +
31             " <transport name=\"JMSTransport\" pivot=\"java:org.apache.axis.transport.jms.JMSSender\"/>\n" +
32             " <service name=\"" + WSDDConstants.URI_WSDD + "\" provider=\"java:MSG\">\n" +
33             " <parameter name=\"allowedMethods\" value=\"AdminService\"/>\n" +
34             " <parameter name=\"className\" value=\"org.apache.axis.utils.Admin\"/>\n" +
35             " </service>\n" +
36             "</deployment>";
37
38     // the JMS URL target endpoint address
39
static String JavaDoc sampleJmsUrl = "jms:/MyQ?" +
40                                  "vendor=JNDI" +
41                                  "&java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory" +
42                                  "&java.naming.provider.url=file:///c:/JNDIStore" +
43                                  "&ConnectionFactoryJNDIName=MyCF" +
44                                  "&deliveryMode=persistent" +
45                                  "&priority=5" +
46                                  "&ttl=10000" +
47                                  "&debug=true";
48     /*
49     // example using Sonic
50     static String sampleJmsUrl = "jms:/SampleQ1?" +
51                                  "vendor=SonicMQ" +
52                                  "&brokerURL=localhost:2506" +
53                                  "&deliveryMode=persistent" +
54                                  "&priority=5" +
55                                  "&ttl=10000";
56     */

57
58     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
59         Options opts = new Options( args );
60
61         // first check if we should print usage
62
if ((opts.isFlagSet('?') > 0) || (opts.isFlagSet('h') > 0))
63             printUsage();
64
65         String JavaDoc username = opts.getUser();
66         String JavaDoc password = opts.getPassword();
67
68         HashMap JavaDoc connectorMap = SimpleJMSListener.createConnectorMap(opts);
69         HashMap JavaDoc cfMap = SimpleJMSListener.createCFMap(opts);
70         String JavaDoc destination = opts.isValueSet('d');
71
72         // create the jms listener
73
SimpleJMSListener listener = new SimpleJMSListener(connectorMap,
74                                                            cfMap,
75                                                            destination,
76                                                            username,
77                                                            password,
78                                                            false);
79         listener.start();
80
81         args = opts.getRemainingArgs();
82         if ( args == null || args.length == 0)
83             printUsage();
84
85         for (int i = 0; i < args.length; i++)
86         {
87             try
88             {
89                 Float JavaDoc res = getQuote(args[i], username, password);
90                 System.out.println(args[i] + ": " + res);
91             }
92             catch(AxisFault af)
93             {
94                 System.out.println(af.dumpToString());
95             }
96         }
97
98         // shutdown
99
listener.shutdown();
100
101         // close all JMSConnectors whose configuration matches that of the JMS URL
102
// note: this is optional, as all connectors will be closed upon exit
103
JMSTransport.closeMatchingJMSConnectors(sampleJmsUrl, username, password);
104
105         System.exit(1);
106     }
107
108     public static Float JavaDoc getQuote(String JavaDoc ticker, String JavaDoc username, String JavaDoc password)
109         throws javax.xml.rpc.ServiceException JavaDoc, AxisFault
110     {
111         Float JavaDoc res = new Float JavaDoc(-1.0);
112
113         Service service = new Service(new XMLStringProvider(wsdd));
114
115         // create a new Call object
116
Call call = (Call) service.createCall();
117         call.setOperationName( new QName JavaDoc("urn:xmltoday-delayed-quotes", "getQuote") );
118         call.addParameter( "symbol", XMLType.XSD_STRING, ParameterMode.IN );
119         call.setReturnType( XMLType.XSD_FLOAT );
120
121         try
122         {
123             java.net.URL JavaDoc jmsurl = new java.net.URL JavaDoc(sampleJmsUrl);
124             call.setTargetEndpointAddress(jmsurl);
125
126             // set additional params on the call if desired
127
call.setUsername(username);
128             call.setPassword(password);
129             call.setTimeout(new Integer JavaDoc(30000));
130
131             res = (Float JavaDoc) call.invoke(new Object JavaDoc[] {ticker});
132         }
133         catch (java.net.MalformedURLException JavaDoc e)
134         {
135             throw new AxisFault("Invalid JMS URL", e);
136         }
137         catch (java.rmi.RemoteException JavaDoc e)
138         {
139             throw new AxisFault("Failed in getQuote()", e);
140         }
141
142         return res;
143     }
144
145     public static void printUsage()
146     {
147         System.out.println("JMSURLTest: Tests JMS transport by obtaining stock quote");
148         System.out.println(" Usage: JMSURLTest <symbol 1> <symbol 2> <symbol 3> ...");
149         System.out.println(" Opts: -? this message");
150         System.out.println();
151         System.out.println(" -c connection factory properties filename");
152         System.out.println(" -d destination");
153         System.out.println(" -t topic [absence of -t indicates queue]");
154         System.out.println();
155         System.out.println(" -u username");
156         System.out.println(" -w password");
157         System.out.println();
158         System.out.println(" -s single-threaded listener");
159         System.out.println(" [absence of option => multithreaded]");
160
161         System.exit(1);
162     }
163 }
164
Popular Tags