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: Consumer.java 155459 2005-02-26 13:24:44Z dirkv $
9    */
10  package org.apache.commons.messenger.tool;
11  
12  import java.io.FileWriter;
13  import java.io.OutputStreamWriter;
14  import java.io.PrintWriter;
15  import java.io.Writer;
16  
17  import javax.jms.Destination;
18  import javax.jms.JMSException;
19  import javax.jms.Message;
20  import javax.jms.TextMessage;
21  
22  import org.apache.commons.messenger.Messenger;
23  import org.apache.commons.messenger.MessengerManager;
24  
25  /** <p><code>Consumer</code> is a sample program that 
26    * consumes a single message and either writes it to 
27    * either stanard output or a named file.</p>
28    *
29    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
30    * @version $Revision: 155459 $
31    */
32  public class Consumer {
33  
34      private Messenger messenger;
35  
36      public static void main(String[] args) {
37          try {
38              Consumer client = new Consumer();
39              client.run( args );
40          }
41          catch (JMSException e) {
42              System.out.println( "Caught: " + e );
43              Exception linked = e.getLinkedException();
44              if ( linked != null ) {
45                  System.out.println( "Underlying exception: " + linked );
46                  linked.printStackTrace();
47              }
48              else {
49                  e.printStackTrace();
50              }
51          }
52          catch (Exception e) {
53              System.out.println( "Caught: " + e );
54              e.printStackTrace();
55          }
56      }
57      
58      public void run(String[] args) throws Exception {
59          if ( args.length < 2 ) {
60              System.out.println( "Usage: Consumer messengerName destination [fileName]" );
61              System.out.println( "If no fileName is provided then the current input stream is used" );
62              return;
63          }
64          String name = args[0];
65          String subject = args[1];
66          messenger = MessengerManager.get( name );
67          if ( messenger == null ) {
68              throw new JMSException( "No such messenger called: " + name );
69          }
70          Destination destination = messenger.getDestination( subject );
71          if ( destination == null ) {
72              throw new JMSException( "Could not find destination: " + subject );
73          }
74  
75          Writer writer = ( args.length > 2 ) 
76              ? new FileWriter( args[2] )
77              : new OutputStreamWriter( System.out );
78           
79          Message message = messenger.receive( destination );
80          
81          writeMessage( message, writer );
82          
83          // close the JMS connection to release any background threads        
84          messenger.close();
85      }    
86      
87      /** Writes the given message to the Writer */
88      protected void writeMessage(Message message, Writer out) throws Exception {
89          PrintWriter writer = new PrintWriter(out);
90          if ( message instanceof TextMessage ) {
91              TextMessage textMessage = (TextMessage) message;
92              writer.println( textMessage.getText() );
93          }
94          writer.close();
95      }
96  }
97  
98