View Javadoc

1   /*
2    * Copyright (C) The Apache Software Foundation. All rights reserved.
3    *
4    * This software is published under the terms of the Apache Software License
5    * version 1.1, a copy of which has been included with this distribution in
6    * the LICENSE file.
7    * 
8    * $Id: ConsumerTask.java 155459 2005-02-26 13:24:44Z dirkv $
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   * <p><code>ConsumerTask</code> is an Ant task which will 
29   * publish all of the given text files as a JMS Text Message
30   * using a given JMS Connection (Messenger) and a Destination
31   *
32   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
33   * @version $Revision: 155459 $
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      /** the number of messages to receive */
44      private int count;
45  
46      /** the output directory */
47      private File dir = new File(".");    
48      
49      // Properties
50      //-------------------------------------------------------------------------
51      
52  
53      /** 
54       * Sets the output directory 
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      /** Sets the Messenger to be used */
68      public void setMessenger(Messenger messenger) {
69          this.messenger = messenger;
70      }
71      
72      /** Getter for property messengerName.
73       * @return Value of property messengerName.
74       */
75      public String getMessengerName() {
76          return messengerName;
77      }
78  
79      /** Setter for property messengerName.
80       * @param messengerName New value of property messengerName.
81       */
82      public void setMessengerName(String messengerName) {
83          this.messengerName = messengerName;
84      }
85  
86      /** Getter for property destination.
87       * @return Value of property destination.
88       */
89      public Destination getDestination() throws JMSException {
90          if ( destination == null ) {
91              destination = getMessenger().getDestination( getSubject() );
92          }
93          return destination;
94      }
95  
96      /** Setter for property destination.
97       * @param destination New value of property destination.
98       */
99      public void setDestination(Destination destination) {
100         this.destination = destination;
101     }
102 
103     /** Getter for property subject.
104      * @return Value of property subject.
105      */
106     public String getSubject() {
107         return subject;
108     }
109 
110     /** Setter for property subject.
111      * @param subject New value of property subject.
112      */
113     public void setSubject(String subject) {
114         this.subject = subject;
115     }
116     
117     
118     /** Getter for property messengerManager.
119      * @return Value of property messengerManager.
120      */
121     public MessengerManager getMessengerManager() {
122         return messengerManager;
123     }
124     
125     /** Setter for property messengerManager.
126      * @param messengerManager New value of property messengerManager.
127      */
128     public void setMessengerManager(MessengerManager messengerManager) {
129         this.messengerManager = messengerManager;
130     }
131 
132     /** 
133      * Sets the URI of the Messenger.xml configuration document to use
134      * to configure the messengers to use for this task.
135      */
136     public void setConfiguration(String uri) throws JMSException {
137         setMessengerManager( MessengerManager.load( uri ) );
138     }
139     
140     /** 
141      * @return the number of messages to receive.
142      * A number less than or equal to 0 will receive messages forever
143      */
144     public int getCount() {
145         return count;
146     }
147     
148     /** 
149      * Setter for the number of messages to receive.
150      * A number less than or equal to 0 will receive messages forever
151      */
152     public void setCount(int count) {
153         this.count = count;
154     }
155     
156     
157     // Task interface
158     //-------------------------------------------------------------------------
159     
160     /**
161      * Performs the copy operation.
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                 // close the JMS connection to release any background threads        
204                 messenger.close();
205             }
206             catch (Exception e) {
207                 // ignore close exceptions
208             }
209         }
210     }
211 
212     /**
213      * Processes a given message
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             // #### bit of a hack!!!
225             // ideally we need an XML format for message persistence
226             text = message.toString();
227         }       
228         processMessageText(text);
229     }
230     
231     /** 
232      * Writes the given text to a file
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