Http Invoker

Http Invoker使用HTTP传送物件,传送时使用Java的序列化机制来传送,由于透过HTTP传送,所以 在使用它们时要搭配Spring Web框架来使用,也就是使用到DispatcherServlet,可以改写 Hessian、 Burlap,只要修改一下service‐config.xml就可以了:

•      service‐config.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="urlMapping" class="org.springframework.web.servlet. 		→ handler.SimpleUrlHandlerMapping"> 		<property name="mappings"> 			<props> 				<prop key="/some.service">serviceExporter</prop> 			</props> 		</property> 	</bean> 	<bean id="viewResolver" class="org.springframework.web.servlet. 		→ view.InternalResourceViewResolver"> 		<property name="prefix"> 			<value>/WEB-INF/jsp/</value> 		</property> 		<property name="suffix"> 			<value>.jsp</value> 		</property> 	</bean> 	<bean id="someService" class="onlyfun.caterpillar.SomeServiceImpl"/> 	<bean id="serviceExporter" class="org.springframework.remoting. 		→ httpinvoker.HttpInvokerServiceExporter"> 		<property name="service"> 			<ref bean="someService"/> 		</property> 		<property name="serviceInterface"> 			<value>onlyfun.caterpillar.ISomeService</value> 		</property> 	</bean> </beans>bsp;

接下来客户端的部份,可以改写 Hessian、Burlap  的内容,修改一下Bean定义档的内容:

•      invoker‐client.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="someServiceProxy" class="org.springframework.remoting. 		→ httpinvoker.HttpInvokerProxyFactoryBean"> 		<property name="serviceUrl"> 			<value> 				http://localhost:8080/HttpInvokerDemo/some.service 			</value> 		</property> 		<property name="serviceInterface"> 			<value>onlyfun.caterpillar.ISomeService</value> 		</property> 	</bean> </beans>

注意到"serviceUrl"属性的设定,它是个标准的 HTTP 请求位址,来撰写个简单的客户端程式以使 用 Http Invoker 伺服器上的服务:

•      HessianClient.java
package onlyfun.caterpillar; import org.springframework.context.ApplicationContext; import org.springframework.context. support.FileSystemXmlApplicationContext; public class HessianClient { 	public static void main(String[] args) {  		ApplicationContext context =new FileSystemXmlApplicationContext( "invoker-client.xml"); 		ISomeService service =(ISomeService) context.getBean("someServiceProxy"); 		String result1 = service.doSomeService("Some request");  		System.out.println(result1); 		int result2 = service.doOtherService(1);  		System.out.println(result2); 	} }

执行的结果与 RMI 是相同的。

原文地址:https://www.cnblogs.com/chenying99/p/2555626.html