说说WebService和RMI的协作。我们先来看这个需求的模型是怎么样的。看下图
3.3我们可以先写个RMI的Client 测试一下
3.6实现WebService的客户端
首先cd到src目录,然后敲wsimport -keep http://localhost:8435/MyTimeServer?wsdl 这个会自动生成一堆代码,基本上可以理解为stub一类的东西
然后我们就可以直接调用啦
系统要求有Smart device对远程的JVM2里的程序控制,所以我们引入了WebService,通过WebService间接调用JVM2里的东西,WebSerivce本身运行在JVM1,JVM1和JVM2可以在不同机器上,这里demo程序是在一台机器上运行的。
WebService在这里既是SmartDevice的Server端,又是RMI的Client端。RMI的Server端在JVM2的App里作为一部分服务存在。
三、代码分解,下面我们开始demo这个协作的代码
3.1 首先我们抽象一个RMI的Interface
package cn.heapstack.rmi.interfaces;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Date;
public interface IBridge extends Remote
{
public Date getDate() throws RemoteException;
public String getOtherInfo() throws RemoteException;
}
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Date;
public interface IBridge extends Remote
{
public Date getDate() throws RemoteException;
public String getOtherInfo() throws RemoteException;
}
简单的接口,一个是获得时间的,一个是获取随便什么消息的接口
3.2实现RMI的Server端
package cn.heapstack.rmi.server;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
import cn.heapstack.rmi.interfaces.IBridge;
public class BriderImpl extends UnicastRemoteObject implements IBridge
{
protected BriderImpl() throws RemoteException
{
super ();
}
private static final long serialVersionUID = - 2194578051853930272L ;
public Date getDate() throws RemoteException
{
return new Date( 2008 , 5 , 10 );
}
public String getOtherInfo() throws RemoteException
{
return " This is the message from the jvm, path info: " + BriderImpl.class .getResource( " / " ).getPath();
}
}
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
import cn.heapstack.rmi.interfaces.IBridge;
public class BriderImpl extends UnicastRemoteObject implements IBridge
{
protected BriderImpl() throws RemoteException
{
super ();
}
private static final long serialVersionUID = - 2194578051853930272L ;
public Date getDate() throws RemoteException
{
return new Date( 2008 , 5 , 10 );
}
public String getOtherInfo() throws RemoteException
{
return " This is the message from the jvm, path info: " + BriderImpl.class .getResource( " / " ).getPath();
}
}
package cn.heapstack.rmi.server;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import cn.heapstack.rmi.interfaces.IBridge;
public class RmiServer
{
public static void main(String[] args)
{
try
{
LocateRegistry.createRegistry( 1099 );
IBridge bridge = new BriderImpl();
Naming.rebind( " bridge " , bridge);
System.out.println( " RMI Server is ready. " );
}
catch (Exception e)
{
System.out.println( " RMI Server failed: " + e);
}
}
}
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import cn.heapstack.rmi.interfaces.IBridge;
public class RmiServer
{
public static void main(String[] args)
{
try
{
LocateRegistry.createRegistry( 1099 );
IBridge bridge = new BriderImpl();
Naming.rebind( " bridge " , bridge);
System.out.println( " RMI Server is ready. " );
}
catch (Exception e)
{
System.out.println( " RMI Server failed: " + e);
}
}
}
3.3我们可以先写个RMI的Client 测试一下
package cn.heapstack.rmi.client;
import java.rmi.Naming;
import cn.heapstack.rmi.interfaces.IBridge;
public class RmiClient
{
/**
* @param args
*/
public static void main(String[] args)
{
IBridge bridge;
try
{
bridge = (IBridge) Naming.lookup( " bridge " );
System.out.println( bridge.getDate() );
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
import java.rmi.Naming;
import cn.heapstack.rmi.interfaces.IBridge;
public class RmiClient
{
/**
* @param args
*/
public static void main(String[] args)
{
IBridge bridge;
try
{
bridge = (IBridge) Naming.lookup( " bridge " );
System.out.println( bridge.getDate() );
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
3.4我们开始实现我们的WebService Server端
Java6.0实现WebService也很简单,我直接贴代码了。Server端如下,如果不太懂,还是建议看后面的参考资料
package cn.heapstack.ws.server;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Date;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import cn.heapstack.rmi.interfaces.IBridge;
@WebService(targetNamespace = "http://www.heapstack.cn/ws/MyTimeServer")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class MyTimeServer
{
@WebMethod
public String getServerTime()
{
//通常的做法
//return new Date(System.currentTimeMillis()).toString();
//这里我想要调用RMI,取得另一个JVM中程序调用的结果,当然这个JVM可以在两外一台机器上运行
//这里WebService的Server 其实是RMI的 Client
Date date = null;
try
{
IBridge bridge = (IBridge) Naming.lookup("bridge");
date = bridge.getDate();
return date.toString();
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}
发布WebService
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Date;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import cn.heapstack.rmi.interfaces.IBridge;
@WebService(targetNamespace = "http://www.heapstack.cn/ws/MyTimeServer")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class MyTimeServer
{
@WebMethod
public String getServerTime()
{
//通常的做法
//return new Date(System.currentTimeMillis()).toString();
//这里我想要调用RMI,取得另一个JVM中程序调用的结果,当然这个JVM可以在两外一台机器上运行
//这里WebService的Server 其实是RMI的 Client
Date date = null;
try
{
IBridge bridge = (IBridge) Naming.lookup("bridge");
date = bridge.getDate();
return date.toString();
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}
package cn.heapstack.ws.server;
import javax.xml.ws.Endpoint;
import cn.heapstack.ws.server.MyTimeServer;
public class Main
{
public static void main(String[] args)
{
Endpoint.publish("http://localhost:8435/MyTimeServer", new MyTimeServer());
}
}
import javax.xml.ws.Endpoint;
import cn.heapstack.ws.server.MyTimeServer;
public class Main
{
public static void main(String[] args)
{
Endpoint.publish("http://localhost:8435/MyTimeServer", new MyTimeServer());
}
}
3.6实现WebService的客户端
首先cd到src目录,然后敲wsimport -keep http://localhost:8435/MyTimeServer?wsdl 这个会自动生成一堆代码,基本上可以理解为stub一类的东西
然后我们就可以直接调用啦
package
cn.heapstack.ws.client;
import cn.heapstack.ws.mytimeserver.* ;
public class WSClient
{
/**
* @param args
*/
public static void main(String[] args)
{
//自动生成的代码 WebService的Stub
MyTimeServerService ws = new MyTimeServerService();
//自动生成的代码 WebService的实例类
MyTimeServer service = ws.getMyTimeServerPort();
//调用WebService,WebSerivce会调用RMI的方法获得返回结果
String date = service.getServerTime();
System.out.println("WS Client get:"+ date );
}
}
import cn.heapstack.ws.mytimeserver.* ;
public class WSClient
{
/**
* @param args
*/
public static void main(String[] args)
{
//自动生成的代码 WebService的Stub
MyTimeServerService ws = new MyTimeServerService();
//自动生成的代码 WebService的实例类
MyTimeServer service = ws.getMyTimeServerPort();
//调用WebService,WebSerivce会调用RMI的方法获得返回结果
String date = service.getServerTime();
System.out.println("WS Client get:"+ date );
}
}