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: Main.java 155459 2005-02-26 13:24:44Z dirkv $
009 */
010 package org.apache.commons.messagelet;
011
012 import java.util.Iterator;
013
014 import javax.jms.JMSException;
015 import javax.servlet.ServletContext;
016
017 import org.apache.commons.logging.Log;
018 import org.apache.commons.logging.LogFactory;
019 import org.apache.commons.messagelet.model.SubscriptionDigester;
020 import org.apache.commons.messagelet.model.SubscriptionList;
021 import org.apache.commons.messenger.Messenger;
022 import org.apache.commons.messenger.MessengerManager;
023
024 /**
025 * <p><code>Main</code> is a simple command line program that will
026 * create a number of subscriptions and consume messages using just regular
027 * MDO and MessageListener classes.
028 *
029 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
030 * @version $Revision: 155459 $
031 */
032 public class Main {
033
034 /** Logger */
035 private static final Log log = LogFactory.getLog(Main.class);
036
037 /** The JMS connections */
038 private MessengerManager manager;
039
040 /** The JMS Subscriptions */
041 private SubscriptionList subscriptionList;
042
043 /** The URI that connections are loaded from */
044 private String connectionsConfig = "Messenger.xml";
045
046 /** The URI where subscriptions are loaded from */
047 private String subscriptionsConfig = "subscriptions.xml";
048
049 /** Should we use a stopwatch to output performance metrics */
050 private boolean useStopWatch = false;
051
052
053 public static void main(String[] args) {
054 Main main = new Main();
055 if ( args.length <= 0 ) {
056 System.out.println( "Usage <subscriptionConfigFile> [<connectionsConfigFile>]" );
057 return;
058 }
059 if ( args.length > 0 ) {
060 main.setSubscriptionsConfig( args[0] );
061 }
062 if ( args.length > 1 ) {
063 main.setConnectionsConfig( args[1] );
064 }
065
066 try {
067 main.run();
068 }
069 catch (Exception e) {
070 log.error( "Caught: " + e, e );
071 }
072 }
073
074 public Main() {
075 }
076
077
078 /**
079 * Starts all the JMS connections and consumes JMS messages,
080 * passing them onto the MessageListener and Message Driven Objects
081 */
082 public void run() throws Exception {
083
084 // force lazy construction
085 SubscriptionManager subscriber = new SubscriptionManager();
086 subscriber.setMessengerManager( getMessengerManager() );
087 subscriber.setSubscriptionList( createSubscriptionList() );
088 subscriber.setServletContext( getServletContext() );
089
090 subscriber.subscribe();
091
092 // now lets start all the connections...
093 for (Iterator iter = manager.getMessengerNames(); iter.hasNext(); ) {
094 String name = (String) iter.next();
095 Messenger messenger = manager.getMessenger( name );
096 try {
097 messenger.getConnection().start();
098 }
099 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 }