KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > userguide > clients > TCPClient


1 /*
2  * Copyright 2004,2005 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 userguide.clients;
17
18 import org.apache.axis2.Constants;
19 import org.apache.axis2.addressing.AddressingConstants;
20 import org.apache.axis2.addressing.EndpointReference;
21 import org.apache.axis2.clientapi.Call;
22 import org.apache.axis2.engine.AxisFault;
23 import org.apache.axis2.om.*;
24
25 import javax.xml.namespace.QName JavaDoc;
26 import javax.xml.stream.FactoryConfigurationError;
27 import javax.xml.stream.XMLOutputFactory;
28 import javax.xml.stream.XMLStreamException;
29 import javax.xml.stream.XMLStreamWriter;
30
31 /**
32  * This is a Client progam that accesses 'MyService' web service in Axis2 samples
33  */

34 public class TCPClient {
35
36     private static String JavaDoc toEpr = "tcp://localhost:8080/axis2/services/MyService";
37     
38     public static void main(String JavaDoc[] args) throws AxisFault {
39         
40             Call call = new Call();
41             call.setTo(new EndpointReference(AddressingConstants.WSA_TO,toEpr));
42             call.setTransportInfo(Constants.TRANSPORT_TCP,Constants.TRANSPORT_TCP,false);
43             //call.engageModule(new QName(Constants.MODULE_ADDRESSING));
44

45             OMElement result = call.invokeBlocking("echo", getPayload());
46         
47             try {
48                 XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
49                 result.serializeWithCache(new OMOutput(writer));
50                 writer.flush();
51             } catch (XMLStreamException e) {
52                 e.printStackTrace();
53             } catch (FactoryConfigurationError e) {
54                 e.printStackTrace();
55             }
56     }
57     
58     
59     private static OMElement getPayload() {
60         OMFactory fac = OMAbstractFactory.getOMFactory();
61         OMNamespace omNs = fac.createOMNamespace(
62                 "tcp://localhost:8080/axis2/services/MyService", "example1");
63         OMElement method = fac.createOMElement("echo", omNs);
64         OMElement value = fac.createOMElement("Text", omNs);
65         value.addChild(fac.createText(value, "Axis2 Echo String "));
66         method.addChild(value);
67
68         return method;
69     }
70 }
71
Popular Tags