KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > web > SpringBrokerContextListener


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
26 import javax.servlet.ServletContextEvent JavaDoc;
27 import javax.servlet.ServletContextListener JavaDoc;
28
29 /**
30  * Used to configure and instance of ActiveMQ <tt>BrokerService</tt> using
31  * ActiveMQ/Spring's xml configuration. <p/> The configuration file is specified
32  * via the context init parameter <tt>brokerURI</tt>, typically: <code>
33  * &lt;context-param&gt;
34  * &lt;param-name&gt;brokerURI&lt;/param-name&gt;
35  * &lt;param-value&gt;/WEB-INF/activemq.xml&lt;/param-value&gt;
36  * &lt;/context-param&gt;
37  * </code>
38  *
39  * As a a default, if a <tt>brokerURI</tt> is not specified it will look up
40  * for <tt>activemq.xml</tt>
41  *
42  * @version $Revision: 1.1 $
43  */

44 public class SpringBrokerContextListener implements ServletContextListener JavaDoc {
45
46     /** broker uri context parameter name: <tt>brokerURI</tt> */
47     public static final String JavaDoc INIT_PARAM_BROKER_URI = "brokerURI";
48
49     /** the broker container instance */
50     private BrokerService brokerContainer;
51
52     /**
53      * Set the broker container to be used by this listener
54      *
55      * @param container
56      * the container to be used.
57      */

58     protected void setBrokerService(BrokerService container) {
59         this.brokerContainer = container;
60     }
61
62     /**
63      * Return the broker container.
64      */

65     protected BrokerService getBrokerService() {
66         return this.brokerContainer;
67     }
68
69     public void contextInitialized(ServletContextEvent JavaDoc event) {
70         ServletContext JavaDoc 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 JavaDoc e) {
81             context.log("Failed to start ActiveMQ broker: " + e, e);
82         }
83     }
84
85     public void contextDestroyed(ServletContextEvent JavaDoc event) {
86         ServletContext JavaDoc context = event.getServletContext();
87         if (brokerContainer != null) {
88             try {
89                 brokerContainer.stop();
90             }
91             catch (Exception JavaDoc e) {
92                 context.log("Failed to stop the ActiveMQ Broker: " + e, e);
93             }
94             brokerContainer = null;
95         }
96     }
97
98     /**
99      * Factory method to create a new ActiveMQ Broker
100      */

101     protected BrokerService createBroker(ServletContext JavaDoc context) {
102         String JavaDoc 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 JavaDoc e) {
113             context.log("Failed to create broker: " + e, e);
114         }
115         return factory.getBroker();
116     }
117 }
118
Popular Tags