KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > jms > JmsReceiverComponent


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 package org.apache.servicemix.components.jms;
18
19 import javax.jbi.JBIException;
20 import javax.jms.Connection JavaDoc;
21 import javax.jms.ConnectionFactory JavaDoc;
22 import javax.jms.Destination JavaDoc;
23 import javax.jms.JMSException JavaDoc;
24 import javax.jms.MessageConsumer JavaDoc;
25 import javax.jms.Session JavaDoc;
26
27 import org.springframework.beans.factory.InitializingBean;
28 import org.springframework.jms.core.JmsTemplate;
29
30 /**
31  * A component which uses a {@link JmsTemplate} to consume messages from a
32  * destination.
33  *
34  * @version $Revision: 441456 $
35  */

36 public class JmsReceiverComponent extends JmsInBinding implements InitializingBean {
37     private JmsTemplate template;
38     private String JavaDoc selector;
39     private MessageConsumer JavaDoc consumer;
40     private ConnectionFactory JavaDoc connectionFactory;
41     private Connection JavaDoc connection;
42     private Session JavaDoc session;
43
44     public void afterPropertiesSet() throws Exception JavaDoc {
45         if (template == null) {
46             throw new IllegalArgumentException JavaDoc("Must have a template set");
47         }
48     }
49     
50     public void start() throws JBIException {
51         // Start receiving messages only when the component has actually been started.
52
super.start();
53         try {
54             connectionFactory = template.getConnectionFactory();
55             /*
56              * Component code did not work for JMS 1.02 compliant provider because uses APIs
57              * that did not exist in JMS 1.02 : ConnectionFactory.createConnection,
58              * Connection.createSession
59              */

60             if (template instanceof org.springframework.jms.core.JmsTemplate102) {
61                 //Note1 - would've preferred to call JmsTemplate102 methods but they are protected.
62
if (template.isPubSubDomain()) {
63                     javax.jms.TopicConnection JavaDoc tc;
64                     connection = tc = ((javax.jms.TopicConnectionFactory JavaDoc)connectionFactory).createTopicConnection();
65                     session = tc.createTopicSession(template.isSessionTransacted(), template.getSessionAcknowledgeMode());
66                 }
67                 else {
68                     javax.jms.QueueConnection JavaDoc qc;
69                     connection = qc = ((javax.jms.QueueConnectionFactory JavaDoc)connectionFactory).createQueueConnection();
70                     session = qc.createQueueSession(template.isSessionTransacted(), template.getSessionAcknowledgeMode());
71                 }
72             } else { // JMS 1.1 style
73
connection = connectionFactory.createConnection();
74                 session = connection.createSession(template.isSessionTransacted(), template.getSessionAcknowledgeMode());
75             }
76
77             Destination JavaDoc defaultDestination = template.getDefaultDestination();
78             if (defaultDestination == null) {
79                 defaultDestination = template.getDestinationResolver().resolveDestinationName(session, template.getDefaultDestinationName(),
80                         template.isPubSubDomain());
81             }
82
83             /*
84              * Component code did not work for JMS 1.02 compliant provider because uses APIs
85              * that did not exist in JMS 1.02: Session.createConsumer
86              */

87             if (template instanceof org.springframework.jms.core.JmsTemplate102) {
88                 //Note1 - would've preferred to call JmsTemplate102.createConsumer but it is protected. Code below is same.
89
//Note2 - assert that defaultDestination is correct type according to isPubSubDomain()
90
if (template.isPubSubDomain()) {
91                     consumer = ((javax.jms.TopicSession JavaDoc)session).createSubscriber((javax.jms.Topic JavaDoc)defaultDestination, selector, template.isPubSubNoLocal());
92                 } else {
93                     consumer = ((javax.jms.QueueSession JavaDoc)session).createReceiver((javax.jms.Queue JavaDoc)defaultDestination, selector);
94                 }
95             } else { // JMS 1.1 style
96
consumer = session.createConsumer(defaultDestination, selector);
97             }
98             connection.start();
99             consumer.setMessageListener(this);
100         } catch (JMSException JavaDoc e) {
101             throw new JBIException("Unable to start jms component");
102         }
103     }
104
105     public void stop() throws JBIException {
106         try {
107             if (consumer != null) {
108                 consumer.close();
109             }
110             if (session != null) {
111                 session.close();
112             }
113             if (connection != null) {
114                 connection.close();
115             }
116         } catch (JMSException JavaDoc e) {
117             throw new JBIException("Unable to stop jms component");
118         } finally {
119             connection = null;
120             session = null;
121             consumer = null;
122         }
123     }
124
125     public JmsTemplate getTemplate() {
126         return template;
127     }
128
129     public void setTemplate(JmsTemplate template) {
130         this.template = template;
131     }
132
133     public String JavaDoc getSelector() {
134         return selector;
135     }
136
137     public void setSelector(String JavaDoc selector) {
138         this.selector = selector;
139     }
140
141 }
142
143
Popular Tags