Coverage Report - org.apache.commons.messenger.tool.Consumer
 
Classes in this File Line Coverage Branch Coverage Complexity
Consumer
0%
0/38
0%
0/12
4.333
 
 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  0
 public class Consumer {
 33  
 
 34  
     private Messenger messenger;
 35  
 
 36  
     public static void main(String[] args) {
 37  
         try {
 38  0
             Consumer client = new Consumer();
 39  0
             client.run( args );
 40  
         }
 41  0
         catch (JMSException e) {
 42  0
             System.out.println( "Caught: " + e );
 43  0
             Exception linked = e.getLinkedException();
 44  0
             if ( linked != null ) {
 45  0
                 System.out.println( "Underlying exception: " + linked );
 46  0
                 linked.printStackTrace();
 47  
             }
 48  
             else {
 49  0
                 e.printStackTrace();
 50  
             }
 51  
         }
 52  0
         catch (Exception e) {
 53  0
             System.out.println( "Caught: " + e );
 54  0
             e.printStackTrace();
 55  0
         }
 56  0
     }
 57  
     
 58  
     public void run(String[] args) throws Exception {
 59  0
         if ( args.length < 2 ) {
 60  0
             System.out.println( "Usage: Consumer messengerName destination [fileName]" );
 61  0
             System.out.println( "If no fileName is provided then the current input stream is used" );
 62  0
             return;
 63  
         }
 64  0
         String name = args[0];
 65  0
         String subject = args[1];
 66  0
         messenger = MessengerManager.get( name );
 67  0
         if ( messenger == null ) {
 68  0
             throw new JMSException( "No such messenger called: " + name );
 69  
         }
 70  0
         Destination destination = messenger.getDestination( subject );
 71  0
         if ( destination == null ) {
 72  0
             throw new JMSException( "Could not find destination: " + subject );
 73  
         }
 74  
 
 75  0
         Writer writer = ( args.length > 2 ) 
 76  
             ? new FileWriter( args[2] )
 77  
             : new OutputStreamWriter( System.out );
 78  
          
 79  0
         Message message = messenger.receive( destination );
 80  
         
 81  0
         writeMessage( message, writer );
 82  
         
 83  
         // close the JMS connection to release any background threads        
 84  0
         messenger.close();
 85  0
     }    
 86  
     
 87  
     /** Writes the given message to the Writer */
 88  
     protected void writeMessage(Message message, Writer out) throws Exception {
 89  0
         PrintWriter writer = new PrintWriter(out);
 90  0
         if ( message instanceof TextMessage ) {
 91  0
             TextMessage textMessage = (TextMessage) message;
 92  0
             writer.println( textMessage.getText() );
 93  
         }
 94  0
         writer.close();
 95  0
     }
 96  
 }
 97  
 
 98