Java HttpInvoker小试(转)

Httpinvoker是SpringFramework提供的远程通讯协议,只能用于JAVA程序间的通讯,且服务端和客户端必须使用SpringFramework。

 

package test;

import java.util.Map;

/**
 * @author zhuc
 * @version 2012-5-18 下午1:19:08
 */
public interface IService {
	/**
	 * @param msg
	 * @return
	 */
	public String getString(String msg);

	/**
	 * @param map
	 * @return
	 */
	public Map<String, Object> getMap(String map);
	
	/**
	 * @return
	 */
	public Car getCar();
}

 

package test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhuc
 * @version 2012-5-18 下午1:19:32
 */
public class ServiceImpl implements IService {

	@Override
	public String getString(String msg) {
		return "hello : " + msg;
	}

	@Override
	public Map<String, Object> getMap(String map) {
		Map<String, Object> testMap = new HashMap<String, Object>();
		testMap.put("m1", 1);
		testMap.put("m2", "2");
		List<String> list = new ArrayList<String>();
		list.add("213");
		list.add("456");
		testMap.put("m3", list);
		testMap.put(map, map);
		System.out.println("远程调用:" + map);
		return testMap;
	}

	/* (non-Javadoc)
	 * @see test.IService#getCar()
	 */
	@Override
	public Car getCar() {
		Car c = new Car();
		c.setName("zhuc-httpinvoker-car");
		c.setCapacity(100);
		return c;
	}

}

 

package test;

import java.io.Serializable;

/**
 * @author zhuc
 * @version 2012-5-17 下午10:29:18
 */
public class Car implements Serializable {

	/** 
     *  
     */
	private static final long serialVersionUID = -1115598660168001267L;

	private String name;

	private transient Integer capacity;

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the capacity
	 */
	public Integer getCapacity() {
		return capacity;
	}

	/**
	 * @param capacity the capacity to set
	 */
	public void setCapacity(Integer capacity) {
		this.capacity = capacity;
	}

}

 

 web.xml具体配置

 

	<!-- 配置DispatcherServlet -->
	<servlet>
		<servlet-name>remote</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 配置该Servlet随应用启动时候启动 -->
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- 配置DispatcherServlet映射的url -->
	<servlet-mapping>
		<servlet-name>remote</servlet-name>
		<url-pattern>/remote/*</url-pattern>
	</servlet-mapping>

 

remote-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>
	<!-- 通过Spring HttpInvoker机制暴露远程访问服务 -->
	<bean id="httpInvokerService" class="test.ServiceImpl" />

	<bean name="/remoteService"
		class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
		<property name="service" ref="httpInvokerService" />
		<property name="serviceInterface" value="test.IService" />
	</bean>

	<!-- url映射 -->
	<bean id="urlMapping"
		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="remote.convert.service">/remoteService</prop>
			</props>
		</property>
	</bean>
	<!-- 此处将url映射为 remote.convert.service -->
</beans>

 

 

客户端

 

package test;

import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author zhuc
 * @version 2012-5-18 下午1:22:39
 */
public class ClientTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"test/remote-client.xml");
		IService service = (IService) applicationContext
				.getBean("remoteService");
		String msg = service.getString("zhuc-httpinvoker");
		System.out.println(msg);

		Car c = service.getCar();
		System.out.println(c.getName());
		System.out.println(c.getCapacity());
		
		Map<String, Object> map = service.getMap("test");
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			System.out.println(entry.getKey() + "," + entry.getValue());
		}
	}

}

 

remote-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>
	<!-- 通过Spring HttpInvoker机制代理远程访问服务 -->
	<bean id="remoteService"
		class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
		<property name="serviceUrl" value="${url}" />
		<property name="serviceInterface" value="test.IService" />
	</bean>
	
	<bean id="globalPropertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>test/client.properties</value>
			</list>
		</property>
	</bean>

</beans>

 

 

新建一个client.properties文件,方便配置

#url=http://localhost:8080/HttpInvoker/remote/remoteService
url=http://localhost:8080/HttpInvoker/remote/remote.convert.service

 

 

输出结果如下:

hello : zhuc-httpinvoker
zhuc-httpinvoker-car
null
test,test
m1,1
m2,2
m3,[213, 456]

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值