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: Main.java 155459 2005-02-26 13:24:44Z dirkv $
9    */
10  package org.apache.commons.messagelet;
11  
12  import java.util.Iterator;
13  
14  import javax.jms.JMSException;
15  import javax.servlet.ServletContext;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  import org.apache.commons.messagelet.model.SubscriptionDigester;
20  import org.apache.commons.messagelet.model.SubscriptionList;
21  import org.apache.commons.messenger.Messenger;
22  import org.apache.commons.messenger.MessengerManager;
23  
24  /** 
25   * <p><code>Main</code> is a simple command line program that will
26   * create a number of subscriptions and consume messages using just regular 
27   * MDO and MessageListener classes.
28   *
29   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
30   * @version $Revision: 155459 $
31   */
32  public class Main {
33  
34      /** Logger */
35      private static final Log log = LogFactory.getLog(Main.class);
36  
37      /** The JMS connections */    
38      private MessengerManager manager;
39  
40      /** The JMS Subscriptions */
41      private SubscriptionList subscriptionList;
42      
43      /** The URI that connections are loaded from */
44      private String connectionsConfig = "Messenger.xml";
45      
46      /** The URI where subscriptions are loaded from */
47      private String subscriptionsConfig = "subscriptions.xml";
48  
49      /** Should we use a stopwatch to output performance metrics */
50      private boolean useStopWatch = false;
51  
52      
53      public static void main(String[] args) {
54          Main main = new Main();
55          if ( args.length <= 0 ) {
56              System.out.println( "Usage <subscriptionConfigFile> [<connectionsConfigFile>]" );
57              return;
58          }
59          if ( args.length > 0 ) {
60              main.setSubscriptionsConfig( args[0] );
61          }
62          if ( args.length > 1 ) {
63              main.setConnectionsConfig( args[1] );
64          }
65  
66          try {
67              main.run();
68          }        
69          catch (Exception e) {
70              log.error( "Caught: " + e, e );
71          }
72      }
73      
74      public Main() {
75      }
76  
77  
78      /**
79       * Starts all the JMS connections and consumes JMS messages, 
80       * passing them onto the MessageListener and Message Driven Objects
81       */
82      public void run() throws Exception {
83  
84          // force lazy construction
85          SubscriptionManager subscriber = new SubscriptionManager();
86          subscriber.setMessengerManager( getMessengerManager() );
87          subscriber.setSubscriptionList( createSubscriptionList() );
88          subscriber.setServletContext( getServletContext() );
89          
90          subscriber.subscribe();
91          
92          // now lets start all the connections...
93          for (Iterator iter = manager.getMessengerNames(); iter.hasNext(); ) {
94              String name = (String) iter.next();
95              Messenger messenger = manager.getMessenger( name );
96              try {
97                  messenger.getConnection().start();
98              }
99              catch (JMSException e) {
100                 log.error( "Caught exception trying to start messenger: " + name + ". Exception: " + e, e );
101             }
102         }
103         
104         //waitForever();
105     }
106 
107     
108     public Messenger getMessenger(String name) throws JMSException {
109         return getMessengerManager().getMessenger( name );
110     }
111     
112     
113     // Properties
114     //-------------------------------------------------------------------------    
115 
116     public String getConnectionsConfig() {
117         return connectionsConfig;
118     }
119     
120     public void setConnectionsConfig(String connectionsConfig) {
121         this.connectionsConfig = connectionsConfig;
122     }
123     
124     public String getSubscriptionsConfig() {
125         return subscriptionsConfig;
126     }
127     
128     public void setSubscriptionsConfig(String subscriptionsConfig) {
129         this.subscriptionsConfig = subscriptionsConfig;
130     }
131     
132     public MessengerManager getMessengerManager() throws JMSException {
133         if ( manager == null ) {
134             manager = createMessengerManager();
135             MessengerManager.setInstance( manager );
136         }
137         return manager;
138     }
139     
140     public void setMessengerManager(MessengerManager manager) {
141         this.manager = manager;
142     }
143     
144     // Implementation methods
145     //-------------------------------------------------------------------------    
146     protected MessengerManager createMessengerManager() throws JMSException {
147         String config = connectionsConfig;
148         
149         log.info( "Creating the JMS connections from the file: " + config );
150         
151         try {
152             return MessengerManager.load( config );
153         }
154         catch (JMSException e) {
155             log.error( "Could not parse Messenger connection XML deployment document for URL: " + config, e );
156             
157             throw new JMSException(
158                 "Could not parse Messenger connection XML deployment document for URL: " + config
159                 + " reason: " + e
160             );
161         }
162     }
163     
164     protected SubscriptionList createSubscriptionList() throws JMSException {
165         String config = subscriptionsConfig;
166         
167         log.info( "Loading the JMS subscriptions from: " + config );
168         
169         try {
170             SubscriptionDigester digester = new SubscriptionDigester();
171             return (SubscriptionList) digester.parse( config );
172         }
173         catch (Exception e) {
174             log.error( "Could not parse Messenger subscription XML deployment document for URL: " + config, e );
175             
176             throw new JMSException(
177                 "Could not parse Messenger subscription XML deployment document for URL: " + config
178                 + " reason: " + e
179             );
180         }
181     }
182     
183     protected ServletContext getServletContext() {
184         return null;
185     }
186     
187     /**
188      * This method blocks the current thread indefinitely until the JVM is terminated.
189      */
190     protected void waitForever() {
191         Object lock = new Object();
192         synchronized (lock) {
193             while (true) {
194                 try {
195                     lock.wait();
196                 }
197                 catch (Exception e) {
198                     log.warn( "Main thread interupted: " + e, e );
199                 }
200             }
201         }
202     }
203 }