Road leading to architect (11th day) Axis2 Web Service (ii)

Source: Internet
Author: User
Tags soap xmlns soap ui stringbuffer wsdl
I. Summary of the day before

The day before we talked about how to generate a Axis2 webservice, how to Deployment and 4 different clients, they are: traditional, non-blocking, duplex mode, duplex non-blocking.

And we've seen a description of a AXIS2 Web service's deployment:

<service name= "HelloWorld" >

<parameter name= "ServiceClass" >org.sky.axis2.helloworld.HelloWorld</parameter>

<operation name= "SayHello" >

<messagereceiver class= "Org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>

<actionMapping>urn:sayHello</actionMapping>

</operation>

</service>

This description represents our Web service in a two-parameter way, and is a Axis2-specific "omelement" type.

Well, we want a simple Java type such as public string SayHello (string name) to write our webservice.

Of course, only our deployment description is slightly different from our client invocation return value. Ii. Writing our webservice with a simple Java type

Hellojava class:

Package org.sky.axis2.helloworld.javatype;

Import javax.xml.stream.XMLStreamException;

Import org.apache.axiom.om.OMElement;

Public class Hellojava {

         public string SayHello (string name ) throws Xmlstreamexception {

                    StringBuffer Hello = new StringBuffer ();

                    Hello.append ("Hello:");

                    Hello.append (name);

                    return hello.tostring ();

        }

}

Service description File:

At this point our corresponding deployment file is the service.xml file content slightly different, to see

<service name= "Hellojava" >

<parameter name= "ServiceClass" locked= "false" >

Org.sky.axis2.helloworld.javatype.HelloJava

</parameter>

<messageReceivers>

<messagereceiver mep= "Http://www.w3.org/2004/08/wsdl/in-out"

class= "Org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>

</messageReceivers>

<actionMapping>urn:sayHello</actionMapping>

</service>

We put this webservice into Tomcat and start looking.

is not more a service called "Hellojava".

We import the WSDL address of the service into the SOAP UI tool and generate the mock client, and then look at its call and return value.

Note This return value:

<soapenv:envelope xmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/" >

<soapenv:Body>

<ns:sayhelloresponse xmlns:ns= "http://javatype.helloworld.axis2.sky.org" >

<ns:return>hello:simon shen</ns:return>

</ns:sayHelloResponse>

</soapenv:Body>

</soapenv:Envelope>

Marked with red bold part, anyway a thick, is useful is good thing ( I like coarse, YEAH).

Let's compare that yesterday with the "Org.apache.axis2.receivers.RawXMLINOutMessageReceiver" service description of the generated return value

Org.apache.axis2.receivers.RawXMLINOutMessageReceiver

<soapenv:envelope xmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/" >

<soapenv:Body>

<!--optional:-->

</soapenv:Body>

</soapenv:Envelope>

See no difference. So, change our client calling code as follows, where we use asynchronous Duplex mode:

Org.sky.axis2.helloworld.javatype.HelloJavaWithReturnDualNonBlock

Package org.sky.axis2.helloworld.javatype;

Import Org.apache.axiom.om.OMAbstractFactory;

Import org.apache.axiom.om.OMElement;

Import Org.apache.axiom.om.OMFactory;

Import Org.apache.axiom.om.OMNamespace;

Import Org.apache.axis2.AxisFault;

Import org.apache.axis2.Constants;

Import org.apache.axis2.addressing.EndpointReference;

Import org.apache.axis2.client.Options;

Import org.apache.axis2.client.ServiceClient;

Import Org.apache.axis2.context.ConfigurationContext;

Import Org.apache.axis2.context.ConfigurationContextFactory;

public class Hellojavawithreturndualnonblock {

private static EndpointReference Targetepr = new EndpointReference (

"Http://localhost:8080/Axis2Service/services/HelloJava");

public static Boolean finish = false;

public void SayHello () {

Omfactory FAC = omabstractfactory.getomfactory ();

Omnamespace omns = Fac.createomnamespace (

"Http://javatype.helloworld.axis2.sky.org", "" ");

Omelement method = Fac.createomelement ("SayHello", omns);

Omelement name = fac.createomelement ("name", Omns);

Name.settext ("YMK");

Method.addchild (name);

Method.build ();

Options options = new options ();

Options.setto (TARGETEPR);

Options.settransportinprotocol (constants.transport_http);

Options.setuseseparatelistener (TRUE);

Options.setaction ("Urn:sayhello");

ServiceClient sender = null;

HELLOJAVANONBLOCKCB callback = new HELLOJAVANONBLOCKCB ();

try {

Sender = new ServiceClient ();

Sender.engagemodule (constants.module_addressing);

Sender.setoptions (options);

Sender.sendreceivenonblocking (method, callback);

Synchronized (callback) {

try {

Callback.wait ();

} catch (Interruptedexception e) {

E.printstacktrace ();

}

}

} catch (Exception e) {

E.printstacktrace ();

} finally {

try {

Sender.cleanup ();

} catch (Exception e) {

}

}

}

public static void Main (string[] args) {

Hellojavawithreturndualnonblock testclient = new Hellojavawithreturndualnonblock ();

Testclient.sayhello ();

}

}

Related callback class,Org.sky.axis2.helloworld.javatype.HelloJavaNonBlockCB:

Package org.sky.axis2.helloworld.javatype;

Import Java.util.Iterator;

Import Javax.xml.namespace.QName;

Import org.apache.axiom.om.OMElement;

Import Org.apache.axiom.om.OMNode;

Import Org.apache.axis2.client.async.AxisCallback;

Import Org.apache.axis2.context.MessageContext;

Import Org.apache.axis2.databinding.utils.BeanUtil;

Import Org.apache.axis2.engine.DefaultObjectSupplier;

public class HELLOJAVANONBLOCKCB implements Axiscallback {

Private Boolean complete = false;

public void OnMessage (Messagecontext msgcontext) {

System.out.println (Msgcontext.getenvelope (). GetBody ());

omelement element = Msgcontext.getenvelope (). GetBody ()

. Getfirstelement ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.