View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.jms;
17  
18  import javax.jms.Destination;
19  import javax.jms.Message;
20  import javax.jms.JMSException;
21  
22  import org.apache.commons.jelly.JellyTagException;
23  import org.apache.commons.jelly.XMLOutput;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /*** Receives a JMS message.
29    *
30    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31    * @version $Revision: 155420 $
32    */
33  public class ReceiveTag extends MessageOperationTag {
34  
35      /*** The Log to which logging calls will be made. */
36      private static final Log log = LogFactory.getLog(ReceiveTag.class);
37  
38      private String var;
39      private long timeout = -1L;
40  
41      public ReceiveTag() {
42      }
43  
44      // Tag interface
45      //-------------------------------------------------------------------------
46      public void doTag(XMLOutput output) throws JellyTagException {
47          // evaluate body as it may contain a <destination> tag
48          invokeBody(output);
49  
50          Message message = null;
51          try {
52              Destination destination = getDestination();
53              if ( destination == null ) {
54                  throw new JellyTagException( "No destination specified. Either specify a 'destination' attribute or use a nested <jms:destination> tag" );
55              }
56              if ( timeout > 0 ) {
57                  if ( log.isDebugEnabled() ) {
58                      log.debug( "Receiving message on destination: " + destination + " with timeout: " + timeout );
59                  }
60  
61                  message = getConnection().receive( destination, timeout );
62              }
63              else if ( timeout == 0 ) {
64                  if ( log.isDebugEnabled() ) {
65                      log.debug( "Receiving message on destination: " + destination + " with No Wait" );
66                  }
67  
68                  message = getConnection().receiveNoWait( destination );
69              }
70              else {
71                  if ( log.isDebugEnabled() ) {
72                      log.debug( "Receiving message on destination: " + destination );
73                  }
74                  message = getConnection().receive( destination );
75              }
76          }
77          catch (JMSException e) {
78              throw new JellyTagException(e);
79          }
80  
81          onMessage( message );
82      }
83  
84      // Properties
85      //-------------------------------------------------------------------------
86      public String getVar() {
87          return var;
88      }
89  
90      /***
91       * Sets the variable name to create for the received message, which will be null if no
92       * message could be returned in the given time period.
93       */
94      public void setVar(String var) {
95          this.var = var;
96      }
97  
98      public long getTimeout() {
99          return timeout;
100     }
101 
102     /***
103      * Sets the timeout period in milliseconds to wait for a message. A value
104      * of -1 will wait forever for a message.
105      */
106     public void setTimeout(long timeout) {
107         this.timeout = timeout;
108     }
109 
110     // Implementation methods
111     //-------------------------------------------------------------------------
112 
113     /***
114      * A strategy method which processes the incoming message, allowing derived classes
115      * to implement different processing methods
116      */
117     protected void onMessage( Message message ) {
118         if ( message != null ) {
119             context.setVariable( var, message );
120         }
121         else {
122             context.removeVariable( var );
123         }
124     }
125 }