Hessian是一個遠程調用的技術,hessian由兩個部分組成,服務端和客戶端,服務端供客戶端調用。下面先上一個小例子,然後再作一個簡單的說明。
服務端項目結構圖:
HelloService接口代碼:
public interface HelloService {
public void hello();
}
HelloServiceImpl實現類代碼:
@Service("helloService")
public class HelloServiceImpl implements HelloService {
@Override
public void hello() {
System.out.println("hello hession !");
}
}
spring相關的配置文件
<bean id="helloService"
class="com.hession.HelloServiceImpl"/>
<bean name="/hello"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="helloService"/>
<property name="serviceInterface"
value="com.hession.HelloService"/>
</bean>
項目中導入hessian包,在web.xml配置hessian的servlet-mapping
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.json</url-pattern>
<url-pattern>*.xml</url-pattern>
<url-pattern>*.jhtml</url-pattern>
<url-pattern>*.do</url-pattern>
<url-pattern>/hessian/*</url-pattern>(配置hessian路徑)
</servlet-mapping>
客戶端:
HelloService接口和服務端的一致,HessianClient代碼如下:
public class HessionClient {
public static void main(String[] args) {
//具體路徑根據服務端的配置改變而改變
String url="http://localhost:8182"
+"/hessian/hessian/hello";
HessianProxyFactory pf = new HessianProxyFactory();
HelloService hello = null;
try {
hello = (HelloService)pf.create(
HelloService.class,url);
hello.hello();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
運行main方法即可調用服務端的業務。