JavaEE开源博客-阿Q

        ~用心做事,诚信做人!以德为本,兼顾各方!细心观察,联系推理!
        ~遵守规范->重构->可复用代码->可扩展性 *大道至简*
        ~时间是挤出来的,要成功,你就必须在最短的时间内采取最大量的行动!坚持到底,看到的就是胜利!再低下、再简单、再不愿意做的事情,你也要把它当成自己的事业做好!以马内利,阿们!

公告

我的分类(专题)

最新日志

最新评论

留言簿

搜索

登陆

友情连接

统计

2008-6-6 21:59:00
HttpInvoker远程调用实例

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="http://www.zhmy.com/org.eesite.bbs.remote.IRemoteService" />

    </bean>

</beans>

 

3web.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="http://www.zhmy.com/org.eesite.bbs.remote.IRemoteService" />

    </bean>

其中

1)IRemoteService只要有与服务端接口的方法一样就可以调用,此处的包名和类名可以根据实际情况给出,不过一般建议和服务器端一样,这样更合理些。

2)EEweb为服务端应用名称,remotingweb.xmlservlet过滤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);

    }

}

 

测试结果:正在请求调用...远程服务调用成功! 哈哈,我来了!

 

 

个人博客: http://zhan.zhmy.com/

posted @ 2008-6-6 21:59:00 sam 阅读全文 | 回复(0) | 引用通告 | 编辑
  • 标签:httpinvoker 远程 调用 rmi 服务 实例 例子 
  • 发表评论:
    网志中国数据载入中...