alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

ActiveMQ example source code file (SpringBrokerContextListener.java)

This example ActiveMQ source code file (SpringBrokerContextListener.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - ActiveMQ tags/keywords

activemq, activemq, broker, broker, brokerfactorybean, brokerservice, brokerservice, exception, failed, failed, servlet, servletcontext, servletcontextresource, starting, string

The ActiveMQ SpringBrokerContextListener.java source code

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

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.xbean.BrokerFactoryBean;
import org.springframework.core.io.Resource;
import org.springframework.web.context.support.ServletContextResource;

/**
 * Used to configure and instance of ActiveMQ <tt>BrokerService using
 * ActiveMQ/Spring's xml configuration. <p/> The configuration file is specified
 * via the context init parameter <tt>brokerURI, typically: 
 * <context-param>
 * <param-name>brokerURI</param-name>
 * <param-value>/WEB-INF/activemq.xml</param-value>
 * </context-param>
 * </code>
 * As a a default, if a <tt>brokerURI is not specified it will look up
 * for <tt>activemq.xml
 * 
 * 
 */
public class SpringBrokerContextListener implements ServletContextListener {

    /** broker uri context parameter name: <tt>brokerURI */
    public static final String INIT_PARAM_BROKER_URI = "brokerURI";

    /** the broker container instance */
    private BrokerService brokerContainer;

    /**
     * Set the broker container to be used by this listener
     * 
     * @param container the container to be used.
     */
    protected void setBrokerService(BrokerService container) {
        this.brokerContainer = container;
    }

    /**
     * Return the broker container.
     */
    protected BrokerService getBrokerService() {
        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 (Exception 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 (Exception e) {
                context.log("Failed to stop the ActiveMQ Broker: " + e, e);
            }
            brokerContainer = null;
        }
    }

    /**
     * Factory method to create a new ActiveMQ Broker
     */
    protected BrokerService 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);
        BrokerFactoryBean factory = new BrokerFactoryBean(resource);
        try {
            factory.afterPropertiesSet();
        } catch (Exception e) {
            context.log("Failed to create broker: " + e, e);
        }
        return factory.getBroker();
    }
}

Other ActiveMQ examples (source code examples)

Here is a short list of links related to this ActiveMQ SpringBrokerContextListener.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.