Httpinvoker是SpringFramework提供的远程通讯协议,只能用于JAVA程序间的通讯,且服务端和客户端必须使用SpringFramework。
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;
}
}
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
新建一个client.properties文件,方便配置
输出结果如下:
hello : zhuc-httpinvoker
zhuc-httpinvoker-car
null
test,test
m1,1
m2,2
m3,[213, 456]