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: Producer.java 155459 2005-02-26 13:24:44Z dirkv $
9    */
10  package org.apache.commons.messenger.tool;
11  
12  import java.io.BufferedReader;
13  import java.io.FileReader;
14  import java.io.IOException;
15  import java.io.InputStreamReader;
16  import java.io.Reader;
17  
18  import javax.jms.Destination;
19  import javax.jms.JMSException;
20  import javax.jms.Message;
21  import javax.jms.TextMessage;
22  
23  import org.apache.commons.messenger.Messenger;
24  import org.apache.commons.messenger.MessengerManager;
25  
26  /** <p><code>Producer</code> is a sample program that 
27    * creates messages and sends them to a given destination which
28    * could either be a queue or a topc.</p>
29    *
30    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31    * @version $Revision: 155459 $
32    */
33  public class Producer {
34  
35      protected Messenger messenger;
36  
37      public static void main(String[] args) {
38          try {
39              Producer client = new Producer();
40              client.run( args );
41          }
42          catch (JMSException e) {
43              System.out.println( "Caught: " + e );
44              Exception linked = e.getLinkedException();
45              if ( linked != null ) {
46                  System.out.println( "Underlying exception: " + linked );
47                  linked.printStackTrace();
48              }
49              else {
50                  e.printStackTrace();
51              }
52          }
53          catch (Exception e) {
54              System.out.println( "Caught: " + e );
55              e.printStackTrace();
56          }
57      }
58      
59      public void run(String[] args) throws Exception {
60          if ( args.length < 2 ) {
61              System.out.println( "Usage: Producer messengerName destination [fileName]" );
62              System.out.println( "If no fileName is provided then the current input stream is used" );
63              return;
64          }
65          String name = args[0];
66          String subject = args[1];
67          messenger = MessengerManager.get( name );
68          if ( messenger == null ) {
69              throw new JMSException( "No such messenger called: " + name );
70          }
71          Destination destination = messenger.getDestination( subject );
72          if ( destination == null ) {
73              throw new JMSException( "Could not find destination: " + subject );
74          }
75  
76          Reader reader = ( args.length > 2 ) 
77              ? new FileReader( args[2] )
78              : new InputStreamReader( System.in );
79              
80          Message message = createMessage( reader );
81          
82          messenger.send( destination, message );
83          
84          // close the JMS connection to release any background threads        
85          messenger.close();
86      }
87      
88      /** Creates a message from the given text file */
89      protected Message createMessage(Reader in) throws Exception {
90          String text = readText(in);
91          TextMessage message = messenger.createTextMessage( text );
92          return message;
93      }
94      
95      /** Reads the given text stream into a single string */
96      protected String readText(Reader in) throws IOException {
97          // read all the text into memory
98          StringBuffer buffer = new StringBuffer();
99          BufferedReader reader = new BufferedReader( in );
100         for ( String line; (line = reader.readLine()) != null; ) {
101             buffer.append( line );
102             buffer.append( '\n' );
103         }
104         reader.close();
105         return buffer.toString();
106     }
107         
108 }
109 
110