Hello Active MQ users , We use Tomcat server 5.5.20 with two web applications , namely Webapp A and Web App. B. The inter web app communication is via the active mq broker. The active mq version is 4..0.2 . As of now, the web app A is always started before B. Hence the broker is specified as a Servlet Context Listener in web.xml of the web app A. The problem we have is to start the broker irrespective of the order of initialization of web apps . We would be adding a new web app called Webapp C . In this scenario, I will not have a control on the order in which the web apps are initialized by Tomcat. The code to start the broker , takes into account, that if the broker port is already occupied, it will attempt to start the broker at a randomly generated port for 5 times. Tomcat does not give us the flexibility of starting the web apps in particular order. The web.xml of Webapp A is as follows <!-- starting active mq broker -->
<listener> <listener-class>com.messaging.ActiveMQBrokerStartListener</listener-clas s> </listener> <!-- starting the rest of services in web app A --> <listener> <listener-class> com.context.ApplicationInitListener </listener-class> </listener> // code for ActiveMQBrokerStartListener BrokerService broker = new BrokerService(); int counter = 0; Properties activeMQProperties = new Properties() ; int portNum ; public void contextInitialized(ServletContextEvent arg0) { System.out.println("Starting ActiveMQ Broker Service..."); //for the broker URI , do not have leading or trailing spaces File dir = new File("../../activemq"); Logger logger = Logger.getLogger(ActiveMQBrokerStartListener.class); dir.mkdir(); InetAddress ipAdress ; broker.setDataDirectory(dir); try{ ipAdress = InetAddress.getLocalHost(); }catch(UnknownHostException e){ logger.error("error in obtaining Local Host address "+e.getMessage()); return ; } // getting the port number from the active mq properties file try{ activeMQProperties.load(new FileInputStream(MessagingConstants.ACTIVEMQ_PORT_FILE_PATH )); portNum = Integer.parseInt(activeMQProperties.getProperty(MessagingConstants.Activ eMQPort,"-1")); if(portNum == -1){ logger.error("active mq port not configured via the proeprties file using the default port value as "+MessagingConstants.DEFAULT_PORT_VALUE); portNum = MessagingConstants.DEFAULT_PORT_VALUE ; } }catch(Exception e){ logger.error("exception while getting port properties "+e.getMessage()); } while(counter < 5){ // 5 attempts to start the broker try{ // broker url is tcp://171.69.155.137:61716?trace=true&wireFormat.maxInactivityDuration=- 1 broker.addConnector(MessagingConstants.CONNECTOR+ ipAdress.getHostAddress()+":"+portNum+MessagingConstants.ACTIVEMQ_URL_SU FFIX); broker.start(); logger.info("Active MQ Broker Started at local host port "+ portNum); break; }catch(Exception e){ // System.out.println(e.getMessage()); if(e instanceof java.net.BindException){ //broker port is already occupied by another process, try to start the broker at different // port number portNum = getRandomPortNumber() ; logger.info("trying to start the broker at port "+portNum); counter ++; }else{ logger.error("exception while starting broker : ",e); e.printStackTrace(); } } } Please let me know, if there is a way to ensure that the broker is started first , irrespective of the order of initilization of web apps. I'll also do a google search on this issue. thanks, Suchitha.