1 18 package org.apache.activemq.web; 19 20 import org.apache.activemq.broker.BrokerService; 21 import org.apache.activemq.xbean.BrokerFactoryBean; 22 import org.springframework.core.io.Resource; 23 import org.springframework.web.context.support.ServletContextResource; 24 25 import javax.servlet.ServletContext ; 26 import javax.servlet.ServletContextEvent ; 27 import javax.servlet.ServletContextListener ; 28 29 44 public class SpringBrokerContextListener implements ServletContextListener { 45 46 47 public static final String INIT_PARAM_BROKER_URI = "brokerURI"; 48 49 50 private BrokerService brokerContainer; 51 52 58 protected void setBrokerService(BrokerService container) { 59 this.brokerContainer = container; 60 } 61 62 65 protected BrokerService getBrokerService() { 66 return this.brokerContainer; 67 } 68 69 public void contextInitialized(ServletContextEvent event) { 70 ServletContext context = event.getServletContext(); 71 context.log("Creating ActiveMQ Broker..."); 72 brokerContainer = createBroker(context); 73 74 context.log("Starting ActiveMQ Broker"); 75 try { 76 brokerContainer.start(); 77 78 context.log("Started ActiveMQ Broker"); 79 } 80 catch (Exception e) { 81 context.log("Failed to start ActiveMQ broker: " + e, e); 82 } 83 } 84 85 public void contextDestroyed(ServletContextEvent event) { 86 ServletContext context = event.getServletContext(); 87 if (brokerContainer != null) { 88 try { 89 brokerContainer.stop(); 90 } 91 catch (Exception e) { 92 context.log("Failed to stop the ActiveMQ Broker: " + e, e); 93 } 94 brokerContainer = null; 95 } 96 } 97 98 101 protected BrokerService createBroker(ServletContext context) { 102 String brokerURI = context.getInitParameter(INIT_PARAM_BROKER_URI); 103 if (brokerURI == null) { 104 brokerURI = "activemq.xml"; 105 } 106 context.log("Loading ActiveMQ Broker configuration from: " + brokerURI); 107 Resource resource = new ServletContextResource(context, brokerURI); 108 BrokerFactoryBean factory = new BrokerFactoryBean(resource); 109 try { 110 factory.afterPropertiesSet(); 111 } 112 catch (Exception e) { 113 context.log("Failed to create broker: " + e, e); 114 } 115 return factory.getBroker(); 116 } 117 } 118 | Popular Tags |