我想在没有消费者或非活动消费者的情况下向队列发送消息。并且稍后消息(读取)消息。下面我展示了我的想法。
将消息发送到队列的代码
@Resource(mappedName = "java:/JmsXA")
private QueueConnectionFactory queueConnectionFactory;
@Resource(mappedName = "queue/DocumentServiceMessageBeanPlanified")
private Queue documentQueueDestinationPlanified;
...
public anExampleMethod() {
QueueConnection queueConnection;
queueConnection = this.queueConnectionFactory.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false, javax.jms.Session.CLIENT_ACKNOWLEDGE);
MessageProducer queueSender = queueSession.createSender(this.documentQueueDestinationPlanified);
Message messagePlanified = queueSession.createMessage();
// populate message
queueSender.send(messagePlanified);
queueSender.close();
queueSession.close();
queueConnection.close();
}
读取/消费消息的代码:
public void otherMethod() throws JMSException {
QueueConnection queueConnection;
queueConnection = this.queueConnectionFactory.createQueueConnection();
QueueSession queueSession =
queueConnection.createQueueSession(false, javax.jms.Session.CLIENT_ACKNOWLEDGE);
MessageConsumer queueConsumer =
queueSession.createConsumer(this.documentQueueDestinationPlanified);
Message aMessage = queueConsumer.receiveNoWait();
while (aMessage != null) {
aMessage.acknowledge();
// some work here with message informations
aMessage = queueConsumer.receiveNoWait();
}
queueConsumer.close();
queueSession.close();
queueConnection.close();
}
另一种方法必须在特殊时刻启动。有特殊配置吗?因为我收到了这个错误:
javax.ejb.EJBException: java.lang.RuntimeException: Unable to inject jndi dependency: env/com.sylob.cochise.dm1.ejb.entite.document.DocumentServiceBean/documentQueueDestinationPlanified into property com.sylob.cochise.dm1.ejb.entite.document.DocumentServiceBean.documentQueueDestinationPlanified: DocumentServiceMessageBeanPlanified not bound
// do you want all the stack trace?
我已经使用了一些消息队列但是使用了messagedrivenbean或者我需要在特殊时刻阅读消息:/
答案 0 :(得分:0)
如果队列是持久的,那么一般的想法似乎是合理的。我在代码中看不到明显错误,错误消息指向配置问题。它似乎忽略了您的mappedName定义。应用程序服务器不需要一致地处理mappedName。也许您的服务器会忽略它。仅使用发送方法或MDB尝试相同的注释,看看是否有效,还可以尝试使用JNDI浏览器或手动查找映射名称,看看是否可以通过这种方式解析条目。我打赌手动查找会起作用(当然注射更好)!
此外,了解您正在使用的应用服务器有用吗?