1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
45
46 public void doTag(XMLOutput output) throws JellyTagException {
47
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
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
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 }