手机版
你好,游客 登录 注册
背景:
阅读新闻

Spring基于JMS实现远程访问

[日期:2011-10-06] 来源:Linux社区  作者:shirdrn [字体:大 中 小]

再看一下,客户端如何进行远程访问。

对应的客户端的配置client.xml,内容如下所示:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  5.   
  6.     <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
  7.         <property name="brokerURL" value="tcp://192.168.1.103:61616" />  
  8.     </bean>  
  9.   
  10.     <bean id="myQueue" class="org.apache.activemq.command.ActiveMQQueue">  
  11.         <constructor-arg value="shirdrnQ" />  
  12.     </bean>  
  13.   
  14.     <bean id="myAccountService"  
  15.         class="org.springframework.jms.remoting.JmsInvokerProxyFactoryBean">  
  16.         <property name="serviceInterface" value="org.shirdrn.spring.remote.jms.AccountService" />  
  17.         <property name="connectionFactory" ref="connectionFactory" />  
  18.         <property name="queue" ref="myQueue" />  
  19.     </bean>  
  20.   
  21. </beans>  

主要是基于Spring提供的org.springframework.jms.remoting.JmsInvokerProxyFactoryBean来实现,通过消息中间件代理,调用远程发布的服务。

需要注意的是,客户端是通过消息队列(org.apache.activemq.command.ActiveMQQueue)的名称(如上述为shirdrnQ)来获取消息(远程服务),如果无法识别消息队列是无法实现服务的远程访问的。

客户端实现远程访问,示例代码如下所示:

  1. package org.shirdrn.spring.remote.jms;  
  2.   
  3. import org.apache.log4j.Logger;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. public class Client {  
  8.       
  9.     private static final Logger LOG = Logger.getLogger(Client.class);  
  10.     static int index = 0;  
  11.       
  12.     private static void invoke(AccountService service) {  
  13.         String mobileNo = "1380013800" + (String.valueOf(index++));  
  14.         String result = service.shoopingPayment(mobileNo, (byte) 3);  
  15.         LOG.info("Client gets result: " + result);  
  16.     }  
  17.       
  18.     public static void main(String[] args) throws Exception {  
  19.         ApplicationContext ctx = new ClassPathXmlApplicationContext(  
  20.                 new String[] { "org/shirdrn/spring/remote/jms/client.xml" });  
  21.         AccountService service = (AccountService) ctx  
  22.                 .getBean("myAccountService");  
  23.         for(int i=0; i<100; i++) {  
  24.             invoke(service);  
  25.         }  
  26.     }  
  27. }  
linux
相关资讯       Spring  JMS 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款