1.JMI开发背景:java的原生RPC方案,现阶段想学习一下,但是碰到以下问题,还未解决,希望知道怎么解决的给提个方案。
开发逻辑分为四部分:
1. 继承java.rmi.Remote接口提供抽象方法;
2. 继承UnicastRemoteObject,实现Remote接口;
3. 提供服务端程序
4. 提供客户端程序
1. 继承Remote接口提供抽象方法
package com.company;
import java.rmi.Remote;
public interface IRService extends Remote {
String service(String content) throws Exception;
}
2. 继承UnicastRemoteObject,实现Remote接口;
package com.company;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class ServiceImpl extends UnicastRemoteObject implements IRService {
private String name;
protected ServiceImpl(String name) throws RemoteException {
this.name = name;
}
@Override
public String service(String content) throws Exception {
return "server +"+content;
}
}
3. 提供服务端程序
package com.company;
import javax.naming.Context;
import javax.naming.InitialContext;
public class Server {
public static void main(String[] args) {
try{
IRService irService = new ServiceImpl("service02");
Context initialContext = new InitialContext();
initialContext.rebind("rmi:127.0.0.1/service02",irService);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("000000!");
}
}
4.