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.Message;
19 import javax.jms.MessageListener;
20
21 import org.apache.commons.jelly.JellyContext;
22 import org.apache.commons.jelly.JellyTagException;
23 import org.apache.commons.jelly.Script;
24 import org.apache.commons.jelly.TagSupport;
25 import org.apache.commons.jelly.XMLOutput;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /***
31 * This tag creates a JMS MessageListener which will invoke this
32 * tag's body whenever a JMS Message is received. The JMS Message
33 * will be available via a variable, which defaults to the 'message'
34 * variable name, but can be overloaded by the var attribute.
35 *
36 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
37 * @version $Revision: 155420 $
38 */
39 public class OnMessageTag extends TagSupport {
40
41 /*** The Log to which logging calls will be made. */
42 private static final Log log = LogFactory.getLog(OnMessageTag.class);
43
44 private String var = "message";
45
46 public OnMessageTag() {
47 }
48
49
50
51 public void doTag(XMLOutput output) throws JellyTagException {
52 ConsumerTag tag = (ConsumerTag) findAncestorWithClass(ConsumerTag.class);
53 if (tag == null) {
54 throw new JellyTagException("This tag must be nested within a ConsumerTag like the subscribe tag");
55 }
56
57 final JellyContext childContext = context.newJellyContext();
58 final Script script = getBody();
59 final XMLOutput childOutput = output;
60
61 MessageListener listener = new MessageListener() {
62 public void onMessage(Message message) {
63 childContext.setVariable(var, message);
64 try {
65 script.run(childContext, childOutput);
66 }
67 catch (Exception e) {
68 log.error("Caught exception processing message: " + message + ". Exception: " + e, e);
69 }
70 }
71 };
72
73
74 tag.setMessageListener(listener);
75 }
76
77
78
79
80
81 /***
82 * Sets the name of the variable used to make the JMS message available to this tags
83 * body when a message is received.
84 */
85 public void setVar(String var) {
86 this.var = var;
87 }
88 }