利用 WAS CE v2.1 创建基于 JMS 的应用
2009-11-11 00:00:00 来源:WEB开发网因为 OrderSenderBean 中引用了 JMS 资源,所以还需要在 ShopWEB 的部署文件中,添加对 JMS 资源引用。
在 web.xml 中添加清单 5 所示代码。
清单 5. web.xml<resource-ref>
<res-ref-name>OrderConnectionFactory</res-ref-name>
<res-type>javax.jms.TopicConnectionFactory</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<message-destination-ref>
<message-destination-ref-name>OrderTopic</message-destination-ref-name>
<message-destination-type>javax.jms.Topic</message-destination-type>
<message-destination-usage>Produces</message-destination-usage>
<message-destination-link>OrderTopic</message-destination-link>
</message-destination-ref>
从代码中可以看出,此处引用的 JMS 资源正是在 jms-resources.xml 中定义的 OrderConnectionFactory 与 OrderTopic。当应用程序部署至 WAS CE 2.1 后,就可以使用这些 JMS 资源。
在 OrderSenderBean 中,有如下代码片段:
清单 6. OrderSenderBean.javapublic class OrderSenderBean {
@Resource(name = "OrderConnectionFactory")
private ConnectionFactory factory;
@Resource(name = "OrderTopic")
private Topic orderTopic;
public String orderGoods() {
......
// sending message to accountant and storage
Connection connection;
try {
connection = factory.createConnection();
Session sess = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
TextMessage msg = sess.createTextMessage("<CustomerId=" +
customerId + "GoodsId=" + goodsId+">");
MessageProducer messageProducer = sess.createProducer(orderTopic);
messageProducer.send(msg);
System.out.println("OrderSenderBean: Order request sent to accountant and storage");
} catch (JMSException e) {
System.out.println("Error " + e);
e.printStackTrace();
}
return "ok";
}
更多精彩
赞助商链接