Chinaunix首页 | 论坛 | 博客
  • 博客访问: 944744
  • 博文数量: 264
  • 博客积分: 10107
  • 博客等级: 上将
  • 技术积分: 2455
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-09 16:34
文章分类

全部博文(264)

文章存档

2012年(1)

2011年(11)

2010年(128)

2009年(82)

2008年(42)

我的朋友

分类: 系统运维

2010-07-20 23:58:13

Axis部署开发webservice (全)

Axis支持三种web service的部署和开发,分别为:
1、Dynamic Invocation Interface(DII)
2、Dynamic Proxy方式
3、Stubs方式
其中:约定Axis_HOME为该TOMCAT_HOME\webapps\Axis目录
在eclipse环境下测试的时候,客户端都应该是web project.
一、编写DII(Dynamic Invocation Interface )方式web服务
1.编写服务端程序HelloClient
public class HelloClient { public String getName(String name) { return "hello "+name; } }



2、将源码拷贝到Axis_HOME下,重命名为 HelloClient.jws
3、访问连接,页面显示Axis自动生成的wsdl
4、编写访问服务的客户端 TestHelloClient.java
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 = ""; Service service = new Service(); Call call = null; call = (Call) service.createCall(); call.setOperationName(new QName( "", "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(); } } }



二、编写Dynamic Proxy方式访问服务
1、编写部署服务端程序,同上边DII方式,本次仍使用上边部署的HelloClient
2、编写代理接口
public interface HelloClientInterface extends java.rmi.Remote { public String getName(String name) throws java.rmi.RemoteException; }



3、编写并执行客户端程序TestHelloClient.java
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 = ""; String nameSpaceUri = ""; 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() ; } } }



三、编写wsdd发布web服务,编写stub client访问web服务
1、编写服务端程序server,SayHello.java,编译server.SayHello.java
package server; public class SayHello { public String getName(String name) { return "hello "+name; } } 2.编写LogHandler.java import 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 LogHandler extends BasicHandler { public void invoke (MessageContext msgContext) throws AxisFault { /** Log an access each time we 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); } } }



3、编写wsdd文件
deploy.wsdd



3、将编译后的文件拷贝到Axis_HOME/WEB-INF/classes下,如:D: \tomcat\webapps\Axis\WEB-INF\classes
4、发布服务:
java org.apache.Axis.client.AdminClient deploy.wsdd
5、生成client stub文件
a:方式1
将SayHello.java拷贝到Axis_HOME/下,重命名为SayHello.jws,
执行下面的命令生存client stub
java org.apache.Axis.wsdl.WSDL2Java -p client /Axis/services/SayHello.jws?wsdl




b:方式2
执行如下命令生成SayHello.wsdl
java org.apache.Axis.wsdl.Java2WSDL -oSayHello.wsdl -l /Axis/services/SayHello -nsayhello server.SayHello



执行如下命令生成client stub
java org.apache.Axis.wsdl.WSDL2Java SayHello.wsdl -p client



生成的stub client文件列表为:
1.SayHello.java
2.SayHelloService.java。
3.SayHelloServiceLocator.java
4.SayHelloSoapBindingStub.java
6、编写客户端程序,编译并执行
public class SayHelloClient { public static void main(String[] args) { try { SayHelloService service = new client. SayHelloServiceLocator(); client.SayHello_PortType client = service.getSayHello(); String retValue=client.getName("zhangsan"); System.out.println(retValue); } catch (Exception e) { System.err.println ("Execution failed. Exception: " + e); } } }
阅读(868) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~