Spring Hessian

1:服务器端pom.xml

<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>${spring.version}</version>
</dependency>

2:服务器端web.xml

<!-- spring hessian -->
	<servlet>
	    <servlet-name>hessian</servlet-name>
	    <servlet-class>
	        org.springframework.web.servlet.DispatcherServlet
	    </servlet-class>
	    <init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:/hessian-servlet.xml</param-value>
		</init-param>
	    <load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
	    <servlet-name>hessian</servlet-name>
	    <url-pattern>/hessian/*</url-pattern>
	</servlet-mapping>

 3:服务器端

(1):applicationContext.xml

<bean id="appService" class="com.fangtoon.appcenter.platform.app.service.AppService" />

 

(2):hessian-servlet.xml:

<beans>
    <bean name="/appServiceCall" class="org.springframework.remoting.caucho.HessianServiceExporter">
            <!-- appService是实现了IAppService接口的SpringBean -->
	    <property name="service" ref="appService"/>
	    <property name="serviceInterface" value="com.fangtoon.appcenterclient.app.service.IAppService"/>
	</bean>
</beans>

 4:客户端spring配置文件:

<!-- Hessian远程调用配置  -->
<bean id="appService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
	    <property name="serviceUrl" value="${hessianBaseUrl}/hessian/appServiceCall"/>
            <property name="overloadEnabled" value="true" />
	    <property name="serviceInterface" value="com.fangtoon.appcenterclient.app.service.IAppService"/>
</bean>

 5:使用Spring调用: 

@Resource(name="appService") IAppService appService;

 6:普通Java调用:

import java.util.List;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.caucho.hessian.client.HessianProxyFactory;
import com.fangtoon.appcenterclient.app.service.IAppService;
import com.fangtoon.appcenterclient.dto.app.BizApp;

public class HessianTest {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Test
	public void testHessianJavaCall() {
		try {
			String url = "http://localhost:8082/AppCenter/hessian/appServiceCall";
			HessianProxyFactory factory = new HessianProxyFactory();
			IAppService appService = (IAppService) factory.create(IAppService.class, url);
			List<BizApp> ret = appService.find();
			System.out.println(ret.get(0).getName());
		} catch (Exception e) {
			logger.error("", e);
		}
	}

}

 7:注意:

(1):传输的Dto必须实现可序列化接口.

猜你喜欢

转载自rayoo.iteye.com/blog/2104486