A Lightweight Implementation of Java RMI - LipeRMI
Previously, the blogger recorded about the RMI of the Java standard library, but later found that there were many problems, especially when it was directly banned on the Android side, so he turned to the third-party LipeRMI
I noticed that there are relatively few Chinese tutorials on LipeRMI. Here is a demo for my review. It would be better if it can help those in need.
LipeRMI Advantages
- Provides a simple, extensible framework and API
- Very lightweight and does not depend on any third-party packages
- Safe and reliable
- Provides an API similar to RMI, allowing programs to replace RMI with LipeRMI with only a few tweaks
- Optimize bandwidth usage
- Optimize client-server communication (reuse the same socket and keep alive)
- When a connection-oriented event occurs, a predetermined action is triggered
Architecture
Engineering structure
➜ hellormi tree
.
├── HelloClient.java
├── HelloImpl.java
├── HelloServer.java
└── IHello.java
0 directories, 4 files
public interface declaration
IHello.java
package hellormi;
import java.util.Date;
public interface IHello {
String sayHello(String name);
Date getDate();
}
Server implementation class
HelloImpl.java
package hellormi;
import java.util.Date;
public class HelloImpl implements IHello {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
@Override
public Date getDate() {
return new Date();
}
}
The server opens the service main class
HelloServer.java
package hellormi;
import net.sf.lipermi.exception.LipeRMIException;
import net.sf.lipermi.handler.CallHandler;
import net.sf.lipermi.net.Server;
import java.io.IOException;
public class HelloServer {
public static void main(String[] args) {
// 远程实例
IHello proxy = new HelloImpl();
CallHandler callHandler = new CallHandler();
// 注册服务
try {
callHandler.registerGlobal(IHello.class, proxy);
} catch (LipeRMIException e) {
e.printStackTrace();
}
Server server = new Server();
// 绑定端口和服务
int port = 8888;
try {
server.bind(port, callHandler);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client calling class
HelloClient.java
package hellormi;
import net.sf.lipermi.handler.CallHandler;
import net.sf.lipermi.net.Client;
import java.io.IOException;
public class HelloClient {
public static void main(String[] args) {
// 建立连接
CallHandler callHandler = new CallHandler();
String remoteHost = "127.0.0.1";
int port = 8888;
Client client = null;
try {
client = new Client(remoteHost, port, callHandler);
} catch (IOException e) {
e.printStackTrace();
}
// 获取远程实例,测试方法
assert client != null;
IHello remoteObj = (IHello) client.getGlobal(IHello.class);
System.out.println(remoteObj.sayHello("Tomcat"));
System.out.println(remoteObj.getDate());
}
}
Test Results
Start the service on the server first, then run HelloClient.java locally
Hello Tomcat
Wed May 02 19:39:15 HKT 2018
summary
The experience of using LipeRMI is similar to that of rmi in the Java standard library, but it has less dependencies and is easy to port. It is also relatively lightweight and independent, and there is no need to perform operations like setting the system ip with native rmi.