Axis2+wsdl2java.bat Generating Client Calls

Source: Internet
Author: User
Tags wsdl

Axis2 and axis increase the way asynchronous calls are made, and the code is easier to use. This article uses an example to describe how to quickly use AXIS2 to create WebService client code.
1 Preparation environment
(1) Download Axis2, and unzip.
Official website: http://ws.apache.org/axis2/
(2) Setting environment variables
SET axis2_home= the directory where the bin directory is extracted, try not to have spaces or Chinese
SET path=%axis2_home%\bin;%path%

(3) Modify the Wsdl2java.bat in the bin directory
For ease of use, and to avoid some noclassfoundexception similar problems, modify the Wsdl2java.bat file to look for:
Set _runjava= "%java_home%\bin\java"
Add the following line below this line
Set java_opts= "-djava.ext.dirs=%axis2_home%\lib"

2 Take a Whois webservice as an example:
Generate Stub code
Wsdl2java-uri Http://www.webservicex.net/whois.asmx?wsdl-o src
After the program runs, the following files are generated in the src file:
E:\SRC\NET
└─webservicex
└─www
Whoiscallbackhandler.java
Whoisstub.java

3 Synchronous calls
(1) Writing Demosync.java
Import net.webservicex.www.*;
public class Demosync {
public static void Main (string[] args) {
try {
Whoisstub stock=new whoisstub ();
Whoisstub.getwhois param=new Whoisstub.getwhois ();
Param.sethostname ("www.sohu.com");
Whoisstub.getwhoisresponse Response=stock. Getwhois (param);
System.out.println (Response.getgetwhoisresult ());

} catch (Java.rmi.RemoteException remoteexception) {
Remoteexception.printstacktrace ();
}
}
}
(2) test
Run Java-djava.ext.dirs=%axis2_home%\lib Demosync

4 Asynchronous calls
(1) Inherit Whoiscallbackhandler class, write Mywhoiscallbackhandler.java as follows;
Package net.webservicex.www;
public class Mywhoiscallbackhandler extends whoiscallbackhandler{
public void Receiveresultgetwhois (
Net.webservicex.www.WhoisStub.GetWhoISResponse result) {
System.out.println (Result.getgetwhoisresult ());
}
}

(2) Writing Demoasync.java
Import net.webservicex.www.*;
public class Demoasync {
public static void Main (string[] args) {
try {
Whoisstub stock=new whoisstub ();
Whoisstub.getwhois param=new Whoisstub.getwhois ();
Param.sethostname ("www.sohu.com");
Whoiscallbackhandler handler=new Mywhoiscallbackhandler ();
Stock.startgetwhois (Param,handler);
System.out.println ("Sent a request.");
while (Handler.getclientdata () ==null)
{
try{
Thread.Sleep (30000);//wait for handler to return the result to test the return situation, in the actual application, here can do other operations, When the results are returned, the data is displayed, saved, or activated at other times in the Mywhoiscallbackhandler.receiveresultgetwhois () method.
}catch (Interruptedexception e) {}
}
} catch (Java.rmi.RemoteException remoteexception) {
Remoteexception.printstacktrace ();
}
}
}
(3) test
Run Java-djava.ext.dirs=%axis2_home%\lib Demoasync


trackback:http://tb.blog.csdn.net/trackback.aspx?postid=1743249


--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------
Web Service-axis2
Recently useful to Web service in projects. Before doing Google a bit, found that there are some articles on the site of the sky covered. Say this is the next generation in the technology of the commanding heights! I Axis2 made a "Hello World!" "It's not that much of a difficulty, it's probably not a high-level feature!" Write the class you want to publish as a service first. Pojo on the line! :

public class Miniservice {
public string Hello (string msg)
{
Return "Hello World";
}
}

To deploy a server correctly, where server.xml this file is essential, here's what this example uses
Server.xml file: Please note that this file is to be placed in the Meta-inf file.

<?xml version= "1.0" encoding= "UTF-8"?>
<service>
<parameter name= "ServiceClass" locked= "false" >com.inventec.ws.MiniService</parameter>
<operation name= "Hello" >
<messagereceiver class= "Org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
</service>

The last packaged jar file is placed under Axis2\web-inf\services. Tomcat will deploy the service.
To properly invoke this service you also have to generate stubs. You can easily generate stubs with the tools you bring with AXIS2. For example:

D:\ws>wsdl2java-uri http://localhost:8080/axis2/services/ws?wsdl

The resulting east is very long to see very tired! You can basically use the following method when invoking the class as follows:


public class Wstest {

/**
* @param args
* @throws RemoteException
*/
public static void Main (string[] args) throws RemoteException {
TODO auto-generated Method Stub


Wsstub stub = new Wsstub ();
Wsstub.hello request = new Wsstub.hello (); /**/
Request.setmsg ("Jade");/**/
Wsstub.helloresponse reponse = Stub.hello (request);
String str = Reponse.get_return ();
System.out.println ("str =" + str);

}

}

Of course, if you publish a method that does not have parameters, do not generate this method object at the time of the call.

Just remember:

Mister into a stub instance
The method of the service in the stub to become one of its variables. We generate a method-like object
Give this method input, call the parameter.
Invokes a method in the stub instance with the same name in the server as the method object generated earlier. and returns a response,
Call Response's Get_return () to get the return value!

Axis2+wsdl2java.bat Generating Client Calls

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.