001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package samples.userguide;
021:
022: import org.apache.axis2.client.ServiceClient;
023: import org.apache.axis2.client.Options;
024: import org.apache.axis2.addressing.EndpointReference;
025: import org.apache.axis2.context.ConfigurationContext;
026: import org.apache.axis2.context.ConfigurationContextFactory;
027: import org.apache.axis2.AxisFault;
028: import org.apache.axiom.om.OMElement;
029: import org.apache.axiom.om.OMFactory;
030: import org.apache.axiom.om.OMAbstractFactory;
031: import org.apache.axiom.om.OMNamespace;
032: import org.apache.axiom.soap.SOAPFactory;
033: import org.apache.axiom.soap.SOAPHeaderBlock;
034:
035: import javax.xml.namespace.QName;
036:
037: public class ServiceInvoker extends Thread {
038:
039: private String invokerName = "anonymous";
040: private String operation = null;
041: private ServiceClient client = null;
042: private OMElement msg = null;
043: private long iterations = 10;
044: private boolean statefull = false;
045:
046: private OMFactory fac = null;
047:
048: private long runningTime = 0;
049:
050: public ServiceInvoker(String epr, String operation) {
051:
052: this .operation = operation;
053:
054: Options options = new Options();
055: options.setTo(new EndpointReference(epr));
056: options.setAction(operation);
057:
058: try {
059: ConfigurationContext configContext = ConfigurationContextFactory
060: .createConfigurationContextFromFileSystem(
061: "client_repo", null);
062:
063: client = new ServiceClient(configContext, null);
064: options.setTimeOutInMilliSeconds(10000000);
065:
066: client.setOptions(options);
067: client.engageModule("addressing");
068:
069: } catch (AxisFault axisFault) {
070: axisFault.printStackTrace();
071: }
072:
073: fac = OMAbstractFactory.getOMFactory();
074: msg = fac.createOMElement("SampleMsg", null);
075:
076: OMElement load = fac.createOMElement("load", null);
077: load.setText("1000");
078: msg.addChild(load);
079: }
080:
081: public String getInvokerName() {
082: return invokerName;
083: }
084:
085: public void setInvokerName(String invokerName) {
086: this .invokerName = invokerName;
087:
088: if (statefull) {
089: client.getOptions().setManageSession(true);
090: client.getOptions().setAction("setClientName");
091:
092: OMElement cName = fac.createOMElement("cName", null);
093: cName.setText(invokerName);
094:
095: try {
096: OMElement response = client.sendReceive(cName);
097: System.out.println(response.getText());
098: } catch (AxisFault axisFault) {
099: axisFault.printStackTrace();
100: }
101: }
102: }
103:
104: public long getRunningTime() {
105: return runningTime;
106: }
107:
108: public void setLoad(String load) {
109: OMElement loadElement = msg.getFirstChildWithName(new QName(
110: "load"));
111: loadElement.setText(load);
112: }
113:
114: public void addDummyElements(long numElements) {
115: OMElement dummies = fac.createOMElement("Dummies", null);
116: msg.addChild(dummies);
117:
118: for (long i = 0; i < numElements; i++) {
119: OMElement dummy = fac.createOMElement("Dummy", null);
120: dummy.setText("This is the dummy element " + i);
121: dummies.addChild(dummy);
122: }
123: }
124:
125: public void setClientSessionID(String id) {
126:
127: SOAPFactory soapFactory = OMAbstractFactory.getSOAP12Factory();
128:
129: OMNamespace synNamespace = soapFactory.createOMNamespace(
130: "http://ws.apache.org/namespaces/synapse", "syn");
131: SOAPHeaderBlock header = soapFactory.createSOAPHeaderBlock(
132: "ClientID", synNamespace);
133: header.setText(id);
134:
135: client.addHeader(header);
136: }
137:
138: public void setIterations(long i) {
139: iterations = i;
140: }
141:
142: public void setStatefull(boolean state) {
143: this .statefull = state;
144: }
145:
146: public void run() {
147:
148: client.getOptions().setAction(operation);
149:
150: try {
151:
152: long t1 = System.currentTimeMillis();
153:
154: for (long i = 0; i < iterations; i++) {
155: OMElement response2 = client.sendReceive(msg);
156: OMElement loadElement = response2
157: .getFirstChildWithName(new QName("load"));
158: System.out.println(invokerName + ": "
159: + loadElement.toString());
160: }
161:
162: long t2 = System.currentTimeMillis();
163:
164: System.out
165: .println("================================================================");
166: System.out.println(invokerName + " completed requests.");
167: System.out
168: .println("================================================================");
169: runningTime = t2 - t1;
170:
171: } catch (AxisFault axisFault) {
172: System.out.println(axisFault.getMessage());
173: }
174: }
175: }
|