(一) 服务器端:域模型和对应的业务(Spring RMI):

// Account domain object
public class Account implements Serializable{
  private String name;

  public String getName();
  public void setName(String name) {
    this.name = name;
  }
}
// Account service
public interface AccountService {

  public void insertAccount(Account acc);
 
  public List getAccounts(String name);
}
// ... and corresponding implement doing nothing at the moment
public class AccountServiceImpl implements AccountService {

  public void insertAccount(Account acc) {
    // do something
  }
 
  public List getAccounts(String name) {
    // do something
  }
}
在Spring的BeanFactory中设置我们的业务:

<bean id="accountService" class="example.AccountServiceImpl">
    <!-- any additional properties, maybe a DAO? -->
</bean>

接下来,我们使用RmiServiceExporter提供我们的业务:

<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
	<!-- does not necessarily have to be the same name as the bean to be exported -->
	<!--value值给用户调用-->
         <property name="serviceName"><value>AccountService</value></property>
	<!--调用service-->
         <property name="service"><ref bean="accountService"/></property>
	<!--service接口-->
         <property name="serviceInterface"><value>example.AccountService</value></property>
	<!-- 注册端口1099 -->
	<property name="registryPort"><value>1199</value></property>
</bean>

注意:servicePort属性,它缺省值为0。这个意味着该业务使用匿名端口通讯。当然你也可以指定一个端口。

客户端连接业务:

public class SimpleObject {
  private AccountService accountService;
  public void setAccountService(AccountService accountService) {
    this.accountService = accountService;
  }
}
配置信息:

<bean class="example.SimpleObject">
<property name="accountService"><ref bean="accountService"/></bean>
</bean>

<bean id="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">

<!--AccountService是调用服务端serviceName的value -->
 <property name="serviceUrl"><value>rmi://HOST:1199/AccountService</value></property>

<!--service接口-->
<property name="serviceInterface"><value>example.AccountService</value></property>
</bean>
客户端测试:(这点是另附,实际操作时,做相应修改)

  1. import com.wym.bi.User;   
    • import com.wym.service.BaseService;   
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  3. public class ClientTest {   
  4.     public static void main(String[] args) {   
  5.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:d:/tnt_project/TEST/conf/applicationContext.xml");   //xml名称做相应调整
  6.         BaseService baseService = (BaseService) context.getBean("baseService");   
  7.         System.out.println(baseService.getHelloword("Yunmin Wu"));   
  8.         User user = new User();   
  9.         user.setName("Dan Qiao");   
  10.         user.setAge(48);   
  11.         System.out.println(baseService.getUser(user));   
  12.     }   
  13. }  

(二) Server端: 使用Hessian或Burlap通过HTTP远程调用业务:

Hessian使用一个特定的servlet来通过HTTP通讯。使用Spring的DispatcherServlet概念,你可以很容易地创建这样的servlet来提供你的业务。首先我们必须在你的应用中创建一个新的servlet(下面来自web.xml):

<servlet>
	<servlet-name>remote</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
</servlet>
				

 

你可能熟悉Spring的DispatcherServlet概念,如果是的话,你得在WEB-INF目录下建立一个应用上下文,remote-servlet.xml

在这个新的应用上下文remote-servlet.xml中,我们将创建一个HessianServiceExporter来输出你的业务:

<bean id="accountService" class="example.AccountServiceImpl">
    <!-- any additional properties, maybe a DAO? -->
</bean>

<bean name="/AccountService" class="org.springframework.remoting.caucho.HessianServiceExporter">
         <property name="serviceName"><value>AccountService</value></property>
	<property name="service"><ref bean="accountService"/></property>
	<property name="serviceInterface"><value>example.AccountService</value></property>
</bean>
				

现在我们准备在客户端连接这个业务。我们使用BeanNameUrlHandlerMapping,就不需要指定处理器映射将请求(url)映射到业务上,因此业务提供在http://HOST:8080/AccountService

客户端连接业务:

<bean class="example.SimpleObject">
    <property name="accountService"><ref bean="accountService"/></property>
</bean>

<bean id="accountService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl"><value>http://remotehost:8080/AccountService</value></property>
<property name="ServiceInterface"><value>example.AccountService</value></property>
</bean>
Burlap,它只不过是Hessian的基于XML实现。因为它和上面的Hessian的例子以相同的方式配置。只要你把Hessian替换成Burlap就可以了。

在通过Hessian或Burlap输出的业务中应用HTTP基本认证:

Hessian和Burlap的优点之一就是我们能很容易地应用HTTP认证,因为两者都是基于HTTP的协议。例如,普通的HTTP服务器安全机制可以很容易地通过使用web.xml安全功能来应用。通常,你不会为每个用户都建立不同的安全信任,而是在Hessian/Burlap的ProxyFactoryBean中定义可共享的信任(和JDBC DataSource相类似)。

 

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
	<property name="interceptors">
		<list>
			<ref bean="authorizationInterceptor"/>
		</list>
	</property>
</bean>

<bean id="authorizationInterceptor" 
	class="org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor">
	<property name="authorizedRoles">
		<list>
			<value>administrator</value>
			<value>operator</value>
		</list>
	</property>	
</bean>
				

 

这个例子中,我们用到了BeanNameUrlHandlerMapping,并设置了一个拦截器,它只允许管理员和操作员调用这个应用上下文中的bean。

注意:当然,这个例子并没有演示灵活的安全设施。如果考虑更灵活的安全设置,可以去看看Acegi Security System,http://acegisecurity.sourceforge.net

(三) 使用HTTP调用器输出业务:

 

在客户端连接业务:

从客户端连接业务与你使用Hessian或Burlap时做的类似。使用代理,Spring可以将你的调用翻译成HTTP 的POST请求到指向输出业务的URL。

	
	<bean id="httpInvokerProxy" class="org.sprfr.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
		<property name="serviceUrl">
			<value>http://remotehost:8080/AccountService</value>
		</property>
		<property name="serviceInterface">
			<value>example.AccountService</value>
		</property>
	</bean>

 

就象上面说的一样,你可以选择使用你想使用的HTTP客户端。缺省情况下,HttpInvokerPropxy使用J2SE的HTTP功能,但是你也可以通过设置httpInvokerRequestExecutor属性选择使用Commons HttpClient:

<property name="httpInvokerRequestExecutor">
	<bean class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor"/>
</property>

为业务对象设置HTTP调用器和你在Hessian或Burlap中使用的方式类似。就象Hessian提供HessianServiceExporter,Spring的HTTP调用器提供了org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter。为了输出AccountService,使用下面的配置:

    <bean name="/AccountService" class="org.sprfr.remoting.httpinvoker.HttpInvokerServiceExporter">
        <property name="service"><ref bean="accountService"/></property>
        <property name="serviceInterface">
            <value>example.AccountService</value>
        </property>
	</bean>