Re: [activemq-dev] [PATCH] module web: using a context listener rather than servlet

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [activemq-dev] [PATCH] module web: using a context listener rather than servlet

jstrachan
Patch applied, many thanks Stephane!

I've made the web.xml in the release distro use the <listener>  
approach instead of the servlet.


On 8 Jun 2005, at 10:44, Stephane Bailliez wrote:

> Using a context listener is more appropriate than a servlet to  
> initialize components. I had added some minor javadocs so that  
> people know from the javadocs what parameter to use for configuration.
> package org.activemq.web;
>
> import javax.servlet.ServletContext;
> import javax.servlet.ServletContextListener;
> import javax.servlet.ServletContextEvent;
> import javax.jms.JMSException;
>
> import org.activemq.spring.SpringBrokerContainerFactory;
> import org.activemq.broker.BrokerContainer;
> import org.springframework.core.io.Resource;
> import org.springframework.web.context.support.ServletContextResource;
>
> /**
>  * Used to configure and instance of ActiveMQ <tt>BrokerContainer</
> tt> using
>  * ActiveMQ/Spring's xml configuration.
>  * <p/>
>  * The configuration file is specified via the context init  
> parameter <tt>brokerURI</tt>,
>  * typically:
>  * <code>
>  * &lt;context-param&gt;
>  * &lt;param-name&gt;brokerURI&lt;/param-name&gt;
>  * &lt;param-value&gt;/WEB-INF/activemq.xml&lt;/param-value&gt;
>  * &lt;/context-param&gt;
>  * </code>
>  *
>  * As a a default, if a <tt>brokerURI</tt> is not specified it will  
> look up for <tt>activemq.xml</tt>
>  */
> public class SpringBrokerContextListener implements  
> ServletContextListener {
>
>     /** broker uri context parameter name: <tt>brokerURI</tt> */
>     public static final String INIT_PARAM_BROKER_URI = "brokerURI";
>
>     /** the broker container instance */
>     private BrokerContainer brokerContainer;
>
>     /**
>      * Set the broker container to be used by this listener
>      * @param container the container to be used.
>      */
>     protected void setBrokerContainer(BrokerContainer container){
>         this.brokerContainer = container;
>     }
>
>     /**
>      * Return the broker container.
>      */
>     protected BrokerContainer getBrokerContainer(){
>         return this.brokerContainer;
>     }
>
>     public void contextInitialized(ServletContextEvent event) {
>         ServletContext context = event.getServletContext();
>         context.log("Creating ActiveMQ Broker...");
>         brokerContainer = createBroker(context);
>
>         context.log("Starting ActiveMQ Broker");
>         try {
>             brokerContainer.start();
>
>             context.log("Started ActiveMQ Broker");
>         } catch (JMSException e) {
>             context.log("Failed to start ActiveMQ broker: " + e, e);
>         }
>     }
>
>     public void contextDestroyed(ServletContextEvent event) {
>         ServletContext context = event.getServletContext();
>         if (brokerContainer != null) {
>             try {
>                 brokerContainer.stop();
>             } catch (JMSException e) {
>                context.log("Failed to stop the ActiveMQ Broker: " +  
> e, e);
>             }
>             brokerContainer = null;
>         }
>     }
>
>     /**
>      * Factory method to create a new ActiveMQ Broker
>      */
>     protected BrokerContainer createBroker(ServletContext context) {
>         String brokerURI = context.getInitParameter
> (INIT_PARAM_BROKER_URI);
>         if (brokerURI == null) {
>             brokerURI = "activemq.xml";
>         }
>         context.log("Loading ActiveMQ Broker configuration from: "  
> + brokerURI);
>         Resource resource = new ServletContextResource(context,  
> brokerURI);
>         return SpringBrokerContainerFactory.newInstance(resource);
>     }
> }
>


James
-------
http://radio.weblogs.com/0112098/

Loading...