%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.jms.ReceiveTag |
|
|
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: 1.5 $ |
|
32 | */ |
|
33 | 0 | public class ReceiveTag extends MessageOperationTag { |
34 | ||
35 | /** The Log to which logging calls will be made. */ |
|
36 | 0 | private static final Log log = LogFactory.getLog(ReceiveTag.class); |
37 | ||
38 | private String var; |
|
39 | 0 | private long timeout = -1L; |
40 | ||
41 | 0 | public ReceiveTag() { |
42 | 0 | } |
43 | ||
44 | // Tag interface |
|
45 | //------------------------------------------------------------------------- |
|
46 | public void doTag(XMLOutput output) throws JellyTagException { |
|
47 | // evaluate body as it may contain a <destination> tag |
|
48 | 0 | invokeBody(output); |
49 | ||
50 | 0 | Message message = null; |
51 | try { |
|
52 | 0 | Destination destination = getDestination(); |
53 | 0 | if ( destination == null ) { |
54 | 0 | throw new JellyTagException( "No destination specified. Either specify a 'destination' attribute or use a nested <jms:destination> tag" ); |
55 | } |
|
56 | 0 | if ( timeout > 0 ) { |
57 | 0 | if ( log.isDebugEnabled() ) { |
58 | 0 | log.debug( "Receiving message on destination: " + destination + " with timeout: " + timeout ); |
59 | } |
|
60 | ||
61 | 0 | message = getConnection().receive( destination, timeout ); |
62 | } |
|
63 | 0 | else if ( timeout == 0 ) { |
64 | 0 | if ( log.isDebugEnabled() ) { |
65 | 0 | log.debug( "Receiving message on destination: " + destination + " with No Wait" ); |
66 | } |
|
67 | ||
68 | 0 | message = getConnection().receiveNoWait( destination ); |
69 | } |
|
70 | else { |
|
71 | 0 | if ( log.isDebugEnabled() ) { |
72 | 0 | log.debug( "Receiving message on destination: " + destination ); |
73 | } |
|
74 | 0 | message = getConnection().receive( destination ); |
75 | } |
|
76 | 0 | } |
77 | catch (JMSException e) { |
|
78 | 0 | throw new JellyTagException(e); |
79 | } |
|
80 | ||
81 | 0 | onMessage( message ); |
82 | 0 | } |
83 | ||
84 | // Properties |
|
85 | //------------------------------------------------------------------------- |
|
86 | public String getVar() { |
|
87 | 0 | 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 | 0 | this.var = class="keyword">var; |
96 | 0 | } |
97 | ||
98 | public long getTimeout() { |
|
99 | 0 | 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 | 0 | this.timeout = timeout; |
108 | 0 | } |
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 | 0 | if ( message != null ) { |
119 | 0 | context.setVariable( var, message ); |
120 | } |
|
121 | else { |
|
122 | 0 | context.removeVariable( var ); |
123 | } |
|
124 | 0 | } |
125 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |