posts - 32, comments - 153, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

使用Hessian开发C/S模式的小系统

Posted on 2007-07-28 10:48 Zou Ang 阅读(3820) 评论(4)  编辑  收藏 所属分类:
使用Hessian开发C/S模式的小系统
之所以叫小系统,主要是因为Hessian不支持Hibernate的延迟加载,所以需要在DAO层使用Hibernate的同学请注意了。

Hessian其实是一种RMI技术,已经被集成在Spring Framework中了,这样就很方便了。

首先看客户端

第一步:
先定义Client与Server交互的接口。
比如:
package edu.zsu.zouang.pos.service;

import edu.zsu.zouang.pos.pojo.User;
/**
 * Please Contact <a href="mailto:richardeee@gmail.com">Zou Ang</a>
 * 
@author Zou Ang
 *
 
*/
public interface ILoginService extends Service{
    
public User login(String loginName, String password);
    
    
public boolean logout(String loginName);
}
然后定义xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>

    
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        
<property name="location">
            
<value>config.properties</value>
        
</property>
    
</bean>

    
<bean id="urlMapping"
        class
="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
    
</bean>
    
    
<bean id="loginClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        
<property name="serviceUrl">
            
<value>http://${serverName}:${port}/${contextPath}/login.do</value>
        
</property>
        
<property name="serviceInterface">
            
<value>edu.zsu.zouang.pos.service.ILoginService</value>
        
</property>
    
</bean>
</beans>
其中config.properties文件的内容如下:
#在此配置服务器端信息

#服务器地址
serverName=172.18.17.111

#服务器端端口号
port=8080

#服务器端上下文
contextPath=pos
这样,在得到loginClient对象以后,就可以直接使用login(username,pwd)来登录了。Hessian会自动把请求发送给服务器端。

客户端的代码基本就这么多,主要应该关注的是对用户的表现,至于业务逻辑,就让服务器端去实现好了。

再看服务器端的配置:
接口跟上面的ILoginService一样
在我的程序中,使用了Spring MVC进行URL Mapping。
web.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
<display-name>pos</display-name>
    
<welcome-file-list>
        
<welcome-file>index.html</welcome-file>
        
<welcome-file>index.htm</welcome-file>
        
<welcome-file>index.jsp</welcome-file>
        
<welcome-file>default.html</welcome-file>
        
<welcome-file>default.htm</welcome-file>
        
<welcome-file>default.jsp</welcome-file>
    
</welcome-file-list>
    
<context-param>
    
<param-name>webAppRootKey</param-name>
    
<param-value>pos.root</param-value>
  
</context-param>
    
<listener>
        
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    
</listener>
    
<context-param>
        
<param-name>log4jConfigLocation</param-name>
        
<param-value>WEB-INF/log4j.properties</param-value>
    
</context-param>
    
<listener>
        
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    
</listener>
    
<context-param>
        
<param-name>contextConfigLocation</param-name>
        
<param-value>
            WEB-INF/springctx-common.xml
            WEB-INF/springctx-login.xml
            WEB-INF/applicationContext.xml
            WEB-INF/DispatcherServlet-servlet.xml
        
</param-value>
    
</context-param>
    
<filter>
        
<filter-name>hibernateFilter</filter-name>
        
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    
</filter>

    
<filter-mapping>
        
<filter-name>hibernateFilter</filter-name>
        
<url-pattern>*.do</url-pattern>
    
</filter-mapping>
    
    
<servlet>
        
<servlet-name>DispatcherServlet</servlet-name>
        
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        
<load-on-startup>1</load-on-startup>
    
</servlet>
    
<servlet-mapping>
        
<servlet-name>DispatcherServlet</servlet-name>
        
<url-pattern>*.do</url-pattern>
    
</servlet-mapping>
    
<servlet>
      
<servlet-name>dwr-invoker</servlet-name>
      
<display-name>DWR Servlet</display-name>
      
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
      
<init-param>
          
<param-name>debug</param-name>
          
<param-value>true</param-value>
      
</init-param>
  
</servlet>
  
<servlet-mapping>
      
<servlet-name>dwr-invoker</servlet-name>
      
<url-pattern>/dwr/*</url-pattern>
  
</servlet-mapping>
</web-app>
其中DispatcherServlet-servlet.xml(注意,名称不能更改)内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
    
<bean name="/login.do" class="org.springframework.remoting.caucho.HessianServiceExporter">
        
<property name="service">
            
<ref bean="loginService" />
        
</property>
        
<property name="serviceInterface">
            
<value>edu.zsu.zouang.pos.service.ILoginService</value>
        
</property>
    
</bean><!--
    
    <bean name="/inventory.do" class="org.springframework.remoting.caucho.HessianServiceExporter">
        <property name="service">
            <ref bean="posGoodsService" />
        </property>
        <property name="serviceInterface">
            <value>edu.zsu.zouang.pos.service.IPosGoodsService</value>
        </property>
    </bean>
    
--></beans>

在配置文件中的loginService就是服务器端时间处理登录的对象,它要实现ILoginService接口
在spring-common.xml中定义了数据源和一些基本的类,比如sessionFactory和transactionManager,dataSource使用了C3P0
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
    
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        
<property name="location">
            
<value>WEB-INF/connection.properties</value>
        
</property>
    
</bean>
    
<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        
<property name="driverClass" value="${jdbc.driverClassName}" />
        
<property name="jdbcUrl" value="${jdbc.url}" />
        
<property name="user" value="${jdbc.username}" />
        
<property name="password" value="${jdbc.password}" />
        
<property name="minPoolSize" value="${jdbc.minPoolSize}" />
        
<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
        
<property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
        
<property name="acquireIncrement" value="${jdbc.acquireIncrement}" />
        
<property name="maxStatements" value="${jdbc.maxStatements}" />
        
<property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
        
<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" />
        
<property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}" />
        
<property name="breakAfterAcquireFailure" value="${jdbc.breakAfterAcquireFailure}" />
        
<property name="testConnectionOnCheckout" value="${jdbc.testConnectionOnCheckout}" />
    
</bean>
    
    
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        
<property name="mappingResources">
            
<list>
                
<value>edu/zsu/zouang/pos/pojo/Goods.hbm.xml</value>
                
<value>edu/zsu/zouang/pos/pojo/GoodsType.hbm.xml</value>
                
<value>edu/zsu/zouang/pos/pojo/User.hbm.xml</value>
                
<value>edu/zsu/zouang/pos/pojo/Role.hbm.xml</value>
                
<value>edu/zsu/zouang/pos/pojo/UserRole.hbm.xml</value>
            
</list>
        
</property>
        
<property name="hibernateProperties">
            
<props>
                
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
                
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            
</props>
        
</property>
        
<property name="dataSource" ref="c3p0DataSource"/>
    
</bean>
    
    
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        
<property name="sessionFactory"><ref local="sessionFactory"/></property>
    
</bean>
</beans>
在applicationContext.xml中配置了Spring MVC所需要的类
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    
<!--Definition of View Resolver -->
    
<bean id="viewResolver"
        class
="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
<property name="viewClass"
            value
="org.springframework.web.servlet.view.JstlView" />
        
<property name="prefix" value="/WEB-INF/jsp/" />
        
<property name="suffix" value=".jsp" />
    
</bean>


    
<!--Request Mapping -->
    
<bean id="urlMapping"
        class
="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
    
</bean>

</beans>
经过上面的配置,就实现了最基础的Hessian功能了。这个程序很小,但是可以帮助我们熟悉一下Hessian,等于是个入门吧


评论

# re: 使用Hessian开发C/S模式的小系统  回复  更多评论   

2007-07-30 16:07 by CowNew开源团队
《J2EE开发全程实录》中给出了一个在分布式系统中使用Hibernate而免于考虑延迟加载的解决方案,可以参考一下,:)

# re: 使用Hessian开发C/S模式的小系统  回复  更多评论   

2007-08-01 23:30 by 橙子
mmmm,,,,,,

果然开始更新了

有钻研就有新发现

技术牛人=zouang

# re: 使用Hessian开发C/S模式的小系统  回复  更多评论   

2009-03-19 19:22 by manlge
多简单的功能让你整这么复杂,这就是Spring垃圾框架带来的结果

# re: 使用Hessian开发C/S模式的小系统  回复  更多评论   

2012-04-25 06:44 by
太谢谢啦,终于明白了 加载spring的顺序哦

只有注册用户登录后才能发表评论。


网站导航: