001: package samples.jms.dii;
002:
003: import org.apache.axis.AxisFault;
004: import org.apache.axis.client.Call;
005: import org.apache.axis.client.Service;
006: import org.apache.axis.configuration.XMLStringProvider;
007: import org.apache.axis.deployment.wsdd.WSDDConstants;
008: import org.apache.axis.encoding.XMLType;
009: import org.apache.axis.transport.jms.JMSConstants;
010: import org.apache.axis.transport.jms.JMSTransport;
011: import org.apache.axis.transport.jms.SimpleJMSListener;
012: import org.apache.axis.utils.Options;
013:
014: import javax.xml.namespace.QName;
015: import javax.xml.rpc.ParameterMode;
016: import java.util.HashMap;
017:
018: /**
019: * Demonstrates use of a JMS URL endpoint address to drive the JMS transport.
020: *
021: * The JMS listener is an intermediary that receives the JMS service request and
022: * invokes the actual stock quote service over HTTP.
023: *
024: * @author Ray Chun (rchun@sonicsoftware.com)
025: */
026:
027: public class JMSURLTest {
028: static final String wsdd = "<deployment xmlns=\"http://xml.apache.org/axis/wsdd/\" "
029: + "xmlns:java=\""
030: + WSDDConstants.URI_WSDD_JAVA
031: + "\">\n"
032: + " <transport name=\"JMSTransport\" pivot=\"java:org.apache.axis.transport.jms.JMSSender\"/>\n"
033: + " <service name=\""
034: + WSDDConstants.URI_WSDD
035: + "\" provider=\"java:MSG\">\n"
036: + " <parameter name=\"allowedMethods\" value=\"AdminService\"/>\n"
037: + " <parameter name=\"className\" value=\"org.apache.axis.utils.Admin\"/>\n"
038: + " </service>\n" + "</deployment>";
039:
040: // the JMS URL target endpoint address
041: static String sampleJmsUrl = "jms:/MyQ?"
042: + "vendor=JNDI"
043: + "&java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory"
044: + "&java.naming.provider.url=file:///c:/JNDIStore"
045: + "&ConnectionFactoryJNDIName=MyCF"
046: + "&deliveryMode=persistent" + "&priority=5" + "&ttl=10000"
047: + "&debug=true";
048:
049: /*
050: // example using Sonic
051: static String sampleJmsUrl = "jms:/SampleQ1?" +
052: "vendor=SonicMQ" +
053: "&brokerURL=localhost:2506" +
054: "&deliveryMode=persistent" +
055: "&priority=5" +
056: "&ttl=10000";
057: */
058:
059: public static void main(String args[]) throws Exception {
060: Options opts = new Options(args);
061:
062: // first check if we should print usage
063: if ((opts.isFlagSet('?') > 0) || (opts.isFlagSet('h') > 0))
064: printUsage();
065:
066: String username = opts.getUser();
067: String password = opts.getPassword();
068:
069: HashMap connectorMap = SimpleJMSListener
070: .createConnectorMap(opts);
071: HashMap cfMap = SimpleJMSListener.createCFMap(opts);
072: String destination = opts.isValueSet('d');
073:
074: // create the jms listener
075: SimpleJMSListener listener = new SimpleJMSListener(
076: connectorMap, cfMap, destination, username, password,
077: false);
078: listener.start();
079:
080: args = opts.getRemainingArgs();
081: if (args == null || args.length == 0)
082: printUsage();
083:
084: for (int i = 0; i < args.length; i++) {
085: try {
086: Float res = getQuote(args[i], username, password);
087: System.out.println(args[i] + ": " + res);
088: } catch (AxisFault af) {
089: System.out.println(af.dumpToString());
090: }
091: }
092:
093: // shutdown
094: listener.shutdown();
095:
096: // close all JMSConnectors whose configuration matches that of the JMS URL
097: // note: this is optional, as all connectors will be closed upon exit
098: JMSTransport.closeMatchingJMSConnectors(sampleJmsUrl, username,
099: password);
100:
101: System.exit(1);
102: }
103:
104: public static Float getQuote(String ticker, String username,
105: String password) throws javax.xml.rpc.ServiceException,
106: AxisFault {
107: Float res = new Float(-1.0);
108:
109: Service service = new Service(new XMLStringProvider(wsdd));
110:
111: // create a new Call object
112: Call call = (Call) service.createCall();
113: call.setOperationName(new QName("urn:xmltoday-delayed-quotes",
114: "getQuote"));
115: call.addParameter("symbol", XMLType.XSD_STRING,
116: ParameterMode.IN);
117: call.setReturnType(XMLType.XSD_FLOAT);
118:
119: try {
120: java.net.URL jmsurl = new java.net.URL(sampleJmsUrl);
121: call.setTargetEndpointAddress(jmsurl);
122:
123: // set additional params on the call if desired
124: call.setUsername(username);
125: call.setPassword(password);
126: call.setTimeout(new Integer(30000));
127:
128: res = (Float) call.invoke(new Object[] { ticker });
129: } catch (java.net.MalformedURLException e) {
130: throw new AxisFault("Invalid JMS URL", e);
131: } catch (java.rmi.RemoteException e) {
132: throw new AxisFault("Failed in getQuote()", e);
133: }
134:
135: return res;
136: }
137:
138: public static void printUsage() {
139: System.out
140: .println("JMSURLTest: Tests JMS transport by obtaining stock quote");
141: System.out
142: .println(" Usage: JMSURLTest <symbol 1> <symbol 2> <symbol 3> ...");
143: System.out.println(" Opts: -? this message");
144: System.out.println();
145: System.out
146: .println(" -c connection factory properties filename");
147: System.out.println(" -d destination");
148: System.out
149: .println(" -t topic [absence of -t indicates queue]");
150: System.out.println();
151: System.out.println(" -u username");
152: System.out.println(" -w password");
153: System.out.println();
154: System.out.println(" -s single-threaded listener");
155: System.out
156: .println(" [absence of option => multithreaded]");
157:
158: System.exit(1);
159: }
160: }
|