`
fengchong719
  • 浏览: 80926 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring - RMI

阅读更多
如果你的RMI的实现里,需要用到Spring IOC&DI,那么你就需要改改你的Code了。

当然了,这里说的是实现类(Stub)类,接口是没有什么问题的只需要继承自remote就行了,需要注意的就是每个方法都要throws RemoteException;
服务端接口:
public interface IDemoServer extends Remote {
 
    /**
     * 查询客户信息
     * @param user 客户信息
     * @return
     */
    List<User> queryUserInfo(User user)throws RemoteException;

}


服务端实现:(需要注意的是必须显示的书写构造方法,还有就是他不能再继承自UnicastRemoteObject类了,因为它会抛出其它异常。)
@Component
public class DemoRMIServer implements IDemoRMIServer {
    @Autowiredv
    private UserService userService;
    

    public DemoRMIServer() {
        super();
    }
    
    /**
     * 查询客户拥有的安全载体信息
     * @return
     */
    public List<User> queryUserInfo(User user)throws RemoteException {
        return userService.queryUserInfo(user);
    }

}

配置文件(Spring):
<bean id="m3AdminRMI" class="org.springframework.remoting.rmi.RmiServiceExporter">
		<property name="serviceName" value="Demo" />
		<property name="service" ref="demoRMIServer" />
		<property name="serviceInterface" value="com.demo.IDemoRMIServer " />
		<!-- <property name="registryHost" value="localhost"/> -->
		<property name="registryPort" value="10999" />
	</bean>


这样服务端的代码就完成了。由于使用了Spring3.0的MVC模式,所以,这里DemoRMIServer 被加了 annotation @Component,所以他也会被Spring 当做一个Bean来实例化,按照规范他会被命名为你的类名,第一字母小写。

现在是客户端:
首先我们要做的就是配置(Spring):
<bean id="rmiService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
		<property name="serviceUrl" value="rmi://127.0.0.1:10999/Demo"></property>
		<property name="serviceInterface" value="com.demo.IDemoRMIServer"></property>
	</bean>


然后,你随便写一个Java类,rmiServer注入进来就行。
public class RMIClientTest {
    
    @Autowired
    private RmiProxyFactoryBean rmiServer;
    

    public void testRMIClient(){
        User user=new User();
        user.setContactName("Yy");
        user.setIdentifierType("1");
        user.setIdentifierNumber("56");
        try {
            IDemoRMIServer server = (IDemoRMIServer )rmiServer.getObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String [] args) {
        FileSystemXmlApplicationContext fsxac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
        System.out.println("init ...");
        IDemoRMIServer service = (IDemoRMIServer )fsxac.getBean("rmiService");
        System.out.println("start service ..."); 
        User user=new User();
        user.setContactName("Yy");
        user.setIdentifierType("01");
        user.setIdentifierNumber("056");
        try {
            System.out.println("start query ...");
            List<User> users= service.queryUserInfo(user);
            for (User user: users) {
                System.out.println(user.getContactName() + ">>>" + user.getPhone() + ">>>" );
            }
            System.out.println(" ... <<");
        } catch (RemoteException e1) {
            e1.printStackTrace();
        }
    }
}

Spring - RMI 就是这么简单,上面提供了两种方式,下面那种方试用来测试的(main方法)。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics