Simple remote framework core: the basic concept in the Java class library
Simple remote framework core: the basic concept in the Java class library
Remote calls are one of the common needs in modern distributed systems.Java provides developers with rich class libraries and frameworks to achieve remote calling functions.This article will introduce some basic concepts in the Java class library to help readers understand the core concepts and usage methods of Simple Remote Framework.
1. RMI (remote method call): RMI is one of the most basic remote call mechanisms in the Java class library.It allows programmers to call remote methods between different Java virtual machines, just like calling local methods.RMI uses Java object serialization to transmit objects between clients and servers.
The following is a simple RMI example:
Server side code:
public interface HelloService extends Remote {
String sayHello() throws RemoteException;
}
public class HelloServiceImpl extends UnicastRemoteObject implements HelloService {
public HelloServiceImpl() throws RemoteException {
super();
}
@Override
public String sayHello() throws RemoteException {
return "Hello, World!";
}
}
public class Server {
public static void main(String[] args) {
try {
HelloService service = new HelloServiceImpl();
Naming.rebind("hello", service);
System.out.println("Server started.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Client code:
public class Client {
public static void main(String[] args) {
try {
HelloService service = (HelloService) Naming.lookup("rmi://localhost/hello");
System.out.println(service.sayHello());
} catch (Exception e) {
e.printStackTrace();
}
}
}
2. RMI-IIOP: RMI-IIOP is an upgraded version of RMI, using Corba's IIOP (Internet Inter-ORB Protocol) as a communication protocol.It provides a stronger remote call function that can communicate with other CORBA compatible platforms.
The following is a simple RMI-IIIP example:
Server side code:
public interface HelloService extends Remote {
String sayHello() throws RemoteException;
}
public class HelloServiceImpl extends PortableRemoteObject implements HelloService {
public HelloServiceImpl() throws RemoteException {
super();
}
@Override
public String sayHello() throws RemoteException {
return "Hello, World!";
}
}
public class Server {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
props.setProperty("java.naming.provider.url", "iiop://localhost:900/NameService");
Context context = new InitialContext(props);
HelloService service = new HelloServiceImpl();
context.rebind("HelloService", service);
System.out.println("Server started.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Client code:
public class Client {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
props.setProperty("java.naming.provider.url", "iiop://localhost:900/NameService");
Context context = new InitialContext(props);
HelloService service = (HelloService) context.lookup("HelloService");
System.out.println(service.sayHello());
} catch (Exception e) {
e.printStackTrace();
}
}
}
3. Hessian: Hessian is a lightweight cross -platform RPC (remote process call) framework.It uses a simple binary serialization protocol to support Java and other languages.
The following is a simple hessian example:
Server side code:
public interface HelloService {
String sayHello();
}
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello() {
return "Hello, World!";
}
}
public class Server {
public static void main(String[] args) {
try {
HelloService service = new HelloServiceImpl();
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setService(service);
exporter.setServiceInterface(HelloService.class);
exporter.setServiceUrl("/HelloService");
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
HttpContext context = server.createContext("/");
context.setHandler(exporter);
server.start();
System.out.println("Server started.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Client code:
public class Client {
public static void main(String[] args) {
try {
HessianProxyFactory factory = new HessianProxyFactory();
HelloService service = (HelloService) factory.create(HelloService.class, "http://localhost:8080/HelloService");
System.out.println(service.sayHello());
} catch (Exception e) {
e.printStackTrace();
}
}
}
This article introduces three common remote call frameworks in the Java class library: RMI, RMI-IIOP, and hessian.Readers can choose the appropriate framework according to their needs to implement the remote call function in a distributed system.The above example code is for reference only, and readers should be modified appropriately according to the specific situation.