Spring使用RMI非常简单,甚至比直接使用RMI还简单。

[b]第一:首先定义接口:[/b]

import java.rmi.Remote;
import java.rmi.RemoteException;

import com.zakisoft.test.rmi.vo.User;

public interface RmiUserService extends Remote{
	User login(String username, String password) throws RemoteException;
	void create(String username, String password) throws RemoteException;
}



import java.io.Serializable;

public class User implements Serializable {

	private static final long serialVersionUID = -3017748605531466285L;
	private String username;
	private String password;

	public User(String username, String password) {

		this.username = username;
		this.password = password;

	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

}




[b]第二:实现接口[/b]


import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

import com.zakisoft.test.rmi.server.UserService;
import com.zakisoft.test.rmi.server.UserServiceImpl;
import com.zakisoft.test.rmi.vo.User;

public class RmiUserServiceImpl extends UnicastRemoteObject implements
		RmiUserService {

	UserService service = new UserServiceImpl();

	public RmiUserServiceImpl() throws RemoteException {
	}

	private static final long serialVersionUID = -7138363701504022490L;

	public void create(String username, String password) throws RemoteException {
		service.create(username, password);
	}

	public User login(String username, String password) throws RemoteException {
		return service.login(username, password);
	}

	public static void main(String[] args) throws RemoteException, MalformedURLException, AlreadyBoundException {
		LocateRegistry.createRegistry(1099);
		Naming.bind("rmi://localhost:1099/UserService", new RmiUserServiceImpl());
		System.out.print("Service start");
	}

}




[b]第三:在Spring配置文件中配置RMI服务[/b]


<bean id="userService" class="com.zakisoft.test.rmi.server.UserServiceImpl"></bean>
    <bean id="rmiService" class="org.springframework.remoting.rmi.RmiServiceExporter">
    	<property name="serviceName" value="UserService"/>
    	<property name="service" ref="userService"/>
    	<property name="serviceInterface" value="com.zakisoft.test.rmi.server.UserService"/>
    	<property name="registryPort" value="1099"/>
    </bean>




[b]第四:启动RMI服务[/b]


public class SpringStartRmi {

	public static void main(String[] args) {
		new ClassPathXmlApplicationContext("zconfig.xml");
	}

}




[b]第五:访问服务[/b]


import org.springframework.remoting.rmi.RmiProxyFactoryBean;

import com.zakisoft.test.rmi.server.UserService;

public class ClientSpringRmi {

	public static void main(String[] args) {
		RmiProxyFactoryBean factory = new RmiProxyFactoryBean();
		factory.setServiceInterface(UserService.class);
		factory.setServiceUrl("rmi://localhost:1099/UserService");
		factory.afterPropertiesSet();

		UserService userService = (UserService)factory.getObject();
		userService.create("test", "test");
		System.out.println(userService.login("new_user_01", "new_password_01"));	
		//userService.login("test", "test");
	}
}




[b]第六:源码下载[/b]


[url]http://www.zakisoft.com/?p=118[/url]