View Javadoc

1   /***
2    *
3    * Copyright 2005 LogicBlaze, Inc.
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.logicblaze.lingo.jms;
19  
20  import org.springframework.beans.factory.DisposableBean;
21  import org.springframework.beans.factory.InitializingBean;
22  
23  import javax.jms.Destination;
24  import javax.jms.JMSException;
25  import javax.jms.MessageConsumer;
26  import javax.jms.Session;
27  import javax.jms.Topic;
28  
29  /***
30   * A JMS MessageListener that exports the specified service bean as a JMS
31   * service endpoint, accessible via a JMS proxy. <p/>
32   * <p>
33   * Note: JMS services exported with this class can be accessed by any JMS
34   * client, as there isn't any special handling involved.
35   * 
36   * @author James Strachan
37   * @see JmsProxyFactoryBean
38   */
39  public class JmsServiceExporter extends JmsServiceExporterMessageListener implements InitializingBean, DisposableBean {
40      private Destination destination;
41      private MessageConsumer consumer;
42      private String messageSelector;
43      private String subscriberName;
44      private boolean noLocal;
45  
46      public void afterPropertiesSet() throws Exception {
47          super.afterPropertiesSet();
48  
49          // do we have a destination specified, if so consume
50          if (destination != null) {
51              consumer = createConsumer();
52              consumer.setMessageListener(this);
53          }
54      }
55  
56      public void destroy() throws Exception {
57          if (consumer != null) {
58              consumer.close();
59          }
60          super.destroy();
61      }
62  
63      public Destination getDestination() {
64          return destination;
65      }
66  
67      /***
68       * If specified then the service will be auto-subscribed to this destination
69       */
70      public void setDestination(Destination destination) {
71          this.destination = destination;
72      }
73  
74      public String getMessageSelector() {
75          return messageSelector;
76      }
77  
78      /***
79       * Sets the message selector applied to the subscription
80       */
81      public void setMessageSelector(String messageSelector) {
82          this.messageSelector = messageSelector;
83      }
84  
85      public boolean isNoLocal() {
86          return noLocal;
87      }
88  
89      /***
90       * Sets whether or not topic subscriptions should receive locally produced
91       * messages
92       */
93      public void setNoLocal(boolean noLocal) {
94          this.noLocal = noLocal;
95      }
96  
97      public String getSubscriberName() {
98          return subscriberName;
99      }
100 
101     /***
102      * Sets the durable subscriber name and enables a durable subscription.
103      */
104     public void setSubscriberName(String subscriberName) {
105         this.subscriberName = subscriberName;
106     }
107 
108     // Implementation methods
109     // -------------------------------------------------------------------------
110 
111 
112     /***
113      * Factory method to create the consumer
114      */
115     protected MessageConsumer createConsumer() throws JMSException {
116         Session session = getResponseRequestor().getSession();
117         if (subscriberName != null) {
118             if (destination instanceof Topic) {
119                 Topic topic = (Topic) destination;
120                 return session.createDurableSubscriber(topic, subscriberName, messageSelector, noLocal);
121             }
122             else {
123                 throw new IllegalArgumentException("Cannot specify the subscriberName property when using a Queue destination");
124             }
125 
126         }
127         else {
128             return session.createConsumer(destination, messageSelector, noLocal);
129         }
130     }
131 
132 }