用spirng和hessian构建分布式应用(远程接口)的方法
来源:互联网 发布:线切割割梅花怎么编程 编辑:程序博客网 时间:2024/04/29 07:23
最近一期的《programmer》里几乎从头至尾在讲关于“J2EE without EJB”的事情,可怜的ejb啊,居然被描述成了遗产系统的重要组成部分。。。
其实有上面的结论,无外乎现在java里面的新技术已经几乎能完全取代ejb的优点,而克服ejb的缺点,entity bean和有状态的session bean已经机乎被视为垃圾,hibernate和spring大行其到,看看最进n期《programmer》中篇幅的比重就知道了。本来我个人的感觉是hibernate取代了entity bean,spring取代了session bean,但是ejb的远程调用用hibernate和spring的架构还取代不了,可是在最近的一期《programmer》中我发现了Hessian!更爽的是,发现了spring原来可以和Hessian结合使用!看来真的可以say byebye to ejb了。
看到这么振奋人心的消息,怎么能不亲自试验一下呢,于是上http://www.caucho.com/以迅雷不及掩耳盗铃之势下载了Hessian的src jar和bin jar。create一个工程,把这些相关的jar统统扔进去,配置和coding就可以开始了。首先,开了如下几个包:
whao.test.hessian.server
放远程服务的接口
whao.test.hessian.server.impl
放远程服务的实现类
whao.test.hessian.client
放客户端应用
1. whao.test.hessian.server中写一个MyService接口:
/*
* Created on
*
*/
package whao.test.hessian.server;
/**
* @author Hao Wei
*
*/
public interface MyService {
public String doSomething(String s);
}
2. whao.test.hessian.server.impl中写一个实现类
/*
* Created on
*
*/
package whao.test.hessian.server.impl;
import whao.test.hessian.server.MyService;
/**
* @author Hao Wei
*
*/
public class MyServiceImpl implements MyService {
/* (non-Javadoc)
* @see whao.test.hessian.server.MyService#doSomething(java.lang.String)
*/
public String doSomething(String s) {
return "HAHAHA: " + s;
}
}
3. 配置远程服务
Hessian的远程服务要配置成servlet,配置如下:
<servlet>
<servlet-name>myservice</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>service-class</param-name>
<param-value>whao.test.hessian.server.impl.MyServiceImpl</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myservice</servlet-name>
<url-pattern>/myservice</url-pattern>
</servlet-mapping>
这样,当启动了这个web应用,这个远程服务就以url http://localhost:8080/test_web/myservice 的形式发布了。然后就是开发客户端来调用这个远程服务。
/*
* Created on
*
*/
package whao.test.hessian.client;
import whao.test.hessian.server.MyService;
import com.caucho.hessian.client.HessianProxyFactory;
/**
* @author Hao Wei
*
*/
public class TestMain {
public static void main(String[] args) throws Exception {
HessianProxyFactory proxyFactory = new HessianProxyFactory();
MyService service = (MyService) proxyFactory.create(MyService.class,
"http://localhost:8080/test_web/myservice");
System.out.println(service.doSomething("xixixixi"));
System.out.println("ok!");
}
}
运行一把,显示
HAHAHA:xixixi
ok!
不错不错,纯Hessian的远程调用就这样搞定了。继续研究《programmer》看到上面介绍用spring的远程访问解决方案来访问ejb的远程服务。什么?spring还有远程解决方案?没听说过嘛,看了《programmer》上的介绍,发现是真的。那么既然spring能访问ejb的远程服务,那么能访问Hessian的远程服务么?打开spring.jar看看,居然发现了名曰org.springframework.remoting.caucho.HessianProxyFactoryBean的类!夏昕真不厚道啊,再他的spring中文教程中居然匿掉了spring里这么好的东西!于是打开sping英文版reference,终于找到了用spring配置Hessian客户端的方法:
<bean id="myService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<value>
http://localhost:8080/test_web/myservice
</value>
</property>
<property name="serviceInterface">
<value>whao.test.hessian.server.MyService</value>
</property>
</bean>
然后客户端就可以这么玩啦:
/*
* Created on
*
*/
package whao.test.hessian.client;
import whao.util.spirng.SpringBeanFactory;
* @author Hao Wei
*
*/
public class TestMain {
public static void main(String[] args) throws Exception {
testWithSpring();
}
public static void testWithSpring(){
MyService service = (MyService)SpringBeanFactory.getBean("myService");
System.out.println(service.doSomething("lllllllll"));
System.out.println("ok!");
}
public static void testWithoutSpring() throws Exception {
HessianProxyFactory proxyFactory = new HessianProxyFactory();
MyService service = (MyService) proxyFactory.create(MyService.class,
"http://localhost:8080/test_web/myservice");
System.out.println(service.doSomething("xixixixi"));
System.out.println("ok!");
}
}
执行一下,输出是:
HAHAHA:lllllllll
ok!
spring真是个好东东,呵呵,其中的SpringBeanFactory是这样实现的:
/*
* Created on
*
*/
package whao.util.spirng;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* @author Hao Wei
*
*/
public class SpringBeanFactory {
private static final Log log = LogFactory.getLog(SpringBeanFactory.class);
private static ApplicationContext ctx = null;
public static Object getBean(ServletContext context, String beanID) {
log.info("beanID=" + beanID);
ApplicationContext ac = WebApplicationContextUtils
.getWebApplicationContext(context);
return ac.getBean(beanID);
}
public static Object getBean(String beanID){
if(ctx == null){
ctx = new FileSystemXmlApplicationContext(
"D://whao-work//src//test_web//test_web//WEB-INF//applicationContext.xml");
}
return ctx.getBean(beanID);
}
}
看到sping.jar的包org.springframework.remoting.caucho中还有另外两个类叫HessianClientInterceptor和HessianServiceExporter,看来貌似hessian服务端也可以和spring结合,还有客户端的Interceptor估计可以实现对远程服务的AOP,要继续研究一下了,待续吧
- 用spirng和hessian构建分布式应用(远程接口)的方法
- 用spirng和hessian构建分布式应用(远程接口)的方法
- 用spring和hessian构建分布式应用(远程接口)的方法
- spirng和hessian构建分布式应用
- 分布式设计《hessian集成spring的应用》
- 使用Hessian和Burlap发布远程接口服务步骤
- hessian 实现远程接口调用
- hessian 远程接口调用原理
- Spirng集成Hessian框架
- Hessian 远程调用框架(Java跨工程调用接口)
- spirng整合mybatis的事务(接口开发配置)
- Hessian源码分析和Hack --让Hessian携带远程调用端的信息
- 有关Spirng ioc的BeanPostProcessor接口案例
- Hessian的应用
- hessian远程调用的使用
- Spring 集成hessian进行接口的暴露和访问
- 远程通信框架--hessian应用分享
- Spirng MVC拦截器的应用
- 2004年度八大推荐最佳管理案例
- 一个小东西:class Gotter,它从一个异质容器里找到你要的那个东西(二)
- 当独孤求败遇见东方不败
- Oracle数据库中分区表的操作方法
- 东方不败回忆录
- 用spirng和hessian构建分布式应用(远程接口)的方法
- .NET 设计时示例
- Windows下如何彻底删除Oracle
- C语言: 按照格式返回系统时间的的函数
- Hibernate Iterator JCS分析
- asp.net里的常用代码
- 我是第三类人(转)
- 许巍 "礼物"
- 运用Raptor-V0.1做的论坛截图-已经更新