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.axiom.om.OMFactory;
023: import org.apache.axiom.om.OMAbstractFactory;
024: import org.apache.axiom.om.OMElement;
025: import org.apache.axiom.om.OMNamespace;
026: import org.apache.axiom.soap.SOAPFactory;
027: import org.apache.axiom.soap.SOAPEnvelope;
028: import org.apache.axiom.soap.SOAPHeader;
029: import org.apache.axiom.soap.SOAPBody;
030: import org.apache.axis2.client.Options;
031: import org.apache.axis2.client.ServiceClient;
032: import org.apache.axis2.client.OperationClient;
033: import org.apache.axis2.addressing.EndpointReference;
034: import org.apache.axis2.context.ConfigurationContext;
035: import org.apache.axis2.context.ConfigurationContextFactory;
036: import org.apache.axis2.context.MessageContext;
037: import org.apache.axis2.AxisFault;
038: import org.apache.axis2.Constants;
039: import org.apache.axis2.transport.http.HttpTransportProperties;
040: import org.apache.axis2.transport.http.HTTPConstants;
041: import org.apache.axis2.wsdl.WSDLConstants;
042:
043: import javax.xml.namespace.QName;
044: import java.util.Random;
045: import java.io.File;
046: import java.net.URL;
047: import java.net.MalformedURLException;
048:
049: public class LoadbalanceFailoverClient {
050:
051: /**
052: * @param args 0: simple | session
053: * 1: port
054: * 2: iteration
055: */
056: public static void main(String[] args) {
057:
058: String mode = System.getProperty("mode");
059: try {
060: if (mode != null) {
061: if (mode.equalsIgnoreCase("session")) {
062: new LoadbalanceFailoverClient().sessionfullClient();
063: } else if (mode.equalsIgnoreCase("simple")
064: || mode.equalsIgnoreCase("")) {
065: new LoadbalanceFailoverClient().sessionlessClient();
066: }
067: } else {
068: // default is simple client
069: new LoadbalanceFailoverClient().sessionlessClient();
070: }
071: } catch (AxisFault axisFault) {
072: System.out.println(axisFault.getMessage());
073: }
074: }
075:
076: public String sessionlessClient() throws AxisFault {
077:
078: String synapsePort = "8080";
079: int iterations = 100;
080: boolean infinite = true;
081:
082: String pPort = getProperty("port", synapsePort);
083: String pIterations = getProperty("i", null);
084: String addUrl = getProperty("addurl", null);
085: String trpUrl = getProperty("trpurl", null);
086: String prxUrl = getProperty("prxurl", null);
087:
088: if (pPort != null) {
089: try {
090: Integer.parseInt(pPort);
091: synapsePort = pPort;
092: } catch (NumberFormatException e) {
093: // run with default value
094: }
095: }
096:
097: if (pIterations != null) {
098: try {
099: iterations = Integer.parseInt(pIterations);
100: if (iterations != -1) {
101: infinite = false;
102: }
103: } catch (NumberFormatException e) {
104: // run with default values
105: }
106: }
107:
108: OMFactory fac = OMAbstractFactory.getOMFactory();
109: OMElement value = fac.createOMElement("Value", null);
110: value.setText("Sample string");
111:
112: Options options = new Options();
113: options.setTo(new EndpointReference("http://localhost:"
114: + synapsePort));
115:
116: options.setAction("urn:sampleOperation");
117:
118: String repo = System.getProperty("repository");
119: ConfigurationContext configContext;
120: if (repo != null) {
121: configContext = ConfigurationContextFactory
122: .createConfigurationContextFromFileSystem(repo,
123: repo + File.separator + "conf"
124: + File.separator + "axis2.xml");
125: } else {
126: configContext = ConfigurationContextFactory
127: .createConfigurationContextFromFileSystem(
128: "client_repo", null);
129: }
130: ServiceClient client = new ServiceClient(configContext, null);
131: options.setTimeOutInMilliSeconds(10000000);
132:
133: // set addressing, transport and proxy url
134: if (addUrl != null && !"null".equals(addUrl)) {
135: client.engageModule("addressing");
136: options.setTo(new EndpointReference(addUrl));
137: }
138: if (trpUrl != null && !"null".equals(trpUrl)) {
139: options.setProperty(Constants.Configuration.TRANSPORT_URL,
140: trpUrl);
141: } else {
142: client.engageModule("addressing");
143: }
144: if (prxUrl != null && !"null".equals(prxUrl)) {
145: HttpTransportProperties.ProxyProperties proxyProperties = new HttpTransportProperties.ProxyProperties();
146: try {
147: URL url = new URL(prxUrl);
148: proxyProperties.setProxyName(url.getHost());
149: proxyProperties.setProxyPort(url.getPort());
150: proxyProperties.setUserName("");
151: proxyProperties.setPassWord("");
152: proxyProperties.setDomain("");
153: options.setProperty(HTTPConstants.PROXY,
154: proxyProperties);
155: } catch (MalformedURLException e) {
156: throw new AxisFault("Error creating proxy URL", e);
157: }
158: }
159:
160: client.setOptions(options);
161: String testString = "";
162:
163: long i = 0;
164: while (i < iterations || infinite) {
165: OMElement responseElement = client.sendReceive(value);
166: String response = responseElement.getText();
167:
168: i++;
169: System.out.println("Request: " + i + " ==> " + response);
170: testString += (":" + i + ">" + response + ":");
171: }
172:
173: return testString;
174: }
175:
176: /**
177: * This method creates 3 soap envelopes for 3 different client based sessions. Then it randomly
178: * choose one envelope for each iteration and send it to the ESB. ESB should be configured with
179: * session affinity load balancer and the SampleClientInitiatedSession dispatcher. This will
180: * output request number, session number and the server ID for each iteration. So it can be
181: * observed that one session number always associated with one server ID.
182: */
183: private void sessionfullClient() {
184:
185: String synapsePort = "8080";
186: int iterations = 100;
187: boolean infinite = true;
188:
189: String pPort = getProperty("port", synapsePort);
190: String pIterations = getProperty("i", null);
191: String addUrl = getProperty("addurl", null);
192: String trpUrl = getProperty("trpurl", null);
193: String prxUrl = getProperty("prxurl", null);
194:
195: if (pPort != null) {
196: try {
197:
198: Integer.parseInt(pPort);
199: synapsePort = pPort;
200: } catch (NumberFormatException e) {
201: // run with default value
202: }
203: }
204:
205: if (pIterations != null) {
206: try {
207: iterations = Integer.parseInt(pIterations);
208: if (iterations != -1) {
209: infinite = false;
210: }
211: } catch (NumberFormatException e) {
212: // run with default values
213: }
214: }
215:
216: Options options = new Options();
217: options.setTo(new EndpointReference("http://localhost:"
218: + synapsePort));
219: options.setAction("urn:sampleOperation");
220: options.setTimeOutInMilliSeconds(10000000);
221:
222: try {
223:
224: SOAPEnvelope env1 = buildSoapEnvelope("c1", "v1");
225: SOAPEnvelope env2 = buildSoapEnvelope("c2", "v1");
226: SOAPEnvelope env3 = buildSoapEnvelope("c3", "v1");
227: SOAPEnvelope[] envelopes = { env1, env2, env3 };
228:
229: ConfigurationContext configContext = ConfigurationContextFactory
230: .createConfigurationContextFromFileSystem(
231: "client_repo", null);
232: ServiceClient client = new ServiceClient(configContext,
233: null);
234:
235: // set addressing, transport and proxy url
236: if (addUrl != null && !"null".equals(addUrl)) {
237: client.engageModule("addressing");
238: options.setTo(new EndpointReference(addUrl));
239: }
240: if (trpUrl != null && !"null".equals(trpUrl)) {
241: options.setProperty(
242: Constants.Configuration.TRANSPORT_URL, trpUrl);
243: } else {
244: client.engageModule("addressing");
245: }
246: if (prxUrl != null && !"null".equals(prxUrl)) {
247: HttpTransportProperties.ProxyProperties proxyProperties = new HttpTransportProperties.ProxyProperties();
248: try {
249: URL url = new URL(prxUrl);
250: proxyProperties.setProxyName(url.getHost());
251: proxyProperties.setProxyPort(url.getPort());
252: proxyProperties.setUserName("");
253: proxyProperties.setPassWord("");
254: proxyProperties.setDomain("");
255: options.setProperty(HTTPConstants.PROXY,
256: proxyProperties);
257: } catch (MalformedURLException e) {
258: throw new AxisFault("Error creating proxy URL", e);
259: }
260: }
261: client.setOptions(options);
262:
263: int i = 0;
264: int sessionNumber = 0;
265: while (i < iterations || infinite) {
266:
267: i++;
268:
269: MessageContext messageContext = new MessageContext();
270: sessionNumber = getSessionTurn(envelopes.length);
271: messageContext.setEnvelope(envelopes[sessionNumber]);
272:
273: OperationClient op = client
274: .createClient(ServiceClient.ANON_OUT_IN_OP);
275: op.addMessageContext(messageContext);
276: op.execute(true);
277:
278: MessageContext responseContext = op
279: .getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
280: SOAPEnvelope responseEnvelope = responseContext
281: .getEnvelope();
282:
283: OMElement vElement = responseEnvelope.getBody()
284: .getFirstChildWithName(new QName("Value"));
285: System.out.println("Request: " + i
286: + " Session number: " + sessionNumber + " "
287: + vElement.getText());
288: }
289:
290: } catch (AxisFault axisFault) {
291: System.out.println(axisFault.getMessage());
292: }
293: }
294:
295: private int getSessionTurn(int max) {
296: Random random = new Random();
297: return random.nextInt(max);
298: }
299:
300: private SOAPEnvelope buildSoapEnvelope(String clientID, String value) {
301:
302: String targetEPR = "http://localhost:9000/soap/Service1";
303: String opration = "sampleOperation";
304:
305: SOAPFactory soapFactory = OMAbstractFactory.getSOAP12Factory();
306:
307: OMNamespace wsaNamespace = soapFactory.createOMNamespace(
308: "http://www.w3.org/2005/08/addressing", "wsa");
309:
310: SOAPEnvelope envelope = soapFactory.createSOAPEnvelope();
311:
312: SOAPHeader header = soapFactory.createSOAPHeader();
313: envelope.addChild(header);
314:
315: OMNamespace synNamespace = soapFactory.createOMNamespace(
316: "http://ws.apache.org/namespaces/synapse", "syn");
317: OMElement clientIDElement = soapFactory.createOMElement(
318: "ClientID", synNamespace);
319: clientIDElement.setText(clientID);
320: header.addChild(clientIDElement);
321:
322: SOAPBody body = soapFactory.createSOAPBody();
323: envelope.addChild(body);
324:
325: OMElement valueElement = soapFactory.createOMElement("Value",
326: null);
327: valueElement.setText(value);
328: body.addChild(valueElement);
329:
330: return envelope;
331: }
332:
333: private static String getProperty(String name, String def) {
334: String result = System.getProperty(name);
335: if (result == null || result.length() == 0) {
336: result = def;
337: }
338: return result;
339: }
340: }
|