1
2
3
4
5
6
7
8
9
10 package org.apache.commons.messenger.task;
11
12 import java.io.File;
13 import java.io.FileWriter;
14 import java.io.IOException;
15
16 import javax.jms.Destination;
17 import javax.jms.JMSException;
18 import javax.jms.Message;
19 import javax.jms.TextMessage;
20
21 import org.apache.commons.messenger.Messenger;
22 import org.apache.commons.messenger.MessengerManager;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.Task;
26
27
28
29
30
31
32
33
34
35 public class ConsumerTask extends Task {
36
37 private Messenger messenger;
38 private String messengerName;
39 private Destination destination;
40 private String subject;
41 private MessengerManager messengerManager;
42
43
44 private int count;
45
46
47 private File dir = new File(".");
48
49
50
51
52
53
54
55
56 public void setDir(File dir) {
57 this.dir = dir;
58 }
59
60 public Messenger getMessenger() throws JMSException {
61 if ( messenger == null ) {
62 messenger = getMessengerManager().getMessenger( getMessengerName() );
63 }
64 return messenger;
65 }
66
67
68 public void setMessenger(Messenger messenger) {
69 this.messenger = messenger;
70 }
71
72
73
74
75 public String getMessengerName() {
76 return messengerName;
77 }
78
79
80
81
82 public void setMessengerName(String messengerName) {
83 this.messengerName = messengerName;
84 }
85
86
87
88
89 public Destination getDestination() throws JMSException {
90 if ( destination == null ) {
91 destination = getMessenger().getDestination( getSubject() );
92 }
93 return destination;
94 }
95
96
97
98
99 public void setDestination(Destination destination) {
100 this.destination = destination;
101 }
102
103
104
105
106 public String getSubject() {
107 return subject;
108 }
109
110
111
112
113 public void setSubject(String subject) {
114 this.subject = subject;
115 }
116
117
118
119
120
121 public MessengerManager getMessengerManager() {
122 return messengerManager;
123 }
124
125
126
127
128 public void setMessengerManager(MessengerManager messengerManager) {
129 this.messengerManager = messengerManager;
130 }
131
132
133
134
135
136 public void setConfiguration(String uri) throws JMSException {
137 setMessengerManager( MessengerManager.load( uri ) );
138 }
139
140
141
142
143
144 public int getCount() {
145 return count;
146 }
147
148
149
150
151
152 public void setCount(int count) {
153 this.count = count;
154 }
155
156
157
158
159
160
161
162
163 public void execute() throws BuildException {
164 try {
165 Messenger messenger = getMessenger();
166 if ( messenger == null ) {
167 throw new BuildException("Must specify a valid Messenger", location );
168 }
169 Destination destination = getDestination();
170 if ( destination == null ) {
171 throw new BuildException("Must specify a valid JMS Destination", location );
172 }
173
174 if ( count > 0 ) {
175 log( "Will wait until I receive: " + count + " messages and will write to directory: " + dir );
176
177 for ( int i = 0; i < count; i++ ) {
178 Message message = messenger.receive( destination );
179 processMessage( message );
180 }
181
182 log( "Finished." );
183 }
184 else {
185 log( "Infinite loop. Will write to directory: " + dir );
186
187 while (true) {
188 Message message = messenger.receive( destination );
189 processMessage( message );
190 }
191 }
192 }
193 catch (IOException e) {
194 log( "Caught exception: " + e, Project.MSG_ERR );
195 throw new BuildException(e, location);
196 }
197 catch (JMSException e) {
198 log( "Caught exception: " + e, Project.MSG_ERR );
199 throw new BuildException(e, location);
200 }
201 finally {
202 try {
203
204 messenger.close();
205 }
206 catch (Exception e) {
207
208 }
209 }
210 }
211
212
213
214
215 protected void processMessage(Message message) throws IOException, JMSException {
216 log( "Received message to: " + message );
217
218 String text = null;
219 if ( message instanceof TextMessage ) {
220 TextMessage textMessage = (TextMessage) message;
221 text = textMessage.toString();
222 }
223 else {
224
225
226 text = message.toString();
227 }
228 processMessageText(text);
229 }
230
231
232
233
234 protected void processMessageText(String text) throws IOException {
235 FileWriter writer = new FileWriter( dir );
236 writer.write ( text );
237 writer.close();
238 }
239 }
240
241