    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.spring;
    19
    20  import org.apache.commons.logging.Log;
    21  import org.apache.commons.logging.LogFactory;
    22  import org.codehaus.activemq.service.Service;
    23  import org.springframework.jms.core.JmsTemplate;
    24
    25  import javax.jms.Connection;
    26  import javax.jms.ConnectionFactory;
    27  import javax.jms.Destination;
    28  import javax.jms.JMSException;
    29  import javax.jms.Message;
    30  import javax.jms.MessageConsumer;
    31  import javax.jms.MessageListener;
    32  import javax.jms.Session;
    33
    34  /***
    35   * @author Mike Perham
    36   * @version $Revision: 1.2 $
    37   */
    38  public class SpringConsumer implements Service, MessageListener {
    39      private static final Log log = LogFactory.getLog(SpringConsumer.class);
    40
    41      private JmsTemplate template;
    42      private String myId = "foo";
    43      private Destination destination;
    44
    45      public void start() throws JMSException {
    46          String selector = "next = '" + myId + "'";
    47
    48          try {
    49              ConnectionFactory factory = template.getConnectionFactory();
    50              Connection connection = factory.createConnection();
    51              connection.setClientID(myId);
    52              connection.start();
    53
    54              Session session = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
    55              MessageConsumer consumer = session.createConsumer(destination, selector, false);
    56              consumer.setMessageListener(this);
    57          }
    58          catch (JMSException ex) {
    59              log.error("", ex.getLinkedException());
    60              throw ex;
    61          }
    62      }
    63
    64
    65      public void stop() throws JMSException {
    66      }
    67
    68      public void onMessage(Message message) {
    69          System.out.println("Received message: " + message);
    70          try {
    71              message.acknowledge();
    72          }
    73          catch (JMSException e) {
    74              log.error("Failed to acknowledge: " + e, e);
    75          }
    76      }
    77
    78      // Properties
    79      //-------------------------------------------------------------------------
    80      public Destination getDestination() {
    81          return destination;
    82      }
    83
    84      public void setDestination(Destination destination) {
    85          this.destination = destination;
    86      }
    87
    88      public String getMyId() {
    89          return myId;
    90      }
    91
    92      public void setMyId(String myId) {
    93          this.myId = myId;
    94      }
    95
    96      public JmsTemplate getTemplate() {
    97          return template;
    98      }
    99
    100     public void setTemplate(JmsTemplate template) {
    101         this.template = template;
    102     }
    103 }