KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > spring > SpringConsumer


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.activemq.spring;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.springframework.jms.core.JmsTemplate;
24
25 import javax.jms.Connection JavaDoc;
26 import javax.jms.ConnectionFactory JavaDoc;
27 import javax.jms.Destination JavaDoc;
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.Message JavaDoc;
30 import javax.jms.MessageConsumer JavaDoc;
31 import javax.jms.MessageListener JavaDoc;
32 import javax.jms.Session JavaDoc;
33
34 public class SpringConsumer extends ConsumerBean implements MessageListener JavaDoc {
35     private static final Log log = LogFactory.getLog(SpringConsumer.class);
36
37     private JmsTemplate template;
38     private String JavaDoc myId = "foo";
39     private Destination JavaDoc destination;
40     private Connection JavaDoc connection;
41     private Session JavaDoc session;
42     private MessageConsumer JavaDoc consumer;
43
44     public void start() throws JMSException JavaDoc {
45         String JavaDoc selector = "next = '" + myId + "'";
46
47         try {
48             ConnectionFactory JavaDoc factory = template.getConnectionFactory();
49             connection = factory.createConnection();
50
51             // we might be a reusable connection in spring
52
// so lets only set the client ID once if its not set
53
synchronized (connection) {
54                 if (connection.getClientID() == null) {
55                     connection.setClientID(myId);
56                 }
57             }
58
59             connection.start();
60
61             session = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
62             consumer = session.createConsumer(destination, selector, false);
63             consumer.setMessageListener(this);
64         }
65         catch (JMSException JavaDoc ex) {
66             log.error("", ex);
67             throw ex;
68         }
69     }
70
71
72     public void stop() throws JMSException JavaDoc {
73         if( consumer!=null )
74             consumer.close();
75         if( session!=null )
76             session.close();
77         if( connection!=null )
78             connection.close();
79     }
80
81     public void onMessage(Message JavaDoc message) {
82         super.onMessage(message);
83         try {
84             message.acknowledge();
85         }
86         catch (JMSException JavaDoc e) {
87             log.error("Failed to acknowledge: " + e, e);
88         }
89     }
90
91     // Properties
92
//-------------------------------------------------------------------------
93
public Destination JavaDoc getDestination() {
94         return destination;
95     }
96
97     public void setDestination(Destination JavaDoc destination) {
98         this.destination = destination;
99     }
100
101     public String JavaDoc getMyId() {
102         return myId;
103     }
104
105     public void setMyId(String JavaDoc myId) {
106         this.myId = myId;
107     }
108
109     public JmsTemplate getTemplate() {
110         return template;
111     }
112
113     public void setTemplate(JmsTemplate template) {
114         this.template = template;
115     }
116 }
117
Popular Tags