HttpInvoker远程调用实例
一、服务器端:
1、服务接口与实现类
package org.eesite.bbs.remote;
/**
* 远程服务接口
*
* @author zhanjia
*
*/
public interface IRemoteService {
public String getString(String msg);
}
package org.eesite.bbs.remote;
/**
* 远程服务接口实现类
*
* @author zhanjia
*
*/
public class RemoteServiceImpl implements IRemoteService {
public String getString(String msg) {
String str = "正在请求调用...远程服务调用成功! " + msg;
return str;
}
}
2、服务暴露配置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="rmiService"
class="org.eesite.bbs.remote.RemoteServiceImpl" />
<bean name="/remoteService"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="rmiService" />
<property name="serviceInterface"
value="org.eesite.bbs.remote.IRemoteService" />
</bean>
</beans>
3、web.xml配置
<!-- 加载服务配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/remote-servlet.xml</param-value>
</context-param>
<!-- 配置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>/remoting/*</url-pattern>
</servlet-mapping>
注意:
注册servlet名为remote,此名字要和服务配置文件的名字的第一部分相同,即servlet的名字必须为remote-servlet中的remote。
二、客户端
1、配置文件
<!-- 通过Spring HttpInvoker机制代理远程访问服务 -->
<bean id="remoteService"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl"
value="http://localhost:2601/EEweb/remoting/remoteService" />
<property name="serviceInterface"
value="org.eesite.bbs.remote.IRemoteService" />
</bean>
其中
1)、IRemoteService只要有与服务端接口的方法一样就可以调用,此处的包名和类名可以根据实际情况给出,不过一般建议和服务器端的接口/类名一样,这样更合理些。
2)、EEweb为服务端应用名称,remoting为web.xml中servlet过滤url的/remoting/*中的remoting
2、测试类
package test;
import org.eesite.bbs.remote.IRemoteService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author zhanjia
*
*/
public class TestRemote {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("remote.xml");
IRemoteService service = (IRemoteService) applicationContext
.getBean("remoteService");
String msg = service.getString("哈哈,我来了!");
System.out.println(msg);
}
}
测试结果:正在请求调用...远程服务调用成功! 哈哈,我来了!
评论
1):注册servlet名为remote,此名字要和服务配置文件的名字的第一部分相同,即servlet的名字必须为remote-servlet中的remote.这里是为什么呢?
2):EEweb为服务端应用名称,remoting为web.xml中servlet过滤url的/remoting/*中的remoting.这里的EEweb为服务端应用名称是什么意思啊?
麻烦您说详细点好吗?
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 753 次
- 性别:

- 来自: 广东省揭阳市

- 详细资料
搜索本博客
我的相册
共 4 张
最新评论
-
HttpInvoker远程调用实例
注册servlet名为remote,此名字要和服务配置文件的名字的第一部分相同, ...
-- by HRoger -
HttpInvoker远程调用实例
写得很好,但是我还是存在一些疑惑的地方,主要有: 1):注册servlet名为 ...
-- by shengdianjisi






评论排行榜