`
ywChen
  • 浏览: 118104 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Spring3 RMI 实现

阅读更多

1.远程接口定义

package org.spring.rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;

/**
 * 远程接口继承Remote
 * @author chenyw
 *
 */
public interface UserService extends Remote{
	/**
	 * 返回用户列表
	 * @return
	 * @throws RemoteException
	 */
	public List<User> getUsers()throws RemoteException;;
}

 

 2.远程接口实现

package org.spring.rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;

/**
 * 远程接口继承Remote
 * @author chenyw
 *
 */
public interface UserService extends Remote{
	/**
	 * 返回用户列表
	 * @return
	 * @throws RemoteException
	 */
	public List<User> getUsers()throws RemoteException;;
}

 

3.实体实现序列化接口

package org.spring.rmi;

import java.io.Serializable;

/**
 * user 序列化
 * @author chenyw
 *
 */
public class User implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public User(String id,String name){
		this.id= id;
		this.name= name;
	}
	private String id;
	private String name;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

 

 4.spring服务端配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd">
		
	<bean id="userService" class="org.spring.rmi.UserServiceImpl">
	   <!-- any additional properties, maybe a DAO? -->
	</bean>
    
    <!-- rmi服务端bean -->
	<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
		<property name="serviceName" value="userService" />
		<property name="service" ref="userService" />
		<property name="serviceInterface"
			value="org.spring.rmi.UserService" />
		<!-- 端口默认为1099 -->
		<property name="registryPort" value="1199" />
	</bean>

	<!-- rmi客户端bean -->
	<bean id="rmiService"
		class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
		<property name="serviceInterface">
			<value>org.spring.rmi.UserService</value>
		</property>
		<property name="serviceUrl">
			<value>rmi://localhost:1199/userService</value>
		</property>
	</bean>
</beans>

 

 由于客户端与服务端都在同一个项目,所以也把客户端的配置写入了,也可以将客户端和服务端分开,写成两个项目,这里就不在详细说明了。如果分开项目请注意(服务器端的接口一定要拷贝一份到客户端,而且包名一定要相同

 

5.客户端调用远程接口

package org.spring.rmi;

import java.rmi.RemoteException;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RmiClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(  
		"spring_rmi.xml");  
		UserService service=(UserService)context.getBean("rmiService");  

		try {  
			List<User> user = service.getUsers();
		    for (User u : user) {
				System.out.println(u.getName());
			}
		} catch (RemoteException e) {  
		// TODO Auto-generated catch block  
		e.printStackTrace();  
		}  
	}

}

 

 (1)将项目部署到tomcat启动

 (2)执行RmiClient 程序即可远程调用

 

运行结果

 

yuwen1
yuwen2
yuwen3
yuwen4
yuwen5

 

如有spring架构用到的包或web.xml不明之处请参照

 

Spring3 MVC 学习笔记(一) 入门

 

本人开了个充值淘宝网店。有需要的朋友请访问的店铺并拍下所充值的话费,

本店已加入消费保障服务计划,货源来源于淘宝充值平台,安全可靠便捷,

支付过后立即到账

http://xiaowen168.taobao.com

 

 

分享到:
评论
1 楼 kobe1700 2011-06-09  
如果上面需要添加安全呢?有没有带安全的例子?

相关推荐

Global site tag (gtag.js) - Google Analytics