Next-generation Web service implementation package-axis2 Study Notes (2)

Source: Internet
Author: User
Tags wsdl

Client call

Web services provide a variety of services, some of which can get results immediately, and some take a long time. Therefore, if we need multiple call methods to deal with different situations. Most Web Services provideBlocking(Blocking) andNon-blocking(Non-blocking) the two concepts of APIs. Should have been learned before, simply put.Blocking API-The caller must wait for the called function to run.

Non-bloking API-After the caller runs the called function, the caller directly goes down. The caller and the called end are executed asynchronously. The return value is implemented using the callback function. This kind of Asynchronization is calledAsynchronous API Layer(API level asynchrony). They only use a connection to send and receive messages. In addition, if a function needs to run for a long time, a time out error occurs, if two connections are used to process the sent and received messages respectively, the call time can be shortened and the time out problem can be solved. Two connections are used to process the sent and received messages respectively.Asynchronous Transport Layer(Transport level asynchrony).

API Transmission Description
Blocking 1 Connection Simple and traditional usage
Non-blocking 1 Connection Use callback or polling
Blocking 2 connection A service has a return value, but its transmission is a single path. (For example, SMTP)
Non-blocking 2 connection Maximum asynchronous execution

The theory is boring. Let's look at the example.

Open eclipse and create a new project named userguide. in the clients package, copy all the files under "samples/userguide/src/userguide/clients" to the package, add all the jar files under the Lib of axis2 to ilbrary (you should not add them all. If you are too lazy, add them all .) five Echo calling methods are found:
Echoblockingclient
Echoblockingdualclient
Echoblockingwsabasedclient
Echonblockingclient
Echonblockingdualclient

One by one.
Echoblockingclient. Java
Public class echoblockingclient {
Private Static endpointreference targetepr = new endpointreference ("http: // localhost: 8080/axis2/services/myservice ");

Public static void main (string [] ARGs ){
Try {
Omelement payload = clientutil. getechoomelement ();
Call call = new call ();
Call. setto (targetepr );
Call.Settransportinfo(Constants. transport_http,
Constants. transport_http,
False );

// Blocking Invocation
Omelement result = call.Invokeblocking("Echo ",
Payload );

Stringwriter writer = new stringwriter ();
Result. serializewithcache (xmloutputfactory. newinstance ()
. Createxmlstreamwriter (writer ));
Writer. Flush ();

System. Out. println (writer. tostring ());

} Catch (axisfault ){
Axisfault. printstacktrace ();
} Catch (xmlstreamexception e ){
E. printstacktrace ();
}
}
}

Like a generation, get an endpointreference and a call. The others are different, but it is also very simple. Get an omelement as a parameter and return an omelement. Unfortunately, there is a mistake in running.

Let's look at the dual-channel version.
Echoblockingdualclient. Java
Public class echoblockingdualclient {
Private Static endpointreference targetepr = new endpointreference ("http: // 127.0.0.1: 8080/axis2/services/myservice ");

Public static void main (string [] ARGs ){
Try {
Omelement payload = clientutil. getechoomelement ();

Call call = new call ();
Call. setto (targetepr );

Call. engagemodule (New QNAME (constants. module_addressing ));
Call. settransportinfo (constants. transport_http,
Constants. transport_http,
True);

// Blocking Invocation
Omelement result = call. invokeblocking ("Echo ",
Payload );

Stringwriter writer = new stringwriter ();
Result. serializewithcache (xmloutputfactory. newinstance ()
. Createxmlstreamwriter (writer ));
Writer. Flush ();
System. Out. println (writer. tostring ());

// Need to close the client side listener.
Call. Close ();

} Catch (axisfault ){
Axisfault. printstacktrace ();
} Catch (exception ex ){
Ex. printstacktrace ();
}

}
}

An engagemodule is added, which seems useless. I can run it even if I delete this sentence, and the last parameter settransportinfo is changed to true. for the three parameters of settransportinfo, the first parameter is the sent transport, the second parameter is the received transport, and the third parameter is "dual-channel". The supported parameters are as follows:
HTTP, HTTP, true
HTTP, HTTP, false
HTTP, SMTP, true
SMTP, HTTP, true
SMTP, SMTP, true

Let's look at the next one,EchonblockingclientThis is the non-blocking mode of a single channel:
Public class echononblockingclient {
Private Static endpointreference targetepr = new endpointreference ("http: // 127.0.0.1: 8080/axis2/services/myservice ");

Public static void main (string [] ARGs ){
Try {
Omelement payload = clientutil. getechoomelement ();

Call call = new call ();
Call. setto (targetepr );
Call. settransportinfo (constants. transport_http,
Constants. transport_http,
False);

// Callback to handle the response
Callback callback = newCallback(){
Public voidOncomplete(Asyncresult result ){
Try {
Stringwriter writer = new stringwriter ();
Result. getresponseenvelope (). serializewithcache (xmloutputfactory. newinstance ()
. Createxmlstreamwriter (writer ));
Writer. Flush ();
System. Out. println (writer. tostring ());

} Catch (xmlstreamexception e ){
Reporterror (E );
}
}

Public voidReporterror(Exception e ){
E. printstacktrace ();
}
};

// Non-blocking Invocation
Call.Invokenonblocking("Echo", payload, callback );

// Wait till the callback es the response.
While (! Callback. iscomplete ()){
Thread. Sleep (1000 );
}

} Catch (axisfault ){
Axisfault. printstacktrace ();
} Catch (exception ex ){
Ex. printstacktrace ();
}

}
}

The difference is that the called method is changed from invokeblocking to invokenonblocking, and then a simple anonymous callback class is written as the callback function. This callback class is an abstract class with two methods: oncomplete and reporterror, which must be implemented by the client. It also has a field, that is, complete, it can be used to set and query whether the call is complete. Unfortunately, it cannot be run. Like the preceding error, the null error is reported during createsoapmessage.

Check the next echononblockingdualclient, which is a non-blocking dual channel:
Public class echononblockingdualclient {
Private Static endpointreference targetepr = new endpointreference ("http: // 127.0.0.1: 8080/axis2/services/myservice ");

Public static void main (string [] ARGs ){
Try {
Omelement payload = clientutil. getechoomelement ();

Call call = new call ();
Call. setto (targetepr );

// The Boolean flag informs the axis2 engine to use two separate transport connection
// To retrieve the response.
Call. engagemodule (New QNAME (constants. module_addressing ));
Call. settransportinfo (constants. transport_http,
Constants. transport_http,
True );

// Callback to handle the response
Callback callback = new callback (){
Public void oncomplete (asyncresult result ){
Try {
Stringwriter writer = new stringwriter ();
Result. getresponseenvelope (). serializewithcache (xmloutputfactory. newinstance ()
. Createxmlstreamwriter (writer ));
Writer. Flush ();
System. Out. println (writer. tostring ());

} Catch (xmlstreamexception e ){
Reporterror (E );
}
}

Public void reporterror (exception e ){
E. printstacktrace ();
}
};

// Non-blocking Invocation
Call. invokenonblocking ("Echo", payload, callback );

// Wait till the callback es the response.
While (! Callback. iscomplete ()){
Thread. Sleep (1000 );
}
// Need to close the client side listener.
Call. Close ();

} Catch (axisfault ){
Axisfault. printstacktrace ();
} Catch (exception ex ){
Ex. printstacktrace ();
}

}
}
The dual-channel and single-channel are basically no different, but when the dual-channel is used, it always needs to set the engagemodule, and then an additional call. close (); close the client side listener ).

All of the above are calls that require a return value. If you do not need a return value, look at pingclient.
Public class pingclient {
Private Static endpointreference targetepr = new endpointreference ("http: // localhost: 8080/axis2/services/myservice ");

Public static void main (string [] ARGs ){
Try {
Omelement payload = clientutil. getpingomelement ();

MessagesenderMsgsender = new messagesender ();
Msgsender. setto (targetepr );
Msgsender. setsendertransport (constants. transport_http );

Msgsender.Send("Ping", payload );

} Catch (axisfault ){
Axisfault. printstacktrace ();
}
}

}
Well, it's a little simpler, just a msgsender. Send.

The client call combination is basically finished, but there is another echoblockingwsabasedclient. What is this? What else is the engagemodule used? Ignore them first.

Let's take a look at the most useful tool I think: wsdl2java, which has wsdl2java under the bin directory. BAT and wsdl2java. sh. What is this tool used to do? Like a generation, it is used to generate stub. That is to say, after someone else releases the web service, there will be a WSDL file, this tool can generate several classes based on the WSDL, and all the underlying calls will be rolled up. Then, you will use the tool like a common function call.
Run the following command to go to the samples/WSDL directory and see axis2sampledoclit. WSDL, run .. /.. /bin/wsdl2java. bat-Uri axis2sampledoclit. WSDL: there are two more directories in the directory. No matter schemaorg_apache_xmlbeans, copy the codegen directory to a project in eclipse. Wow, there are a lot of classes. If you don't read anything else, you can see axis2sampledoclitporttypestub, there are three functions provided by Web Service: echostringarray, echostruct, echostring, haha, what call class, messagecontext, all in it. Use it like this:
Try {
Axis2sampledoclitporttypestub stub = new axis2sampledoclitporttypestub (null, "http: // localhost: 8080/axis2/services/axis2sampledoclitporttype ");
// Create the Request Document to be sent.
EchostringparamdocumentReqdoc = echostringparamdocument. Factory. newinstance ();
Reqdoc. setechostringparam ("axis2 echo ");
// Invokes the web service.
Echostringreturndocument resdoc = stub. echostring (reqdoc );
System. Out. println (resdoc. getechostringreturn ());

} Catch (exception e ){
E. printstacktrace ();
}

It is to create a stub, create another parameter class (echostringparamdocument), and then call the function to pass the parameter, which is no different from the normal function call. If you enter wsdl2java. bat in the command line, you will see its help prompt as follows:
Usage wsdl2code-Uri <location of WSDL>: WSDL File Location
-O <output location>: output file location
-A: generate async style code only. Default if off
-S: Generate sync style code only. Default if off. takes precedence over-
-P <package name>: Set custom package name
-L <language>: Valid ages are Java and CSHARP. Default is Java
-T: Generate testcase to test the generated code
-SS: Generate server side code (I. e. Skeletons). Default is off
-SD: Generate service Descriptor (I. e. axis2.xml). Default is off. Valid with-SS

 

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.