1 package ejava.examples.jms20.jmsmechanics;
2
3 import java.util.LinkedList;
4
5 import javax.jms.JMSException;
6 import javax.jms.Message;
7 import javax.jms.MessageListener;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12
13
14
15
16 public class AsyncClient implements MessageListener, MyClient {
17 private static final Logger logger = LoggerFactory.getLogger(AsyncClient.class);
18 private int count=0;
19 private LinkedList<Message> messages = new LinkedList<>();
20
21 public void onMessage(Message message) {
22 try {
23 String level = message.getStringProperty("level");
24 logger.debug("onMessage received ({}, mode={}, pri={}{}):{}", ++count,
25 message.getJMSDeliveryMode(),
26 message.getJMSPriority(),
27 (level==null?"":", level="+level),
28 message.getJMSMessageID());
29 message.acknowledge();
30 synchronized(messages) {
31 messages.add(message);
32 }
33 } catch (JMSException ex) {
34 logger.error("error handling message", ex);
35 }
36 }
37
38 public int getCount() { return count; }
39
40 public Message getMessage() {
41 synchronized(messages) {
42 return (messages.isEmpty() ? null : messages.remove());
43 }
44 }
45 }