首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

施用Spring JMS轻松实现异步消息传递

2012-09-03 
使用Spring JMS轻松实现异步消息传递图1.贷款处理程序的序列图 (单击截图来查看完整视图)   下面的表3显示

使用Spring JMS轻松实现异步消息传递


图1.贷款处理程序的序列图 (单击截图来查看完整视图)

   下面的表3显示了在例程中我所使用的不同技术和开源框架,并按应用逻辑层排列。

   表3. 在JMS应用程序中使用的框架

<!--CreditRequestSendQueue-->
施用Spring JMS轻松实现异步消息传递<mbeancode="org.jboss.mq.server.jmx.Queue"
施用Spring JMS轻松实现异步消息传递name="jboss.mq.destination:service=Queue,name=CreditRequestSendQueue">
施用Spring JMS轻松实现异步消息传递<dependsoptional-attribute-name="DestinationManager">
施用Spring JMS轻松实现异步消息传递jboss.mq:service=DestinationManager
施用Spring JMS轻松实现异步消息传递</depends>
施用Spring JMS轻松实现异步消息传递</mbean>
施用Spring JMS轻松实现异步消息传递
施用Spring JMS轻松实现异步消息传递<!--CreditRequestReceiveQueue-->
施用Spring JMS轻松实现异步消息传递<mbeancode="org.jboss.mq.server.jmx.Queue"
施用Spring JMS轻松实现异步消息传递name="jboss.mq.destination:service=Queue,name=CreditRequestReceiveQueue">
施用Spring JMS轻松实现异步消息传递<dependsoptional-attribute-name="DestinationManager">
施用Spring JMS轻松实现异步消息传递jboss.mq:service=DestinationManager
施用Spring JMS轻松实现异步消息传递</depends>
施用Spring JMS轻松实现异步消息传递</mbean> 现在,让我们看看如何使用一个名为Hermes的JMS工具来浏览消息队列。Hermes是一个Java Swing应用程序,它可以创建、管理和监视JMS提供商(例如JBossMQ,WebSphereMQ,ActiveMQ和Arjuna服务器)里的JMS目标。从它的网站上下载Hermes,解压缩.zip文件到本地目录(例如,c:/dev/tools/hermes)。一旦安装完成,双击文件hermes.bat(位于bin文件夹下)启动程序。

   要在Hermes里配置JBossMQ服务器,请参考Hermes网站上的这个演示。它有着出色的step-by-step可视化指示来配置JBoss MQ。当配置一个新的JNDI初始上下文时,请输入下面的信息。

providerURL = jnp://localhost:1099initialContextFactory = org.jnp.interfaces.NamingContextFactoryurlPkgPrefixes = org.jnp.interfaces:org.jboss.namingsecurityCredentials = adminsecurityPrincipal = admin

  当您创建新的目标时,请输入queue/CreditRequestSendQueue和queue/CreditRequestReceiveQueue。图2显示了JMS控制台的主窗口,其中有为JMS例程创建的新的消息队列。

施用Spring JMS轻松实现异步消息传递

图 2. Hermes中所有目标的截图.(单击截图来查看完整视图)

   下面的图3显示了在从消息发送者类发送消息到CreditRequestSendQueue后,Hermes JMS控制台及消息队列的截图。您可以看见有5个消息在队列中,控制台显示了消息详情,例如消息ID,消息目标,时间戳和实际的消息内容。

施用Spring JMS轻松实现异步消息传递

图 3. Hermes中所有队列的截图.(单击截图来查看完整视图)

   在例程中使用的队列名称和其他JMS和JNDI参数见表 4。

   表4. Spring JMS配置参数

<beanid="jndiTemplate"class="org.springframework.jndi.JndiTemplate">
施用Spring JMS轻松实现异步消息传递<propertyname="environment">
施用Spring JMS轻松实现异步消息传递<props>
施用Spring JMS轻松实现异步消息传递<propkey="java.naming.factory.initial">
施用Spring JMS轻松实现异步消息传递org.jnp.interfaces.NamingContextFactory
施用Spring JMS轻松实现异步消息传递</prop>
施用Spring JMS轻松实现异步消息传递<propkey="java.naming.provider.url">
施用Spring JMS轻松实现异步消息传递localhost
施用Spring JMS轻松实现异步消息传递</prop>
施用Spring JMS轻松实现异步消息传递<propkey="java.naming.factory.url.pkgs">
施用Spring JMS轻松实现异步消息传递org.jnp.interfaces:org.jboss.naming
施用Spring JMS轻松实现异步消息传递</prop>
施用Spring JMS轻松实现异步消息传递</props>
施用Spring JMS轻松实现异步消息传递</property>
施用Spring JMS轻松实现异步消息传递</bean> 接着,我们配置队列连接工厂。清单3显示了队列连接工厂的配置。

   清单3. JMS队列连接工厂配置

?

施用Spring JMS轻松实现异步消息传递<beanid="jmsQueueConnectionFactory"
施用Spring JMS轻松实现异步消息传递class="org.springframework.jndi.JndiObjectFactoryBean">
施用Spring JMS轻松实现异步消息传递<propertyname="jndiTemplate">
施用Spring JMS轻松实现异步消息传递<refbean="jndiTemplate"/>
施用Spring JMS轻松实现异步消息传递</property>
施用Spring JMS轻松实现异步消息传递<propertyname="jndiName">
施用Spring JMS轻松实现异步消息传递<value>UIL2ConnectionFactory</value>
施用Spring JMS轻松实现异步消息传递</property>
施用Spring JMS轻松实现异步消息传递</bean> 我们定义2个JMS目标来发送和接收消息。详情见清单4和5。

   清单4. 发送队列配置

施用Spring JMS轻松实现异步消息传递<beanid="sendDestination"
施用Spring JMS轻松实现异步消息传递class="org.springframework.jndi.JndiObjectFactoryBean">
施用Spring JMS轻松实现异步消息传递<propertyname="jndiTemplate">
施用Spring JMS轻松实现异步消息传递<refbean="jndiTemplate"/>
施用Spring JMS轻松实现异步消息传递</property>
施用Spring JMS轻松实现异步消息传递<propertyname="jndiName">
施用Spring JMS轻松实现异步消息传递<value>queue/CreditRequestSendQueue</value>
施用Spring JMS轻松实现异步消息传递</property>
施用Spring JMS轻松实现异步消息传递</bean> 清单5. 接收队列配置

?

施用Spring JMS轻松实现异步消息传递<beanid="receiveDestination"
施用Spring JMS轻松实现异步消息传递class="org.springframework.jndi.JndiObjectFactoryBean">
施用Spring JMS轻松实现异步消息传递<propertyname="jndiTemplate">
施用Spring JMS轻松实现异步消息传递<refbean="jndiTemplate"/>
施用Spring JMS轻松实现异步消息传递</property>
施用Spring JMS轻松实现异步消息传递<propertyname="jndiName">
施用Spring JMS轻松实现异步消息传递<value>queue/CreditReqeustReceiveQueue</value>
施用Spring JMS轻松实现异步消息传递</property>
施用Spring JMS轻松实现异步消息传递</bean> 然后我们再来配置JmsTemplate组件。在例程中我们使用JmsTemplate102。同时使用defaultDestination属性来指定JMS目标。

   清单6. JMS模板配置

?

施用Spring JMS轻松实现异步消息传递<beanid="jmsTemplate"
施用Spring JMS轻松实现异步消息传递class="org.springframework.jms.core.JmsTemplate102">
施用Spring JMS轻松实现异步消息传递<propertyname="connectionFactory">
施用Spring JMS轻松实现异步消息传递<refbean="jmsQueueConnectionFactory"/>
施用Spring JMS轻松实现异步消息传递</property>
施用Spring JMS轻松实现异步消息传递<propertyname="defaultDestination">
施用Spring JMS轻松实现异步消息传递<refbean="destination"/>
施用Spring JMS轻松实现异步消息传递</property>
施用Spring JMS轻松实现异步消息传递<propertyname="receiveTimeout">
施用Spring JMS轻松实现异步消息传递<value>30000</value>
施用Spring JMS轻松实现异步消息传递</property>
施用Spring JMS轻松实现异步消息传递</bean> 最后我们配置发送者和接收者组件。清单7和8分别是Sender 和 Receiver对象的配置。

   清单7. JMS Sender配置


?

施用Spring JMS轻松实现异步消息传递<beanid="jmsSender"class="springexample.client.JMSSender">
施用Spring JMS轻松实现异步消息传递<propertyname="jmsTemplate">
施用Spring JMS轻松实现异步消息传递<refbean="jmsTemplate"/>
施用Spring JMS轻松实现异步消息传递</property>
施用Spring JMS轻松实现异步消息传递</bean>

清单8. JMS Receiver配置

?

施用Spring JMS轻松实现异步消息传递<beanid="jmsReceiver"class="springexample.client.JMSReceiver">
施用Spring JMS轻松实现异步消息传递<propertyname="jmsTemplate">
施用Spring JMS轻松实现异步消息传递<refbean="jmsTemplate"/>
施用Spring JMS轻松实现异步消息传递</property>
施用Spring JMS轻松实现异步消息传递</bean>
测试及监视

  我写了一个测试类,命名为LoanApplicationControllerTest,用来测试LoanProc程序。我们可以使用这个类来设定贷款参数以及调用信用请求服务类。

   让我们看一下不使用Spring JMS API而使用传统JMS开发途径的消息发送者实例。清单9显示了MessageSenderJMS类里的sendMessage方法,其中包含了使用JMS API处理消息的所有必需步骤。

   清单9. 传统JMS实例

?

施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递publicvoidsendMessage()...{
施用Spring JMS轻松实现异步消息传递
施用Spring JMS轻松实现异步消息传递queueName="queue/CreditRequestSendQueue";
施用Spring JMS轻松实现异步消息传递System.out.println("Queuenameis"+queueName);
施用Spring JMS轻松实现异步消息传递
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递/**//*
施用Spring JMS轻松实现异步消息传递*CreateJNDIInitialContext
施用Spring JMS轻松实现异步消息传递*/
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递try...{
施用Spring JMS轻松实现异步消息传递Hashtableenv=newHashtable();
施用Spring JMS轻松实现异步消息传递env.put("java.naming.factory.initial",
施用Spring JMS轻松实现异步消息传递"org.jnp.interfaces.NamingContextFactory");
施用Spring JMS轻松实现异步消息传递env.put("java.naming.provider.url","localhost");
施用Spring JMS轻松实现异步消息传递env.put("java.naming.factory.url.pkgs",
施用Spring JMS轻松实现异步消息传递"org.jnp.interfaces:org.jboss.naming");
施用Spring JMS轻松实现异步消息传递
施用Spring JMS轻松实现异步消息传递jndiContext=newInitialContext(env);
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递}catch(NamingExceptione)...{
施用Spring JMS轻松实现异步消息传递System.out.println("CouldnotcreateJNDIAPI"+
施用Spring JMS轻松实现异步消息传递"context:"+e.toString());
施用Spring JMS轻松实现异步消息传递}
施用Spring JMS轻松实现异步消息传递
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递/**//*
施用Spring JMS轻松实现异步消息传递*GetqueueconnectionfactoryandqueueobjectsfromJNDIcontext.
施用Spring JMS轻松实现异步消息传递*/
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递try...{
施用Spring JMS轻松实现异步消息传递queueConnectionFactory=(QueueConnectionFactory)
施用Spring JMS轻松实现异步消息传递jndiContext.lookup("UIL2ConnectionFactory");
施用Spring JMS轻松实现异步消息传递
施用Spring JMS轻松实现异步消息传递queue=(Queue)jndiContext.lookup(queueName);
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递}catch(NamingExceptione)...{
施用Spring JMS轻松实现异步消息传递System.out.println("JNDIAPIlookupfailed:"+
施用Spring JMS轻松实现异步消息传递e.toString());
施用Spring JMS轻松实现异步消息传递}
施用Spring JMS轻松实现异步消息传递
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递/**//*
施用Spring JMS轻松实现异步消息传递*Createconnection,session,senderobjects.
施用Spring JMS轻松实现异步消息传递*Sendthemessage.
施用Spring JMS轻松实现异步消息传递*CleanupJMSconnection.
施用Spring JMS轻松实现异步消息传递*/
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递try...{
施用Spring JMS轻松实现异步消息传递queueConnection=
施用Spring JMS轻松实现异步消息传递queueConnectionFactory.createQueueConnection();
施用Spring JMS轻松实现异步消息传递queueSession=queueConnection.createQueueSession(false,
施用Spring JMS轻松实现异步消息传递Session.AUTO_ACKNOWLEDGE);
施用Spring JMS轻松实现异步消息传递queueSender=queueSession.createSender(queue);
施用Spring JMS轻松实现异步消息传递message=queueSession.createTextMessage();
施用Spring JMS轻松实现异步消息传递message.setText("ThisisasampleJMSmessage.");
施用Spring JMS轻松实现异步消息传递System.out.println("Sendingmessage:"+message.getText());
施用Spring JMS轻松实现异步消息传递queueSender.send(message);
施用Spring JMS轻松实现异步消息传递
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递}catch(JMSExceptione)...{
施用Spring JMS轻松实现异步消息传递System.out.println("Exceptionoccurred:"+e.toString());
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递}finally...{
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递if(queueConnection!=null)...{
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递try...{
施用Spring JMS轻松实现异步消息传递queueConnection.close();
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递}catch(JMSExceptione)...{}
施用Spring JMS轻松实现异步消息传递}
施用Spring JMS轻松实现异步消息传递}
施用Spring JMS轻松实现异步消息传递} 现在,我们来看看使用了Spring的消息发送者实例。清单10显示了MessageSenderSpringJMS类中send方法的代码。

   清单10. 使用Spring API的JMS实例

?

施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递publicvoidsend()...{
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递try...{
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递ClassPathXmlApplicationContextappContext=newClassPathXmlApplicationContext(newString[]...{
施用Spring JMS轻松实现异步消息传递"spring-jms.xml"});
施用Spring JMS轻松实现异步消息传递
施用Spring JMS轻松实现异步消息传递System.out.println("Classpathloaded");
施用Spring JMS轻松实现异步消息传递
施用Spring JMS轻松实现异步消息传递JMSSenderjmsSender=(JMSSender)appContext.getBean("jmsSender");
施用Spring JMS轻松实现异步消息传递
施用Spring JMS轻松实现异步消息传递jmsSender.sendMesage();
施用Spring JMS轻松实现异步消息传递
施用Spring JMS轻松实现异步消息传递System.out.println("MessagesentusingSpringJMS.");
施用Spring JMS轻松实现异步消息传递施用Spring JMS轻松实现异步消息传递}catch(Exceptione)...{
施用Spring JMS轻松实现异步消息传递e.printStackTrace();
施用Spring JMS轻松实现异步消息传递}
施用Spring JMS轻松实现异步消息传递} 如您所见,通过使用配置文件,所有与管理JMS资源有关的步骤都将交由Spring容器处理。我们只需引用一个JMSSender对象,然后调用对象里的sendMessage方法。结束语

  在本文中,我们看到Spring框架是如何使用JMS API简化异步消息传递。Spring去掉了所有使用JMS处理消息所必需的样本代码(例如得到一个队列连接工厂,从Java代码里创建队列和会话对象, 在运行时使用配置文件对它们进行组配)。我们可以动态的交换JMS资源对象,而不必修改任何Java代码,这要感谢Inversion of Control (IOC) 原则的力量。

   既然异步消息传递是SOA框架的整体构成部分,Spring很适合纳入到SOA工具集。此外,JMS管理工具(如Hermes)使得创建、管理和监督JMS资源变得容易,特别是对于系统管理员来说。

参考资料本文配套的示例代码 Spring JMS documentation"1-2-3 Messaging with Spring JMS"JBoss MQ wiki

热点排行