三种Axis的部署和开发
源文件:http://ychx7.spaces.live.com/blog/cns!ba7ed89939b141d7!119.entry
Axis支持三种web service的部署和开发,分别为:
1、Dynamic Invocation Interface ( DII)
2、Stubs方式
3、Dynamic Proxy方式
二、编写DII(Dynamic Invocation Interface )方式web服务
1.编写服务端程序HelloClient
public class HelloClient{public String getName(String name){return "hello "+name;}}
import org.apache.Axis.client.Call;import org.apache.Axis.client.Service;import javax.xml.namespace.QName;import javax.xml.rpc.ServiceException;import java.net.MalformedURLException;import java.rmi.RemoteException;public class SayHelloClient2{public static void main(String[] args){try{String endpoint ="http://localhost:8080/Axis/HelloClient.jws";Service service = new Service();Call call = null;call = (Call) service.createCall();call.setOperationName(new QName("http://localhost:8080/Axis/HelloClient.jws","getName"));call.setTargetEndpointAddress(new java.net.URL(endpoint));String ret = (String) call.invoke(new Object[]{"zhangsan"});System.out.println("return value is " + ret);}catch (Exception ex){ex.printStackTrace();}}}
public interface HelloClientInterfaceextends java.rmi.Remote{public String getName(String name)throws java.rmi.RemoteException;}
import javax.xml.rpc.Service;import javax.xml.rpc.ServiceFactory;import java.net.URL;import javax.xml.namespace.QName;public class TestHelloClient{public static void main(String[] args){try{String wsdlUrl ="http://localhost:8080/Axis/HelloClient.jws?wsdl";String nameSpaceUri ="http://localhost:8080/Axis/HelloClient.jws";String serviceName = "HelloClientService";String portName = "HelloClient";ServiceFactory serviceFactory =ServiceFactory.newInstance();Service afService =serviceFactory.createService(new URL(wsdlUrl),new QName(nameSpaceUri, serviceName));HelloClientInterface proxy = (HelloClientInterface)afService.getPort(new QName(nameSpaceUri, portName),HelloClientInterface.class);System.out.println("return value is "+proxy.getName("john") ) ;}catch(Exception ex){ex.printStackTrace() ;}}}
package server;public class SayHello{public String getName(String name){return "hello "+name;}}2.编写LogHandler.javaimport org.apache.Axis.AxisFault;import org.apache.Axis.Handler;import org.apache.Axis.MessageContext;import org.apache.Axis.handlers.BasicHandler;import java.util.Date;public class LogHandlerextends BasicHandler{public void invoke(MessageContext msgContext)throws AxisFault{/** Log an access each timewe get invoked.*/try {Handler serviceHandler= msgContext.getService();Integer numAccesses =(Integer)serviceHandler.getOption("accesses");if (numAccesses == null)numAccesses = new Integer(0);numAccesses = new Integer(numAccesses.intValue() + 1);Date date = new Date();String result =date + ": service " +msgContext.getTargetService() +" accessed " + numAccesses + " time(s).";serviceHandler.setOption("accesses", numAccesses);System.out.println(result);} catch (Exception e){throw AxisFault.makeFault(e);}}}
<deployment xmlns="http://xml.apache.org/Axis/wsdd/"xmlns:java="http://xml.apache.org/Axis/wsdd/providers/java"><handler name="print" type="java:LogHandler"/><service name="sayhello"provider="java:RPC"><requestFlow><handler type="print"/></requestFlow><parameter name="className"value="server.SayHello"/><parameter name="allowedMethods"value="*"/></service></deployment>
java org.apache.Axis.wsdl.WSDL2Java-p client http://localhost:8080/Axis/services/SayHello.jws?wsdl
java org.apache.Axis.wsdl.Java2WSDL-oSayHello.wsdl -lhttp://localhost:8080/Axis/services/SayHello -nsayhello server.SayHello执行如下命令生成client stub
public class SayHelloClient{public static void main(String[] args){try{SayHelloService service = new client.SayHelloServiceLocator();client.SayHello_PortTypeclient = service.getSayHello();String retValue=client.getName("zhangsan");System.out.println(retValue);}catch (Exception e){System.err.println("Execution failed. Exception: " + e);}}}