Hessian 使用例子

时间:2022-01-08 23:24:07

一、协议包(数据对象需要实现序列化接口,可以用于服务端接口、客户端调用服务之用)

Hessian 使用例子

/**
*
*/
package com.junge.demo.protocol.model; import java.io.Serializable; /**
* @author Administrator
*
*/
public class User implements Serializable { /**
*
*/
private static final long serialVersionUID = -8967262939434460765L; private Integer userid;
private String username; public Integer getUserid() {
return userid;
} public void setUserid(Integer userid) {
this.userid = userid;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} @Override
public String toString() {
return "User [userid=" + userid + ", username=" + username + "]";
}
}
/**
*
*/
package com.junge.demo.protocol.service; import com.junge.demo.protocol.model.User; /**
* @author Administrator
*
*/
public interface UserService { Integer addUser(User user); void delUser(Integer userid); void modifyUser(User user); User getUserByUserId(Integer userid);
}

二、服务端(需要建立web项目)

/**
*
*/
package com.junge.spring.hessian.service.impl; import com.junge.demo.protocol.model.User;
import com.junge.demo.protocol.service.UserService; /**
* @author Administrator
*
*/
public class UserServiceImpl implements UserService { public Integer addUser(User user) {
System.out.println("addUser:" + user);
return user.getUserid();
} public void delUser(Integer userid) {
System.out.println("delUser,userid=" + userid); } public void modifyUser(User user) {
System.out.println("modifyUser:" + user); } public User getUserByUserId(Integer userid) {
System.out.println("getUserByUserId,userid={}" + userid); User user = new User();
user.setUserid(userid);
user.setUsername("张三");
return user;
} }

web.xml配置

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>CalServiceServlet</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>com.junge.spring.hessian.service.impl.CalServiceImpl</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CalServiceServlet</servlet-name>
<url-pattern>/calService</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>UserServiceServlet</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>com.junge.spring.hessian.service.impl.UserServiceImpl</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>UserServiceServlet</servlet-name>
<url-pattern>/userService</url-pattern>
</servlet-mapping>
</web-app>

三、客户端

/**
*
*/
package com.junge.demo.hessian; import com.caucho.hessian.client.HessianProxyFactory;
import com.junge.demo.protocol.model.User;
import com.junge.demo.protocol.service.UserService; /**
* @author Administrator
*
*/
public class HessianClient { /**
* @param args
*/
public static void main(String[] args) {
/*
* try { String url = "http://localhost:8080/hessian/calService";
* HessianProxyFactory factory = new HessianProxyFactory();
* factory.setOverloadEnabled(true); CalService basic = (CalService)
* factory.create(CalService.class, url); System.out.println(basic.add(3, 4));
* System.out.println(basic.sub(3, 0)); } catch (Exception e) {
* e.printStackTrace(); }
*/ addUserBatch();
} public static void addUserBatch() { Long btime = System.currentTimeMillis(); String url = "http://localhost:8080/hessian/userService";
HessianProxyFactory factory = new HessianProxyFactory();
factory.setOverloadEnabled(true); try {
UserService basic = (UserService) factory.create(UserService.class, url);
for (int i = 0; i < 5000; i++) { new Thread(new OperUser(basic)).start();
}
} catch (Exception e) {
e.printStackTrace();
} Long etime = System.currentTimeMillis(); System.out.println(etime - btime);
} } class OperUser implements Runnable {
private UserService userService; public OperUser(UserService userService) {
this.userService = userService;
} @Override
public void run() {
// 增加、详情、修改、删除
User user = new User();
user.setUserid(1);
user.setUsername("名字");
userService.addUser(user); User user2 = userService.getUserByUserId(1); userService.modifyUser(user2); userService.delUser(user2.getUserid()); }
}