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.logicblaze.lingo.jms.impl.DefaultJmsProducer;
21  import org.logicblaze.lingo.jms.impl.OneWayRequestor;
22  import org.springframework.beans.factory.DisposableBean;
23  import org.springframework.beans.factory.InitializingBean;
24  import org.springframework.remoting.support.RemoteInvocationResult;
25  
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.Session;
32  
33  /***
34   * A JMS MessageListener that exports the specified service bean as a JMS service
35   * endpoint, accessible via a JMS proxy.
36   * <p/>
37   * <p>Note: JMS services exported with this class can be accessed by
38   * any JMS client, as there isn't any special handling involved.
39   *
40   * @author James Strachan
41   * @see JmsProxyFactoryBean
42   */
43  public class JmsServiceExporter extends JmsServiceExporterSupport implements InitializingBean, DisposableBean {
44      private JmsProducer producer;
45      private ConnectionFactory connectionFactory;
46      private Destination destination;
47      private MessageConsumer consumer;
48  
49      public void afterPropertiesSet() throws Exception {
50          if (producer == null) {
51              if (connectionFactory == null) {
52                  throw new IllegalArgumentException("requestor or connectionFactory is required");
53              }
54              else {
55                  producer = DefaultJmsProducer.newInstance(connectionFactory);
56              }
57          }
58          Requestor responseRequestor = getResponseRequestor();
59          if (responseRequestor == null) {
60              setResponseRequestor(new OneWayRequestor(producer, null));
61          }
62  
63          // do we have a destination specified, if so consume
64          if (destination != null) {
65              Session session = producer.getSession();
66              consumer = session.createConsumer(destination);
67              consumer.setMessageListener(this);
68          }
69  
70          super.afterPropertiesSet();
71      }
72  
73      public void destroy() throws Exception {
74          if (consumer != null) {
75              consumer.close();
76          }
77      }
78  
79      public JmsProducer getProducer() {
80          return producer;
81      }
82  
83      public void setProducer(JmsProducer producer) {
84          this.producer = producer;
85      }
86  
87      public ConnectionFactory getConnectionFactory() {
88          return connectionFactory;
89      }
90  
91      /***
92       * Used to create a default {@link JmsProducer} if no producer is explicitly
93       * configured.
94       */
95      public void setConnectionFactory(ConnectionFactory connectionFactory) {
96          this.connectionFactory = connectionFactory;
97      }
98  
99      public Destination getDestination() {
100         return destination;
101     }
102 
103     /***
104      * If specified then the service will be auto-subscribed to this destination
105      */
106     public void setDestination(Destination destination) {
107         this.destination = destination;
108     }
109 
110     /***
111      * Send the given RemoteInvocationResult as a JMS message to the originator
112      *
113      * @param message current HTTP message
114      * @param result  the RemoteInvocationResult object
115      * @throws javax.jms.JMSException if thrown by trying to send the message
116      */
117     protected void writeRemoteInvocationResult(final Message message, final RemoteInvocationResult result) throws JMSException {
118         Message responseMessage = createResponseMessage(producer.getSession(), message, result);
119         producer.getMessageProducer().send(message.getJMSReplyTo(), responseMessage);
120     }
121 
122 }