Reputation: 527
i am using activeMQ to log something. the main code of sender class is like this:
for (int i = 1; i <= 5; i++) {
TextMessage message = session.createTextMessage("ActiveMq send" + i);
System.out.println("send msg:" + "ActiveMq send" + i);
producer.send(message);
}
Then i use another receiver to receive the 5 message from the sender.
MessageConsumer consumer = session.createConsumer(destination);
while (true) {// loop to receive message
TextMessage message = (TextMessage) consumer.receive();
if (null != message) {
System.out.println("received msg:" + message.getText());
}
}
if i write like this, i can only receive one or two message but not 5. if i use consumer.receive(10000)
, truly i will get the 5 message, but what if the sender sends millions of message? how to receive them all without losing one?
Upvotes: 0
Views: 1318
Reputation: 2132
Think you need some codes as follows :
while ((IMessage msg = consumer.receive(2000)) != null) {
// Process your message
}
Upvotes: 0
Reputation: 11377
@Evgeniy is right - MessageConsumer.receive() the correct call, or you can use listeners.
One other tip from real world experience with ActiveMQ is that it is buggy. I'll try not to rant because there are a lot of good things about the product. But I've run several ActiveMQ servers for more than 5 years, used at least as many different versions, and the entire time my experience has been that it is feature rich, and tends to work correctly in small test environments, but when under load "in the wild" it does all sorts of strange things - including losing messages, store-and-forward conduits randomly not working, and messages being "hidden" until they magically reappear later after a restart.
And I'm not the only person to experience this:
It's not impossible that whatever odd behavior you are seeing is simply a bug. (But that's also an easy out - it's probably more likely that there is something else wrong with your setup that we can't see from your post.)
Upvotes: 1
Reputation: 135992
MessageConsumer.receive should be what you need, according to JMS API it blocks indefinitely until a message is produced. Another way is to set a listener for this consumer - MessageConsumer.setMessageListener(MessageListener listener)
Upvotes: 0