七、axis调用RMI模式WebService

和先前写的web service几乎一样,不同的是这里写的WebService要实现接口,而我们实现的接口要继承rmi的Remote接口才行。

1、RMI接口,很简单的2个方法,继承Remote接口

 

代码
package com.hoo.rmi;

import java.rmi.Remote;
/**
* <b>function:</b>用axis实现RMI调用
*
@author hoojo
* @createDate Dec 16, 2010 16:22:25 PM
* @file HelloRMI.java
* @package com.hoo.rmi
* @project AxisWebService
* @blog
http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
*
@version 1.0
*/
public interface HelloRMI extends Remote {
public String sayHello(String name);

public int getAge(int age);
}

 

 

2、实现上面的接口

 

代码
package com.hoo.rmi.impl;

import com.hoo.rmi.HelloRMI;

/**
* <b>function:</b>实现RMI服务器接口
*
@author hoojo
* @createDate Dec 16, 2010 16:23:15 PM
* @file HelloRMIImpl.java
* @package com.hoo.rmi.impl
* @project AxisWebService
* @blog
http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
*
@version 1.0
*/
public class HelloRMIImpl implements HelloRMI {

public int getAge(int age) {
return age + 10;
}

public String sayHello(String name) {
return name + " say: hello rmi WebService !";
}
}

 

 

 

3、定制wsdd发布服务

 

代码
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java
="http://xml.apache.org/axis/wsdd/providers/java">
<service name="HelloRMI" provider="java:RPC">
<parameter name="className" value="com.hoo.rmi.impl.HelloRMIImpl" />
<parameter name="allowedMethods" value="getAge,sayHello" />
<parameter name="scope" value="request" />
</service>
</deployment>

 

 

 

4、命令行发布服务

java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8080/AxisWebService/services/AdminService deployRMI.wsdd

在浏览器中输入:

http://localhost:8080/AxisWebService/servlet/AxisServlet

即可查看到刚才发布的服务

 

5、编写客户端调用代码

 

代码
package com.hoo.client;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import com.hoo.rmi.HelloRMI;
/**
* <b>function:</b>axis WebService的RMI调用模式
*
@author hoojo
* @createDate Dec 16, 2010 16:28:09 PM
* @file HelloRMIClient.java
* @package com.hoo.client
* @project AxisWebService
* @blog
http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
*
@version 1.0
*/
public class HelloRMIClient {

public static void main(String[] args) throws ServiceException, RemoteException {
String url
= "http://localhost:8080/AxisWebService/services/HelloRMI";
//axis call调用
Service service = new Service();
Call call
= (Call) service.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(
new QName(url, "sayHello"));
String result
= (String) call.invoke(new Object[] { "jack" });
System.out.println(result);

//远程接口模式,只管接口不管实现,这就是面向接口的好处
HelloRMI rmi = (HelloRMI) service.getPort(url, HelloRMI.class);
System.out.println(rmi.getAge(
22));
}
}

 

 

HelloRMI rmi = (HelloRMI) service.getPort(url, HelloRMI.class);

用rmi接口调用的还是比较简单明了,接口中的方法都一目了然。我们也不用操心接口里面的方法是怎么样实现的,更具有面向接口编程、更加透明化。

posted on 2010-12-20 14:30  hoojo  阅读(3227)  评论(0编辑  收藏  举报