首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

hessian远道框架使用

2013-08-01 
hessian远程框架使用hessian远程框架的使用简述hessian是个简单好用的java远程调用框架,其不与spring结合

hessian远程框架使用
     hessian远程框架的使用简述

hessian是个简单好用的java远程调用框架,其不与spring结合使用,比结合spring使用更为简单,所以我一般直接采用不与spring整合配置的使用方式,当然,在具体的服务类里,还是会去调用spring容器的服务类来执行数据库访问。这样我觉得更方便。

   下面,我就讲一下不整合spring的调用方式:

   只要引入hessian的jar包便可,不需要依赖别的jar包,我用的hessian-4.0.7.jar;
hessian会自己序列化与反序列化参数,开发者无需关注这些。
  1.  在web.xml中增加如下配置:

<servlet>
   <servlet-name>remote</servlet-name>
   <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
       <init-param>
      <param-name>service-class</param-name>
      <param-value>com.zlwh.member.remote.BulletinService</param-value>
    </init-param>
   <load-on-startup>1</load-on-startup>
  </servlet>
       <servlet-mapping>
    <servlet-name>remote</servlet-name>
    <url-pattern>/remote/*</url-pattern>
  </servlet-mapping>

注意service-class参数配置替换成你自己的服务类便可以。

2. 服务类及接口:

public interface IBulletinService {
   public String addBulletin(Bulletin entity);  
   public int getUserNoReadCount(Long userId)throws Exception; 
}

实现类:
public class BulletinService implements IBulletinService{
@Override
public String addBulletin(Bulletin entity) {
String result="OK";
BulletinManager bulletinManager=ApplicationContextUtils.getBean("bulletinManager");
entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
result=this.validateInfo(entity);
try{
    bulletinManager.save(entity);
}catch(Exception e){
result="FAILED";
}
return result;
}

@Override
public int getUserNoReadCount(Long userId)throws Exception{
BulletinManager bulletinManager=ApplicationContextUtils.getBean("bulletinManager");
return bulletinManager.getUserNoReadCount(userId);
}
}

3.客户端调用:

客户端需要引入服务的接口类IBulletinService ,hessian的jar包,如果参数不是本地对象,需要引入参数对象。


public class TestRemote {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String url = "http://127.0.0.1:8080/uc-backend/remote";
        HessianProxyFactory factory = new HessianProxyFactory();
IBulletinService hessianServer =(IBulletinService)factory.create(IBulletinService.class, url);
Bulletin bulletin=new bulletin();
hessianServer.addBulletin(bulletin);
System.out.println("success!");
int num=hessianServer.getUserNoReadCount(1L);
System.out.println("没有阅读的数量:"+num);
}

}

  

热点排行