package server.demo;
public interface DemoApi {
public void setName(String name);
public String sayHello();
public User getUser();
}
实现接口:
package server.demo;
public class DemoService implements DemoApi {
private String name;
public String sayHello() {
return "Hello "+name;
}
public void setName(String name) {
this.name=name;
System.out.println("Hello "+name);
}
public User getUser() {
return new User("lingling", "pass");
}
}
创建User类,注意:一定要implements Serializable。
package server.demo;
import java.io.Serializable;
public class User implements Serializable {
String name = "kitty";
String password = "nopass";
public User() {
super();
}
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
配置web.xml:
first
com.caucho.hessian.server.HessianServlet
service-class
server.demo.DemoService
first
/helloworld
将项目发布到tomcat服务器上。
Java客户端:
环境:j2sdk1.4.2
依赖的包:hessian-3.1.6.jar
创建一个名为HessianClient的java project,载入hessian-3.1.6.jar。
创建与服务器一样的接口及User类,注意它们所在的包路径也要与服务器相同,否则会出现java.lang.ClassNotFoundException。
接口:
package server.demo;
public interface DemoApi {
public void setName(String name);
public String sayHello();
public User getUser();
}
User类:
package server.demo;
import java.io.Serializable;
public class User implements Serializable {
String name = "kitty";
String password = "nopass";
public User() {
super();
}
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
客户端程序:
package client.demo;
import java.net.MalformedURLException;
import server.demo.DemoApi;
import com.caucho.hessian.client.HessianProxyFactory;
public class ClientTest {
public static void main(String[] args) throws MalformedURLException,
ClassNotFoundException {
String url = "http://localhost:9520/HessianServer/helloworld";
HessianProxyFactory factory = new HessianProxyFactory();
DemoApi api = (DemoApi) factory.create(url);
api.setName("test");
System.out.println(api.sayHello());
System.out.println(api.getUser().getName());
System.out.println(api.getUser().getPassword());
}
}
现在,启动tomcat,运行ClientTest。运行结果:
Hello test
lingling
pass
现在,在以上基础上将hessian与spring整合。
Java服务器端(即我们上面建的名为HessianServer的web project):
环境:j2sdk1.4.2、Tomcat6.0
依赖的包:
1.Hessian包:hessian-3.1.6.jar
2.spring-framework-2.0.2包:
a)aopalliance.jar
b)commons-logging.jar
c)log4j-1.2.14.jar
d)spring.jar
e)spring-aop.jar
f)spring-beans.jar
g)spring-context.jar
h)spring-core.jar
i)spring-jdbc.jar
j)spring-jms.jar
k)spring-web.jar
l)spring-webmvc.jar
配置web.xml,web.xml中增加:
remote
org.springframework.web.servlet.DispatcherServlet
namespace
classes/remote-servlet
1
remote
/remote/*
写spring的发布hessian服务的配置文件remote-servlet.xml,这个文件就建在工程的src下,即deploy到tomcat后,在WEB-INF/classes目录下。注意:这个文件为什么叫remote-servlet.xml呢?因为我们在web.xml中有配置:classes/remote-servlet所以文件名为remote-servlet.xml。
以下是remote-servlet.xml的内容:
class="org.springframework.remoting.caucho.HessianServiceExporter">
好了,现在可以测试一下了。在刚才建的名为HessianClient的java project中修改client.demo.ClientTest.java如下:
package client.demo;
import java.net.MalformedURLException;
import server.demo.DemoApi;
import com.caucho.hessian.client.HessianProxyFactory;
public class ClientTest {
public static void main(String[] args) throws MalformedURLException,
ClassNotFoundException {
// String url = "http://localhost:9520/HessianServer/helloworld";
String url = "http://localhost:9520/HessianServer/remote/helloSpring";
HessianProxyFactory factory = new HessianProxyFactory();
DemoApi api = (DemoApi) factory.create(DemoApi.class, url);
api.setName("test");
System.out.println(api.sayHello());
System.out.println(api.getUser().getName());
System.out.println(api.getUser().getPassword());
}
}
现在,启动tomcat,运行ClientTest。运行结果:
Hello test
lingling
pass
如果客户端也使用spring呢?现在做一个spring整合hessian的客户端demo。首先载入spring-framework-2.0.2包:
1)aopalliance.jar
2)commons-logging.jar
3)log4j-1.2.14.jar
4)spring.jar
5)spring-aop.jar
6)spring-beans.jar
7)spring-context.jar
8)spring-core.jar
9)spring-jdbc.jar
10)spring-jms.jar
11)spring-web.jar
12)spring-webmvc.jar
在名为HessianClient的java project中src目录下,新建一个remote-client.xml,这个文件可随意命名。remote-client.xml内容为:
class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
http://localhost:9520/HessianServer/remote/helloSpring
server.demo.DemoApi
在刚才建的名为HessianClient的java project中修改client.demo.ClientTest.java如下:
package client.demo;
import java.net.MalformedURLException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import server.demo.DemoApi;
public class ClientTest {
public static void main(String[] args) throws MalformedURLException,
ClassNotFoundException {
// String url = "http://localhost:9520/HessianServer/helloworld";
// String url =
// "http://localhost:9520/HessianServer/remote/helloSpring";
// HessianProxyFactory factory = new HessianProxyFactory();
// DemoApi api = (DemoApi) factory.create(DemoApi.class, url);
ApplicationContext context = new ClassPathXmlApplicationContext(
"remote-client.xml");
DemoApi api = (DemoApi) context.getBean("helloSpring");
api.setName("test");
System.out.println(api.sayHello());
System.out.println(api.getUser().getName());
System.out.println(api.getUser().getPassword());
}
}
现在,启动tomcat,运行ClientTest。运行结果:
Hello test
lingling
pass
至此over~
PS:有时候可能因hessian或spring的版本问题使程序不能运行