Hession远程服务
1.适用场景
一个系统向另一个系统提供远程服务。主要采用RPC二进制通信协议,适用于发送二进制数据。
2.代码示例
2.1.1.Server端代码结构
2.1.2.Server端代码
//TODO 说明:server端主要是向client端提供服务的接口。
接口RMIInterface.java代码
packagecom.suning.inteface;
publicinterfaceRMIInterface {
publicString sayHello();
}
//TODO 说明:此接口只以一个方法。
接口实现类RMIInterfaceImpl.java代码
packagecom.suning.impl;
importorg.springframework.stereotype.Service;
importcom.suning.inteface.RMIInterface;
@Service("rMIInterface")
publicclassRMIInterfaceImplimplementsRMIInterface {
@Override
publicString sayHello() {
return"HelloRemote Client!";
}
}
//TODO 说明:此接口实现类实现sayHello( )方法,返回"Hello RemoteClient!"一句话。
配置文件applicationContext.xml
的注解功能。
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--引入Spring注释功能,指出需要注释注入的包-->
<context:annotation-config/>
<context:component-scanbase-package="com.suning"/>
</beans>
//TODO 说明:此配置文件是spring的配置文件,这里配置它主要是因为类中用到了spring
配置文件remoting-servlet.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<beanname="/remotingService"class="org.springframework.remoting.caucho.HessianServiceExporter">
<propertyname="service"ref="rMIInterface"/>
<propertyname="serviceInterface"value="com.suning.inteface.RMIInterface"/>
</bean>
</beans>
//TODO 说明:此配置文件是服务端开放服务的配置文件,客户端引用的服务名就是"/remotingService"。
配置文件web.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"version="2.5">
<display-name>RMIServer8080</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
</web-app>
//TODO 说明:此配置文件时web项目的基本配置文件,实现hession在其中加入了remotingservlet以及servlet mapping。
2.2.Client端
2.2.1.Client端代码结构
2.2.2.Client端代码
接口RMIInterface.java代码
packagecom.suning.service;
publicinterfaceRMIInterface {
publicString sayHello();
}
//TODO 说明:此接口与服务端的接口相同,也可以直接引入服务端的接口用。
配置文件applicationContext.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--引入Spring注释功能,指出需要注释注入的包-->
<context:annotation-config/>
<context:component-scanbase-package="com.suning"/>
<beanid="remotingService"class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl"value="http://localhost:8080/RMIServer/remoting/remotingService"/>
<property name="serviceInterface"value="com.suning.service.RMIInterface"/>
</bean>
</beans>
//TODO 说明:此spring的配置文件中配置了服务器端得服务"remotingService",value值是服务器端开放的服务"/remotingService"的绝对地址。
配置文件struts.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constantname="struts.objectFactory"value="spring"/>
<includefile="struts-default.xml"/>
<packagename="suning"extends="struts-default">
<actionname="rmiAction"class="com.suning.action.RMIAction">
<resultname="success">/jsp/rmiTest.jsp</result>
</action>
</package>
</struts>
//TODO 说明:这里的struts.xml配置文件没有任何与hession相关的配置,只是基础的struts2配置文件。
配置文件web.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"version="2.5">
<display-name>Struts2Tiles</display-name>
<!--配置spring监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>jsp/rmiTest.jsp</welcome-file>
</welcome-file-list>
</web-app>
//TODO 说明:这里的web.xml配置文件没有任何与hession相关的配置,只是基础的web项目配置文件。
RMIAction.java代码
packagecom.suning.action;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importcom.opensymphony.xwork2.ActionSupport;
importcom.suning.service.RMIInterface;
publicclassRMIActionextends ActionSupport {
privatestaticfinallongserialVersionUID =-1135797722699269421L;
@Autowired(required=false)
@Qualifier("remotingService")
privateRMIInterfacerMIInterface;
publicString execute(){
Stringmessage = "";
try{
message= rMIInterface.sayHello();
}catch(Exceptione){
message= "远程服务连接失败!";
}
this.addActionMessage(message);
returnSUCCESS;
}
}
//TODO 说明:此action与struts2的action无异,这里获得server端的返回值。
rmiTest.jsp的代码
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s"uri="/struts-tags"%>
<!DOCTYPEhtmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>远程接口调用(RMI)测试</title>
<s:head/>
</head>
<body>
<s:actionmessage/>
<ahref="rmiAction.action">远程接口调用(RMI)测试</a>
</body>
</html>
//TODO 说明:页面一个按钮,将server端得到的值打印在页面上。