1.hessian-demo-api
package com.weizhi.hessian.demo.api;
/**
* @ClassName IRpcService
* @Description TODO
* @Author weizhi2018
* @Date 2013-9-27 下午02:58:46
*
*/
public interface IRpcService {
public String deal(String data);
}
2.hessian-demo-server
用tomcat6发布服务
package com.weizhi.hessian.demo.server;
import com.caucho.hessian.server.HessianServlet;
import com.weizhi.hessian.demo.api.IRpcService;
/**
* @ClassName RpcServer
* @Description TODO
* @Author weizhi2018
* @Date 2013-9-27 下午02:59:43
*
*/
public class RpcServerImpl extends HessianServlet implements IRpcService {
/**
*
*/
private static final long serialVersionUID = 1L;
/*
* @see com.weizhi.hessian.demo.api.IRpcService#deal(java.lang.String)
*/
public String deal(String data) {
if (data == null || "".equals(data))
return "[Invalid Parameter]";
return "[Tested," + data + "]";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>service</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>service-class</param-name>
<param-value>com.weizhi.hessian.demo.server.RpcServerImpl</param-value>
</init-param>
<init-param>
<param-name>service-api</param-name>
<param-value>com.weizhi.hessian.demo.api.IRpcService</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>service</servlet-name>
<url-pattern>/service</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.hessian-demo-client
package com.weizhi.hessian.demo.client;
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;
import com.weizhi.hessian.demo.api.IRpcService;
/**
* @ClassName RpcClient
* @Description TODO
* @Author weizhi2018
* @Date 2013-9-27 下午05:55:09
*
*/
public class RpcClient {
/**
* @throws MalformedURLException
* @Title main
* @Description TODO
* @Author weizhi2018
* @param args
* @throws
*/
public static void main(String[] args) throws MalformedURLException {
String url = "http://localhost:8080/hessian-demo-server/service";
HessianProxyFactory factory = new HessianProxyFactory();
IRpcService service = (IRpcService) factory.create(IRpcService.class, url);
String data1 = "weizhi2018";
System.out.println("return:" + service.deal(data1));
String data2 = "";
System.out.println("return:" + service.deal(data2));
}
}
4. hessian-4.0.7.jar