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.JMSException;
20 import javax.jms.MessageListener;
21
22 import org.apache.commons.jelly.JellyTagException;
23 import org.apache.commons.jelly.XMLOutput;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 /***
28 * Performs a subscription to some JMS connection to a destination maybe with a selector.
29 * A JMS MessageListener can be specified, or a special child tag can explicitly set it on
30 * its parent (so a special tag could construct a MessageListener object and register it with this tag).
31 *
32 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
33 * @version $Revision: 155420 $
34 */
35 public class SubscribeTag extends MessageOperationTag implements ConsumerTag {
36
37 /*** The Log to which logging calls will be made. */
38 private static final Log log = LogFactory.getLog(SubscribeTag.class);
39
40 /*** the JMS Selector for the subscription */
41 private String selector;
42
43 /*** The JMS MessageListener used to create the subscription */
44 private MessageListener messageListener;
45
46 public SubscribeTag() {
47 }
48
49
50
51 public void doTag(XMLOutput output) throws JellyTagException {
52
53
54 invokeBody(output);
55
56 MessageListener listener = getMessageListener();
57 if (listener == null) {
58 throw new JellyTagException( "No messageListener attribute is specified so could not subscribe" );
59 }
60
61
62 setMessageListener(null);
63
64
65 Destination destination = null;
66 try {
67 destination = getDestination();
68 }
69 catch (JMSException e) {
70 throw new JellyTagException(e);
71 }
72
73 if ( destination == null ) {
74 throw new JellyTagException( "No destination specified. Either specify a 'destination' attribute or use a nested <jms:destination> tag" );
75 }
76
77 if ( log.isDebugEnabled() ) {
78 log.debug( "About to consume to: " + destination + " with listener: " + listener );
79 }
80
81 log.info( "About to consume to: " + destination + " with listener: " + listener );
82
83 try {
84 if (selector == null ) {
85 getConnection().addListener( destination, listener );
86 }
87 else {
88 getConnection().addListener( destination, selector, listener );
89 }
90 }
91 catch (JMSException e) {
92 throw new JellyTagException(e);
93 }
94 }
95
96
97
98
99 /***
100 * Sets the optional JMS Message selector for the subscription
101 */
102 public void setSelector(String selector) {
103 this.selector = selector;
104 }
105
106
107 /***
108 * Returns the messageListener.
109 * @return MessageListener
110 */
111 public MessageListener getMessageListener() {
112 return messageListener;
113 }
114
115
116 /***
117 * Sets the JMS messageListener used ot consume JMS messages on the given destination
118 */
119 public void setMessageListener(MessageListener messageListener) {
120 this.messageListener = messageListener;
121 }
122
123 }