| 1 |
/* |
| 2 |
* Copyright 2001,2004 The Apache Software Foundation. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
package samples.jms.stub; |
| 17 |
|
| 18 |
import samples.jms.stub.xmltoday_delayed_quotes.*; |
| 19 |
import org.apache.axis.AxisFault; |
| 20 |
import org.apache.axis.utils.Options; |
| 21 |
import org.apache.axis.transport.jms.JMSTransport; |
| 22 |
import org.apache.axis.transport.jms.SimpleJMSListener; |
| 23 |
import java.util.HashMap; |
| 24 |
|
| 25 |
import junit.framework.AssertionFailedError; |
| 26 |
import junit.framework.TestCase; |
| 27 |
|
| 28 |
import java.rmi.RemoteException; |
| 29 |
|
| 30 |
import javax.xml.rpc.ServiceException; |
| 31 |
import javax.xml.rpc.Stub; |
| 32 |
|
| 33 |
/** |
| 34 |
* Demonstrates use of wsdl2java-generated static stubs to invoke JMS endpoints. |
| 35 |
* |
| 36 |
* The JMS listener is an intermediary that receives the JMS service request and |
| 37 |
* invokes the actual stock quote service over HTTP. |
| 38 |
* |
| 39 |
* @author Ray Chun ([email protected]) |
| 40 |
*/ |
| 41 |
|
| 42 |
public class JMSURLStubTest extends TestCase { |
| 43 |
public JMSURLStubTest(String name) { |
| 44 |
super(name); |
| 45 |
} |
| 46 |
|
| 47 |
public static Float getQuote(String ticker) throws AxisFault { |
| 48 |
float quote = -1.0F; |
| 49 |
|
| 50 |
GetQuoteServiceLocator locator = new GetQuoteServiceLocator(); |
| 51 |
GetQuote getQuote; |
| 52 |
|
| 53 |
try { |
| 54 |
getQuote = locator.getGetQuote(); |
| 55 |
} |
| 56 |
catch (ServiceException e) { |
| 57 |
throw new AxisFault("JAX-RPC ServiceException caught: ", e); |
| 58 |
} |
| 59 |
assertTrue("getQuote is null", getQuote != null); |
| 60 |
|
| 61 |
try { |
| 62 |
quote = getQuote.getQuote(ticker); |
| 63 |
System.out.println("quote: " + quote); |
| 64 |
|
| 65 |
// close matching connectors |
| 66 |
// note: this is optional, as all connectors will be closed upon exit |
| 67 |
String endptAddr = locator.getGetQuoteAddress(); |
| 68 |
JMSTransport.closeMatchingJMSConnectors(endptAddr, null, null); |
| 69 |
} |
| 70 |
catch (RemoteException e) { |
| 71 |
throw new AxisFault("Remote Exception caught: ", e); |
| 72 |
} |
| 73 |
return new Float(quote); |
| 74 |
} |
| 75 |
|
| 76 |
public static void printUsage() |
| 77 |
{ |
| 78 |
System.out.println("JMSURLStubTest: Tests JMS transport by obtaining stock quote using wsdl2java-generated stub classes"); |
| 79 |
System.out.println(" Usage: JMSURLStubTest <symbol 1> <symbol 2> <symbol 3> ..."); |
| 80 |
System.out.println(" Opts: -? this message"); |
| 81 |
System.out.println(); |
| 82 |
System.out.println(" -c connection factory properties filename"); |
| 83 |
System.out.println(" -d destination"); |
| 84 |
System.out.println(" -t topic [absence of -t indicates queue]"); |
| 85 |
System.out.println(); |
| 86 |
System.out.println(" -u username"); |
| 87 |
System.out.println(" -w password"); |
| 88 |
System.out.println(); |
| 89 |
System.out.println(" -s single-threaded listener"); |
| 90 |
System.out.println(" [absence of option => multithreaded]"); |
| 91 |
|
| 92 |
System.exit(1); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Conn args are still required to set up the JMS listener |
| 97 |
*/ |
| 98 |
public static void main(String[] args) throws Exception |
| 99 |
{ |
| 100 |
Options opts = new Options( args ); |
| 101 |
|
| 102 |
// first check if we should print usage |
| 103 |
if ((opts.isFlagSet('?') > 0) || (opts.isFlagSet('h') > 0)) |
| 104 |
printUsage(); |
| 105 |
|
| 106 |
String username = opts.getUser(); |
| 107 |
String password = opts.getPassword(); |
| 108 |
|
| 109 |
HashMap connectorMap = SimpleJMSListener.createConnectorMap(opts); |
| 110 |
HashMap cfMap = SimpleJMSListener.createCFMap(opts); |
| 111 |
String destination = opts.isValueSet('d'); |
| 112 |
|
| 113 |
args = opts.getRemainingArgs(); |
| 114 |
if ( args == null || args.length == 0) |
| 115 |
printUsage(); |
| 116 |
|
| 117 |
// create the jms listener |
| 118 |
SimpleJMSListener listener = new SimpleJMSListener(connectorMap, |
| 119 |
cfMap, |
| 120 |
destination, |
| 121 |
username, |
| 122 |
password, |
| 123 |
false); |
| 124 |
listener.start(); |
| 125 |
|
| 126 |
JMSURLStubTest stubTest = new JMSURLStubTest("JMS URL static stub test"); |
| 127 |
|
| 128 |
for (int i = 0; i < args.length; i++) |
| 129 |
{ |
| 130 |
try |
| 131 |
{ |
| 132 |
Float quote = stubTest.getQuote(args[i]); |
| 133 |
System.out.println(args[i] + ": " + quote); |
| 134 |
} |
| 135 |
catch(AxisFault af) |
| 136 |
{ |
| 137 |
System.out.println(af.dumpToString()); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
listener.shutdown(); |
| 142 |
|
| 143 |
System.exit(1); |
| 144 |
} |
| 145 |
} |