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.MessageListener;
19  
20  import org.apache.commons.jelly.JellyTagException;
21  import org.apache.commons.jelly.XMLOutput;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import org.apache.commons.messenger.tool.StopWatchMessageListener;
27  
28  /***
29   * This tag can be used to measure the amount of time it takes to process JMS messages.
30   * This tag can be wrapped around any custom JMS tag which consumes JMS messages.
31   *
32   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
33   * @version $Revision: 155420 $
34   */
35  public class StopwatchTag extends MessageOperationTag implements ConsumerTag {
36  
37      /*** the underlying MessageListener */
38      private MessageListener messageListener;
39  
40      /*** The Log to which logging calls will be made. */
41      private Log log = LogFactory.getLog( StopwatchTag.class );
42  
43      /*** the message group size */
44      private int groupSize = 1000;
45  
46      public StopwatchTag() {
47      }
48  
49      // Tag interface
50      //-------------------------------------------------------------------------
51      public void doTag(XMLOutput output) throws JellyTagException {
52  
53          // evaluate body as it may contain child tags to register a MessageListener
54          invokeBody(output);
55  
56          MessageListener listener = getMessageListener();
57  
58          ConsumerTag tag = (ConsumerTag) findAncestorWithClass(ConsumerTag.class);
59          if (tag == null) {
60              throw new JellyTagException("This tag must be nested within a ConsumerTag like the subscribe tag");
61          }
62  
63          // clear the listener for the next tag invocation, if caching is employed
64          setMessageListener(null);
65  
66          StopWatchMessageListener stopWatch = new StopWatchMessageListener(listener);
67          stopWatch.setGroupSize(groupSize);
68          stopWatch.setLog(log);
69  
70          // perform the consumption
71          tag.setMessageListener(stopWatch);
72      }
73  
74      // Properties
75      //-------------------------------------------------------------------------
76  
77      /***
78       * @return the number of messages in the group before the performance statistics are logged
79       */
80      public int getGroupSize() {
81          return groupSize;
82      }
83  
84      /***
85       * Sets the number of messages in the group before the performance statistics are logged
86       */
87      public void setGroupSize(int groupSize) {
88          this.groupSize = groupSize;
89      }
90  
91  
92      /***
93       * @return the logger to which statistic messages will be sent
94       */
95      public Log getLog() {
96          return log;
97      }
98  
99      /***
100      * Sets the logger to which statistic messages will be sent
101      */
102     public void setLog(Log log) {
103         this.log = log;
104     }
105 
106     /***
107      * @return the MessageListener which this listener delegates to
108      */
109     public MessageListener getMessageListener() {
110         return messageListener;
111     }
112 
113     /***
114      * Sets the JMS messageListener used to consume JMS messages on the given destination
115      */
116     public void setMessageListener(MessageListener messageListener) {
117         this.messageListener = messageListener;
118     }
119 
120 }