Wildfly上的JMS 2.0 QueueBrowser不返回消息 [英] JMS 2.0 QueueBrowser on Wildfly does not return messages
问题描述
我有2个简单的EJB bean. 第一个是定时器方法,每秒调用一次.在这种方法中,我向TestQueue添加了10条随机消息.
I have 2 simple EJB beans. The first one is a timer method that is called every second. In this method I add 10 random messages to a TestQueue.
@Singleton
@Startup
public class Timer {
@Inject
private JMSContext context;
@Resource(mappedName = "java:/jms/queue/TestQueue")
private Queue queue;
@Schedule(hour = "*", minute = "*", second = "*/1", persistent = false)
@AccessTimeout(unit = TimeUnit.DAYS, value = 1)
public void addToQueue() {
for(int i = 0; i<30; i++)
context.createProducer().send(queue, "msg " + (new Random().nextInt(100)));
printQueueSize();
}
public void printQueueSize() {
QueueBrowser qb = context.createBrowser(queue);
Enumeration enumeration = null;
try {
enumeration = qb.getEnumeration();
} catch (JMSException e) {
e.printStackTrace();
}
int size = 0;
while(enumeration.hasMoreElements()) {
enumeration.nextElement();
size++;
}
System.out.println("Queue size: " + size);
}
}
第二个bean是MDB使用者.我放入Thread.sleep(100)来模拟长时间运行的任务,并确保TestQueue中有一些消息.
Second bean is a MDB consumer. I put Thread.sleep(100) to simulate long running task and make sure that there are some messages in TestQueue.
@MessageDriven(
activationConfig = {
@ActivationConfigProperty(propertyName = "destination", propertyValue = "java:/jms/queue/TestQueue"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "maxSession", propertyValue = "1")
})
public class Consumer implements MessageListener {
@Override
public void onMessage(Message msg) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
问题在于控制台输出显示:
The problem is that console output shows:
15:06:29,006 INFO [stdout] (EJB default - 9) Queue size: 0
15:06:30,006 INFO [stdout] (EJB default - 10) Queue size: 0
15:06:31,009 INFO [stdout] (EJB default - 1) Queue size: 0
等
但是在wildfly管理控制台中,我看到每秒有越来越多的消息:
but in the wildfly admin console I can see that there are more and more messages every second:
问题是,为什么QueueBrowser返回空的Enumeration?这是HornetQ实施中的错误还是出于某些原因?
The question is, why the QueueBrowser return empty Enumeration? Is it a bug in HornetQ implementation or there is some reason for that?
推荐答案
尽管这不能回答您的问题,但我将展示如何在HornetQ中通过EJB服务获得队列大小:
Although this will not answer your question, I will show how I get the queue size in HornetQ, in an EJB Service:
InitialContext initialContext = null;
try {
// Step 1. Create an initial context to perform the JNDI lookup.
initialContext = getContext();
// Step 2. Perfom a lookup on the queue
Queue queue = (Queue) initialContext.lookup("jmx/queue/YOUR_QUEUE_NAME");
// Step 7. Use JMX to retrieve the message counters using the
// JMSQueueControl
ObjectName on = ObjectNameBuilder.DEFAULT
.getJMSQueueObjectName(queue.getQueueName());
JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL("service:jmx:http-remoting-jmx://localhost:9990"), new HashMap<String, Object>());//this is the URL for WildFly 8.2 (should work for all 8.X)
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
JMSQueueControl queueControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, JMSQueueControl.class, false);
// Step 8. List the message counters and convert them to
// MessageCounterInfo data structure.
String counters = queueControl.listMessageCounter();
MessageCounterInfo messageCounter = MessageCounterInfo.fromJSON(counters);
queueControl.getConsumerCount();//this returns consumer count
queueControl.getMessagesAdded();//returns messages added from the last time we checked
queueControl.isPaused();//prints out, whether the queue is paused (you can stop a queue from being processed e.g. from JMX)
queueControl.getMessageCount();//message count so far
} finally {
// Step 17. Be sure to close our JMS resources!
if (initialContext != null) {
initialContext.close();
}
}
这篇关于Wildfly上的JMS 2.0 QueueBrowser不返回消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!