利用 WAS CE v2.1 创建基于 JMS 的应用
2009-11-11 00:00:00 来源:WEB开发网OrderSenderBean 使用了 @Resource 来绑定 JMS 资源。此段代码也展示了利用 JMS 队列发送消息的流程。
创建 MDB(Message Driven Bean)
Java EE 中的 MDB 可以用于接收 JMS 消息。为此,创建两个 MDB-ShopAccountant 与 ShopStorage,分别表示会计与仓库。参见清单 7 和清单 8。
清单 7. ShopAccountant 代码片段@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType",
propertyValue = "javax.jms.Topic"),
@ActivationConfigProperty(propertyName = "destination",
propertyValue = "OrderTopic")})
public class ShopAccountant implements MessageListener {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("ShopAccountant: Order Received \n" +
textMessage.getText());
//do accountant work
......
} catch (JMSException e) {
e.printStackTrace();
}
}
}
清单 8. ShopStorage 代码片段@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType",
propertyValue = "javax.jms.Topic"),
@ActivationConfigProperty(propertyName = "destination",
propertyValue = "OrderTopic")
})
public class ShopStorage implements MessageListener {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("ShopStorage: Order Received \n" + textMessage.getText());
//find good in storage
......
} catch (JMSException e) {
e.printStackTrace();
}
}
}
更多精彩
赞助商链接