001    /*
002     * Copyright (C) The Apache Software Foundation. All rights reserved.
003     *
004     * This software is published under the terms of the Apache Software License
005     * version 1.1, a copy of which has been included with this distribution in
006     * the LICENSE file.
007     * 
008     * $Id: Consumer.java 155459 2005-02-26 13:24:44Z dirkv $
009     */
010    package org.apache.commons.messenger.tool;
011    
012    import java.io.FileWriter;
013    import java.io.OutputStreamWriter;
014    import java.io.PrintWriter;
015    import java.io.Writer;
016    
017    import javax.jms.Destination;
018    import javax.jms.JMSException;
019    import javax.jms.Message;
020    import javax.jms.TextMessage;
021    
022    import org.apache.commons.messenger.Messenger;
023    import org.apache.commons.messenger.MessengerManager;
024    
025    /** <p><code>Consumer</code> is a sample program that 
026      * consumes a single message and either writes it to 
027      * either stanard output or a named file.</p>
028      *
029      * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
030      * @version $Revision: 155459 $
031      */
032    public class Consumer {
033    
034        private Messenger messenger;
035    
036        public static void main(String[] args) {
037            try {
038                Consumer client = new Consumer();
039                client.run( args );
040            }
041            catch (JMSException e) {
042                System.out.println( "Caught: " + e );
043                Exception linked = e.getLinkedException();
044                if ( linked != null ) {
045                    System.out.println( "Underlying exception: " + linked );
046                    linked.printStackTrace();
047                }
048                else {
049                    e.printStackTrace();
050                }
051            }
052            catch (Exception e) {
053                System.out.println( "Caught: " + e );
054                e.printStackTrace();
055            }
056        }
057        
058        public void run(String[] args) throws Exception {
059            if ( args.length < 2 ) {
060                System.out.println( "Usage: Consumer messengerName destination [fileName]" );
061                System.out.println( "If no fileName is provided then the current input stream is used" );
062                return;
063            }
064            String name = args[0];
065            String subject = args[1];
066            messenger = MessengerManager.get( name );
067            if ( messenger == null ) {
068                throw new JMSException( "No such messenger called: " + name );
069            }
070            Destination destination = messenger.getDestination( subject );
071            if ( destination == null ) {
072                throw new JMSException( "Could not find destination: " + subject );
073            }
074    
075            Writer writer = ( args.length > 2 ) 
076                ? new FileWriter( args[2] )
077                : new OutputStreamWriter( System.out );
078             
079            Message message = messenger.receive( destination );
080            
081            writeMessage( message, writer );
082            
083            // close the JMS connection to release any background threads        
084            messenger.close();
085        }    
086        
087        /** Writes the given message to the Writer */
088        protected void writeMessage(Message message, Writer out) throws Exception {
089            PrintWriter writer = new PrintWriter(out);
090            if ( message instanceof TextMessage ) {
091                TextMessage textMessage = (TextMessage) message;
092                writer.println( textMessage.getText() );
093            }
094            writer.close();
095        }
096    }
097    
098