JavaRMI在网上的教程和
例子很多,很多都提到了创建存根(Sub),但是我一直写RMI都不需要创建存根,想问下大家在程序中怎么做的.
接口和实现类没什么特别的.
package net.topect.remoting;
import java.io.Serializable;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface IHello
Service extends Remote, Serializable {
public String sayHello(String name) throws RemoteException;
}
/**
*
*/
package net.topect.remoting;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
/**
* @author ZhenQin
*
*/
public class HelloServiceImpl
extends UnicastRemoteObject
implements IHelloService, Serializable {
/**
*
*/
private static final long serialVersionUID = 8052974676662502712L;
/**
*
*/
public HelloServiceImpl() throws RemoteException {
System.out.println("******************************");
}
/* (non-Javadoc)
* @see net.topect.remoting.IHelloService#sayHello(java.lang.String)
*/
@Override
public String sayHello(String name) throws RemoteException {
System.out.println("Server: Hello, " + name);
return "Hello, " + name;
}
}
RMI服务器端.
/**
*
*/
package net.topect.remoting;
import java.rmi.AlreadyBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import net.neces.util.NecesUtils;
/**
* <p>
* Rmi服务启动类
* </p>
* net.neces.run.RmiServerService
*
* @author ZhenQin
*
*/
public class RmiServerService {
/**
* RMI默认的
服务端口号
*/
public final static int DEFAULT_RMI_PORT = 1099;
/**
* Rmi端口号,当程序在执行init方法前更改有效
*/
private int rmiPort = DEFAULT_RMI_PORT;
/**
* 构造方法
*/
public RmiServerService() {
}
/**
* 执行默认的初始化,构造对象后,执行该方法有效.
* 在执行该方法前可以更改服务的端口号
*/
public void init() {
System.setProperty ("java.security.policy",
RmiServerService.class.getResource ("rmiserver.policy").getFile());
if(System.getSecurityManager() == null){
System.setSecurityManager(new RMISecurityManager());
}
Registry registry = null;
try {
registry = LocateRegistry.createRegistry(rmiPort);
System.err.println("RMI服务已经在 "+ rmiPort + " 号端口启动!");
} catch (RemoteException e) {
e.printStackTrace();
}
try {
registry.bind("helloService", new net.topect.remoting.HelloServiceImpl());
} catch (java.rmi.RemoteException e) {
e.printStackTrace();
} catch (AlreadyBoundException e) {
e.printStackTrace();
}
}
public void setRmiPort(int rmiPort) {
this.rmiPort = rmiPort;
}
public int getRmiPort() {
return rmiPort;
}
/**
* RmiServerService程序入口
* @param args
*/
public static void main(String[] args) {
RmiServerService rmiServerService = new RmiServerService();
rmiServerService.init();
}
}
客户端.
/**
*
*/
package net.topect.remoting;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* @author ZhenQin
*
*/
public class RmiClientService {
/**
*
*/
public RmiClientService() {
}
/**
* @param args
*/
public static void main(String[] args) {
System.setProperty ("java.security.policy",
RmiServerService.class.getResource ("rmiserver.policy").getFile());
if(System.getSecurityManager() == null){
System.setSecurityManager(new RMISecurityManager());
}
Context cxt = null;
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
props.put(Context.PROVIDER_URL, "rmi://localhost:1099");
try {
cxt = new InitialContext(props);
} catch (NamingException e) {
e.printStackTrace();
}
try {
IHelloService helloService = (IHelloService)
cxt.lookup("helloService");
System.out.println(helloService.sayHello("JNDI, Frends"));
} catch (Exception e) {
e.printStackTrace();
}
try {
IHelloService helloService = (IHelloService)
Naming.lookup("rmi://localhost:1099/helloService");
System.out.println(helloService.sayHello("Rmi, Frends"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
rmiserver.policy
grant{
permission java.security.AllPermission;
};
运行输出.
服务器端:
RMI服务已经在 1099 号端口启动!
******************************
Server: Hello, Frends
Server: Hello, Frends
客户端:
Hello, JNDI, Frends
Hello, Rmi, Frends
希望各位给个见解,本人在做一个基于RMI-JNDI分布式应用.大概的思路就像SpringRMI一样,普通的POJO类也可以实现
远程绑定,不一定要实现Remote接口等.