RMI remote method invocation

Please reprint from the source: http://eksliang.iteye.com/blog/2267510

I. Overview

Java RMI refers to Remote Method Invocation. It is a mechanism that enables an object on one Java virtual machine to call a method on an object in another Java virtual machine. Any object that can be called with this method must implement this remote interface. Java RMI is not a new technology. It was available in the era of Java 1.1. The famous EJB was built on the basis of rmi. Now there are some open source remote call components whose underlying technology is also rmi.

 In the era of vigorously advocating Web Service and SOA, should every application be implemented with clumsy Web Service components? After the comparison test, RMI is the simplest, and it is the most suitable for some small applications.

However, RMI has the following two limitations

  • RMI closely depends on the IP address and port of the server, but it does not know the future server IP and port during development, but the client program depends on this IP and port.
  • RMI is a remote call of Java language, and the programming language at both ends must be implemented in Java.

 

2. Use RMI to publish services

2.1. Define the RMI service interface UserService class

 

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;
import com.gosun.jws.daomain.Users;

/**
 * <pre>
 * The most primitive RMI remote method call,
 * The publishing interface must inherit the Remote interface for identification
 * All methods of this interface must throw throws RemoteException
 * </pre>
 * @author Ickes
 */
public interface UserService extends Remote {
	/**
	 * test return list
	 *
	 * @return
	 */
	public List<Users> geAlltUsers() throws RemoteException;

	/**
	 * Test return entity, and pass common parameters
	 *
	 * @param id
	 * @return
	 */
	public Users getUser(String id) throws RemoteException;

	/***
	 * Test incoming object
	 *
	 * @param user
	 */
	public void save(Users user)throws RemoteException;

	/**
	 * Test successor collection
	 *
	 * @param users
	 */
	public void saves(List<Users> users) throws RemoteException;
}

 

2.2, UserService service implementation class

 

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;
import com.gosun.jws.daomain.Users;
import com.gosun.jws.daomain.UsersFactory;

/**
 * Implementation interface of UserService interface
 * @author Ickes
 */
public class UserServiceImpl extends UnicastRemoteObject implements UserService{
	private static final long serialVersionUID = 1L;
	protected UserServiceImpl() throws RemoteException {
		super();
	}

	@Override
	public List<Users> geAlltUsers() {
		return UsersFactory.getUsers();
	}

	@Override
	public Users getUser(String id) {
		System.out.println(id);
		return UsersFactory.getUser();
	}

	@Override
	public void save(Users user) {
		System.out.println(user.toJson());
	}

	@Override
	public void saves(List<Users> users) {
		for (Users u : users) {
			System.out.println(u.toJson());
		}
	}

}

   Test used: UsersFactory tool class and Users entity class in: http://eksliang.iteye.com/blog/2265021 article 3.1 and 3.2 code

 

 

2.3. Start the RMI service and register

 

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

/**
 * The server-side code written in pure Java is as follows
 * @author Ickes
 *
 */
public class ServiceDemo {
	public static void main(String[] args) {
		try {
			UserService userService = new UserServiceImpl();
			//Register the communication port, the default port is 1099
			LocateRegistry.createRegistry(1099);
			//Register the communication path
			Naming.rebind("rmi://127.0.0.1:1099/UserService", userService);
		} catch (RemoteException e) {
			e.printStackTrace ();
		} catch (MalformedURLException e) {
			e.printStackTrace ();
		}

	}
}

 

 

3. Client call

 

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.List;
import com.gosun.jws.daomain.Users;

/**
 * Pure JAVA client calling code
 * @author Ickes
 */
public class ClientDemo {
	public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException {
		 UserService us = (UserService) Naming.lookup("rmi://127.0.0.1:1099/UserService");
		 Users user = us.getUser("a001");
			System.out.println(user.toJson());
			System.out.println("----------------------------------------");
			List<Users> users = us.geAlltUsers();
			for (Users u : users) {
				System.out.println(u.toJson());
			}
			System.out.println("----------------------------------------");
			us.save(user);
			System.out.println("----------------------------------------");
			us.saves(users);
	}
}

 

 

四、与Spring集成发布RMI服务

4.1配置spring的RmiServiceExporter

在classpath目录下面新建applicationContext-rmi.xml文件,文件内容如下:

 

	<!-- 在Spring中装配RMI服务 -->
	<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
		<!--需要发布的实现类 -->
		<property name="service" ref="userService" />
		<!-- 注册到rmi上面的名字 -->
		<property name="serviceName" value="UserService" />
		<!-- 接口完整类名 -->
		<property name="serviceInterface" value="com.gosun.jws.rmi.UserService" />
		<!-- 默认是localhost,如果是localhost就不要填写,如果填写反而报错 
		<property name="registryHost" value="localhost" />
		-->
		<!-- 默认为1099 -->
		<property name="registryPort" value="1199" />
	</bean>
	<bean id="userService" class="com.gosun.jws.rmi.UserServiceImpl" />

 4.2发布

让spring加载applicationContext-rmi.xml这个文件便完成了对RMI服务的启动。这里不累赘!

 

 

 

五、与Spring集成调用RMI服务

5.1配置Spring的RmiProxyFactoryBean

在spring中调用RMI也非常的简单,使用Spring的RmiProxyFactoryBean,该类是一个工厂对象,可以为RMI服务创建代理。使用也非常简单,在classpath下面创建client/applicationContext-rmi.xml文件,内容如下:

 

	<!-- 使用spring调用RMI的服务 -->
	<bean id="userService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
		<property name="serviceUrl" value="rmi://localhost:1199/UserService"/>
		<property name="serviceInterface" value="com.gosun.jws.rmi.UserService" />
	</bean>

 

5.2、客户端调用

public class ClientDemo {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("client-application/applicationContext-rmi.xml");
		UserService us = (UserService) ac.getBean("userService");
		Users user = us.getUser("a001");
		System.out.println(user.toJson());
		System.out.println("----------------------------------------");
		List<Users> users = us.geAlltUsers();
		for (Users u : users) {
			System.out.println(u.toJson());
		}
		System.out.println("----------------------------------------");
		us.save(user);
		System.out.println("----------------------------------------");
		us.saves(users);
	}
}

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326271905&siteId=291194637