View Javadoc
1   package ejava.examples.jms20.jmsmechanics;
2   
3   import javax.jms.JMSConsumer;
4   import javax.jms.JMSException;
5   import javax.jms.Message;
6   
7   import org.slf4j.Logger;
8   import org.slf4j.LoggerFactory;
9   
10  /**
11   * This client will synchronously poll for messages from a JMSConsumer. It 
12   * runs within the thread of the caller. 
13   */
14  public class SyncClient implements MyClient {
15      private static final Logger logger = LoggerFactory.getLogger(SyncClient.class);    
16      private JMSConsumer consumer;
17      private int count=0;
18      
19      public SyncClient(JMSConsumer syncConsumer) {
20          this.consumer = syncConsumer;
21      }
22      
23      public int getCount() { return count; }
24      
25      public Message getMessage() throws JMSException {
26          Message message=consumer.receiveNoWait();
27          if (message != null) {
28              String level = message.getStringProperty("level");            
29              logger.debug("receive ({}, mode={}, pri={}{}):{}",++count,
30                      message.getJMSDeliveryMode(),
31                      message.getJMSPriority(),
32                      (level==null?"":", level="+level),
33                      message.getJMSMessageID());
34              message.acknowledge();
35          }        
36          return message;
37      }
38  }