我在Linux操作系统中使用HornetQ和JBoss6, 有谁可以告诉我如何控制JMS队列来删除消息或更改消息的顺序 ,与队列连接的MessageConsumers细节?如何控制或管理JMS队列?例如。改变队列中的消息顺序,删除消息等
哪种方法最适合这种要求? 任何建议表示赞赏。
谢谢。
我在Linux操作系统中使用HornetQ和JBoss6, 有谁可以告诉我如何控制JMS队列来删除消息或更改消息的顺序 ,与队列连接的MessageConsumers细节?如何控制或管理JMS队列?例如。改变队列中的消息顺序,删除消息等
哪种方法最适合这种要求? 任何建议表示赞赏。
谢谢。
一个队列是一个简单的概念 - 多个源写入队列,单个消费者按照收到的顺序逐个读取消息。试图引入随机访问混淆了这个概念,并与队列中的意图背道而驰。
如果您不能修改用户删除或消息进行排序,然后介绍中介队列和消息驱动bean(MDB)做的工作:在MDB会消耗队列Q
消息,丢弃某些消息和在将消息发布到队列Q'
之前重新排序其他消息。
前:
Q -> orignal consumer
后:
Q -> your filtering and sorting MDB -> Q' -> original consumer
这会保留在您的设计组件的意图,是在我看来,更容易解释和理解。
编辑:您的MDB可能看起来像下面显示的示例(基于Java Enterprise Edition 6 tutorial)。本教程还包含有关打包和部署MDB的信息。
// MDB to consume messages on the original queue
@MessageDriven(mappedName="jms/IncomingQueue", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode",
propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType",
propertyValue = "javax.jms.Queue")
})
public class MyMDB implements MessageListener {
@EJB
private MessageFilter messageFilter;
public void onMessage(Message message) {
// pass on to MessageFilter bean for processing
messageFilter.filter(message);
}
}
// singleton bean to filter and sort messages, then re-publish to the original consumer
// if singleton doesn't work in your environment then you might have to persist the
// messages using e.g. JPA
@Singleton
public class MessageFilter {
@Resource(name="jms/OutgoingQueue")
Queue outgoingQueue;
@Resource(name="jms/QueueConnectionFactory")
QueueConnectionFactory qcf;
// accept incoming message from the MDB
void filter(Message message) {
// filter and sort messages
...
// send to queue read by the original consumer
send(message);
}
// send message to the filtered & sorted queue for the original consumer
void send(Message message) {
QueueConnection queueConnection = qcf.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueSender queueSender = queueSession(outgoingQueue);
queueSender.send(message);
}
}
的Java EE 6教程也对how to create singleton beans例子,这里有一个tutorial for connecting to a queue in order to send a message。
感谢您的回复,您可以详细介绍一下如何使用MDB实现这个功能吗?我需要使用HornetQ Server API还是其他方法? – SmartSolution 2011-06-08 07:09:13
@SmartSolution我已经添加了一个代码示例。我希望这有帮助。 Java Enterprise Edition 6教程包含大量信息,这些信息解释了我在上面的代码中引入的注释和概念。 – 2011-06-08 13:33:57
谢谢金,可以请看看这个问题:http://stackoverflow.com/questions/6289050/how-to-get-details-of-messagereceivers-of-jms-queuehornetq-deployed-in-jboss-as6 – SmartSolution 2011-06-09 06:49:58