8.1web应用示例

8.1web应用示例
为了在本章中演示集成示例,一个web应用示例将被用来证明每个集成都是成功地。这个理由四个网络应用的拷贝,每个为不同的环境进行了定制。每个web应用很小并仅使用ActiveMQ 代理,一个JMS连接工厂和一个JMS queue。图8.1展现了该网络应用示例的目录结构。
如你所见,这个应用的结构是一个基于Maven的Java web 应用的基本结构。虽然在图8.1中的screenshot为jms-webapp-local应用显示了项目结构,其他应用实例的目录结构仅略有不同。每个web application使用Spring框架的网络框架特性,它减少了建立web application的复杂度。为了理解web application是如何工作的,最好examine一些在基本Java web application中找到的主要的项目。
在本章中的练习相关的web application 相关的部分是web.xml文件,Spring应用环境,和JmsMessageSenderService类。下面的列表展示了web.xml相关的部分。
[i]Listing 8.1 The web.xml file[/i]
[code]
...
<resource-ref>
<description>JMS Connection</description>
<res-ref-name>jms/ConnectionFactory</res-ref-name>
<res-type>org.apache.activemq.ActiveMQConnectionFactory</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<res-ref-name>jms/FooQueue</res-ref-name>
<res-type>javax.jms.Queue</res-type>
<res-auth>Container</res-auth>
</resource-ref>
...
[/code]
在web.xml中的<resource-ref>元素引用了和 application server注册的JNDI资源。这个配置使那些资源对web application可用。这个配置仅将为Geronimo集成改变,它将为JMS queue使用基本的<message-destination-ref>替代<resource-ref>。
其它从web application上来的相关配置文件是Spring application context,如下所示。
[i]Listing 8.2 The Spring application context file[/i]
[code]
...
<jee:jndi-lookup id="connectionFactory"
jndi-name="java:comp/env/jms/ConnectionFactory"
cache="true"
resource-ref="true"
lookup-on-startup="true"
expected-type="org.apache.activemq.ActiveMQConnectionFactory"
proxy-interface="javax.jms.ConnectionFactory" />
<jee:jndi-lookup id="fooQueue"
jndi-name="java:comp/env/jms/FooQueue"
cache="true"
resource-ref="true"
lookup-on-startup="true"
expected-type="org.apache.activemq.command.ActiveMQQueue"
proxy-interface="javax.jms.Queue" />
<bean id="jmsMessageBean"

<bean id="messageSenderService"
class="org.apache.activemq.book.ch8.jms.service.JmsMessageSenderService"
p:connectionFactory-ref="connectionFactory"
p:queue-ref="fooQueue" />
...
[/code]
在表8.2中显示的Spring application context 是一个Spring框架的XML配置文件:查看[url]http://www.springframework.org[/url]。(请同时注意Spring的p-namespace[url]http://mng.bz/dLT9[/url]在这个配置中被使用。)<jee:jndi-lookup>元素使用Spring来执行一个对指出资源的JNDI查找。这些资源稍后将在在messageSenderService java bean由Spring实例化以后被injected到messageSenderService Java bean中(值通过Setter方法插入)。接着messageSenderService被web application用于发送JMS消息。表8.3显示了JmsMessageSenderService bean的源码。
[i]Listing 8.3 The JmsMessageSenderService class[/i]
[code]
public class JmsMessageSenderService {
private JmsTemplate jmsTemplate;
public void sendMessage(final JmsMessage bean) throws JMSException {
if (bean.isPersistent()) {
jmsTemplate.setDeliveryPersistent(bean.isPersistent());
}
if (0 != bean.getTimeToLive()) {
jmsTemplate.setTimeToLive(bean.getTimeToLive());
}
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage message = session.createTextMessage(bean.getMessagePayload());
if (bean.getReplyTo() != null && !bean.getReplyTo().equals("")) {
ActiveMQQueue replyToQueue = new ActiveMQQueue(bean.getReplyTo());
message.setJMSReplyTo(replyToQueue);
}
return message;
}
});
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
}
[/code]
JmsMessageSenderService bean被简单的kept所以它仅聚焦于发送JMS消息的任务。该类使用Spring的JmsTemplate和一个anonymous的MessageCreator来简单的发送JMS消息。
在这个web application中这里仅有一个web page并且deliberately未完成的。这是因为该web application仅需要测试这个集成罢了。添加更多详细的东西仅会造出错误并detract本章的真实目的。
为了更好地通过这些类了解一个JMS消息的工作流,看一下图8.2(图略)。
[color=olive]第一步 [/color]JmsMessageSenderService实现了一个匿名的Spring MessageCreator来建立消息。
[color=olive]第二步 [/color]JmsMessageSenderService使用Spring JmsTemplate发送消息到ActiveMQ。
[color=olive]第三步 [/color]Spring DefaultMessageListenerContainer 消费消息并将它转换到JmsMessageDelegate。
[color=olive]第四步 [/color]JmsMessageDelegate bean处理消息(它输出消息的正文)。
JmsMessageSenderService完全从Spring DefaultMessageListenerContainer和JmsMessageDelegate bean隔离。任何通过JmsMessageSenderService发送到ActiveMQ的消息和DefaultMessageListenerContainer是否在线和准备消费消息无关。事实上,JmsMessageSenderService和Spring DefaultMessageListenerContainer可以很容易地从这个应用程序分离以在完全不同的流程中重用并且它不会改变这个应用程序的功能。这是ActiveMQ提供的一个虽然是很小的但完美的异步消息传递例子。
图8.2的步骤都隐藏于显示与图8.3的示例web application中的单一页面的场景内。
当使用在图8.3中显示的页面发送消息,一个小的消息短暂地出现在页面上,然后逐渐很快消退来指示消息被发送。这仅仅是一个检查来显示web应用程序中的一些活动。这些是在web应用程序中唯一可见的功能。其它发生的所有事情都隐藏在场景之下。
这些只是恰当地与应用程序服务器集成的ActiveMQ的web应用程序的一部分。为了更好地了解这个示例web应用的详细内容,并且你自己通过这些示例的工作准确地部署测试你自己的集成,下载该书的示例代码。
本章的四个版本的web应用示例是:
■jms-webapp-geronimo--被用来展示ActiveMQ和Geronimo集成
■jms-webapp-global--被用来展示使用global的JNDI的ActiveMQ和Tomcat或Jetty的集成
■jms-webapp-jboss--被用来展示ActiveMQ和JBoss一起配置和发布
■jms-webapp-local--被用来展示使用local的JNDI的ActiveMQ和Tomcat或jetty的集成
[color=blue]注意 [/color][i]local JNDI配置示例和global JNDI配置示例无法同时部署。因为这会导致classloader issues并使ActiveMQ无法正确部署。确保某刻只部署某一个风格的配置。[/i]
在处理with this chapter,你需要建立四个这样的示例,这能通过下面的Maven命令实现。
[i]Listing 8.4 Build the examples[/i]
[code]
[amq-in-action-example-src] $ cd chapter8/
[chapter8] $ mvn clean install
[INFO] Scanning for projects...
[INFO] Reactor build order:
[INFO] jms-webapp-geronimo
[INFO] jms-webapp-global
[INFO] jms-webapp-jboss
[INFO] jms-webapp-local
[INFO] ActiveMQ In Action Examples Chapter 8
...
[INFO]
[INFO]
[INFO] -------------------------------------------------------------------
-----
[INFO] Reactor Summary:
[INFO] -------------------------------------------------------------------
-----
[INFO] jms-webapp-geronimo ...................................
SUCCESS [4.787s]
[INFO] jms-webapp-global .....................................
SUCCESS [1.265s]
[INFO] jms-webapp-jboss ......................................
SUCCESS [8.278s]
[INFO] jms-webapp-local ......................................
SUCCESS [2.359s]
[INFO] ActiveMQ In Action Examples Chapter 8 .................
SUCCESS [1.911s]
[INFO] -------------------------------------------------------------------
-----
[INFO] -------------------------------------------------------------------
-----
[INFO] BUILD SUCCESSFUL
[INFO] -------------------------------------------------------------------
-----
[INFO] Total time: 18 seconds
[INFO] Finished at: Mon Apr 26 13:24:31 MDT 2010
[INFO] Final Memory: 19M/35M
[INFO] -------------------------------------------------------------------
-----
[/code]
注意表8.4的输出已经被略微省略了。只要你看到BUILD SUCCESSFUL消息,示例已被正确地build并且针对每一个的WAR文件现在应该存在于每个项目targe目录下。然后这个WAR文件能被部署到正确的应用服务并用来测试ActiveMQ集成。
[color=blue]注意 [/color][i]虽然本章详细描述了针对每个应用服务的必要修改,所有这个修改已在每个项目中做了。只要确保下载本书的源码来获取这些示例项目。[/i]
现在你对示例应用已经有了一个概览,你已经准备试试集成了。第一个要和ActiveMQ集成的应用服务是Apache Tomcat。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值