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.jencks;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.springframework.beans.factory.InitializingBean;
23  import org.springframework.jms.core.JmsTemplate;
24  import org.springframework.jms.core.MessageCreator;
25  
26  import javax.jms.Destination;
27  import javax.jms.JMSException;
28  import javax.jms.Message;
29  import javax.jms.MessageListener;
30  import javax.jms.Session;
31  
32  /***
33   * @version $Revision: 1.1.1.1 $
34   */
35  public class TestReplyBean implements MessageListener, InitializingBean {
36      private static final Log log = LogFactory.getLog(TestReplyBean.class);
37      private static int globalCounter = 0;
38  
39      private JmsTemplate jmsTemplate;
40      private int counter = getNextValue();
41  
42      public void onMessage(final Message message) {
43          log.info("TestReplyBean: " + counter + " received message: " + message);
44  
45          // now lets send a reply message
46          try {
47              Destination replyTo = message.getJMSReplyTo();
48              if (replyTo != null) {
49                  log.info("About to send reply to: " + replyTo);
50                  //jmsTemplate.convertAndSend(replyTo, "This is the reply from message: " + message);
51                  jmsTemplate.send(replyTo, new MessageCreator() {
52                      public Message createMessage(Session session) throws JMSException {
53                          return session.createTextMessage("This is the reply from message: " + message);
54                      }
55                  });
56              }
57              else {
58                  log.warn("No JMSReployTo destination so cannot send reply for: " + message);
59              }
60          }
61          catch (JMSException e) {
62              log.error("Failed to send message: " + e, e);
63          }
64      }
65  
66      /***
67       * Provide container construction validation
68       */
69      public void afterPropertiesSet() throws Exception {
70          if (jmsTemplate == null) {
71              throw new IllegalArgumentException("jmsTemplate property must be set");
72          }
73      }
74  
75      // Properties
76      //-------------------------------------------------------------------------
77      public JmsTemplate getJmsTemplate() {
78          return jmsTemplate;
79      }
80  
81      public void setJmsTemplate(JmsTemplate jmsTemplate) {
82          this.jmsTemplate = jmsTemplate;
83      }
84  
85      protected static synchronized int getNextValue() {
86          return ++globalCounter;
87      }
88  
89  }