Return-Path: Delivered-To: apmail-geronimo-activemq-commits-archive@www.apache.org Received: (qmail 79365 invoked from network); 31 Mar 2006 18:12:49 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 31 Mar 2006 18:12:49 -0000 Received: (qmail 72117 invoked by uid 500); 31 Mar 2006 18:12:49 -0000 Delivered-To: apmail-geronimo-activemq-commits-archive@geronimo.apache.org Received: (qmail 72088 invoked by uid 500); 31 Mar 2006 18:12:48 -0000 Mailing-List: contact activemq-commits-help@geronimo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: activemq-dev@geronimo.apache.org Delivered-To: mailing list activemq-commits@geronimo.apache.org Received: (qmail 72079 invoked by uid 99); 31 Mar 2006 18:12:48 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 31 Mar 2006 10:12:48 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Fri, 31 Mar 2006 10:12:47 -0800 Received: (qmail 79264 invoked by uid 65534); 31 Mar 2006 18:12:27 -0000 Message-ID: <20060331181227.79263.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r390470 - in /incubator/activemq/trunk/activemq-core/src: main/java/org/apache/activemq/broker/ main/java/org/apache/activemq/xbean/ test/java/org/apache/activemq/spring/ test/resources/ Date: Fri, 31 Mar 2006 18:12:26 -0000 To: activemq-commits@geronimo.apache.org From: chirino@apache.org X-Mailer: svnmailer-1.0.7 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: chirino Date: Fri Mar 31 10:12:24 2006 New Revision: 390470 URL: http://svn.apache.org/viewcvs?rev=390470&view=rev Log: Implemented: http://issues.apache.org/activemq/browse/AMQ-673 Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/xbean/PooledBrokerFactoryBean.java incubator/activemq/trunk/activemq-core/src/test/resources/spring-embedded-pooled.xml (with props) Modified: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/BrokerService.java incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/spring/SpringTest.java Modified: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/BrokerService.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/BrokerService.java?rev=390470&r1=390469&r2=390470&view=diff ============================================================================== --- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/BrokerService.java (original) +++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/BrokerService.java Fri Mar 31 10:12:24 2006 @@ -323,6 +323,9 @@ } } + public boolean isStarted() { + return started.get(); + } // Service interface // ------------------------------------------------------------------------- Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/xbean/PooledBrokerFactoryBean.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/xbean/PooledBrokerFactoryBean.java?rev=390470&view=auto ============================================================================== --- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/xbean/PooledBrokerFactoryBean.java (added) +++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/xbean/PooledBrokerFactoryBean.java Fri Mar 31 10:12:24 2006 @@ -0,0 +1,109 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed 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.xbean; + +import java.util.HashMap; + +import org.apache.activemq.broker.BrokerService; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.core.io.Resource; + +/** + * Used to share a single broker even if you have multiple broker bean definitions. + * + * A use case is where you have multiple web applications that want to start an embedded broker + * but only the first one to deploy should actually start it. + * + * @version $Revision$ + */ +public class PooledBrokerFactoryBean implements FactoryBean, InitializingBean, DisposableBean { + + static final HashMap sharedBrokerMap = new HashMap(); + + private boolean start; + private Resource config; + + static class SharedBroker { + BrokerFactoryBean factory; + int refCount; + } + + public void afterPropertiesSet() throws Exception { + synchronized( sharedBrokerMap ) { + SharedBroker sharedBroker = (SharedBroker) sharedBrokerMap.get(config); + if( sharedBroker == null ) { + sharedBroker = new SharedBroker(); + sharedBroker.factory = new BrokerFactoryBean(); + sharedBroker.factory.setConfig(config); + sharedBroker.factory.setStart(start); + sharedBroker.factory.afterPropertiesSet(); + sharedBrokerMap.put(config, sharedBroker); + } + sharedBroker.refCount++; + } + } + + public void destroy() throws Exception { + synchronized( sharedBrokerMap ) { + SharedBroker sharedBroker = (SharedBroker) sharedBrokerMap.get(config); + if( sharedBroker != null ) { + sharedBroker.refCount--; + if( sharedBroker.refCount==0 ) { + sharedBroker.factory.destroy(); + sharedBrokerMap.remove(config); + } + } + } + } + + public Resource getConfig() { + return config; + } + + public Object getObject() throws Exception { + synchronized( sharedBrokerMap ) { + SharedBroker sharedBroker = (SharedBroker) sharedBrokerMap.get(config); + if( sharedBroker != null ) { + return sharedBroker.factory.getObject(); + } + } + return null; + } + + public Class getObjectType() { + return BrokerService.class; + } + + public boolean isSingleton() { + return true; + } + + public boolean isStart() { + return start; + } + + public void setConfig(Resource config) { + this.config = config; + } + + public void setStart(boolean start) { + this.start=start; + } + +} Modified: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/spring/SpringTest.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/spring/SpringTest.java?rev=390470&r1=390469&r2=390470&view=diff ============================================================================== --- incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/spring/SpringTest.java (original) +++ incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/spring/SpringTest.java Fri Mar 31 10:12:24 2006 @@ -22,6 +22,7 @@ import junit.framework.TestCase; +import org.apache.activemq.broker.BrokerService; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -35,6 +36,50 @@ protected SpringProducer producer; /** + * Make sure that brokers are being pooled properly. + * + * @throws Exception + */ + public void testSenderWithSpringXmlEmbeddedPooledBrokerConfiguredViaXml() throws Exception { + String config = "spring-embedded-pooled.xml"; + + Thread.currentThread().setContextClassLoader(SpringTest.class.getClassLoader()); + ClassPathXmlApplicationContext context1 = new ClassPathXmlApplicationContext(config); + + BrokerService bs1 = (BrokerService) context1.getBean("broker1"); + assertNotNull(bs1); + BrokerService bs2 = (BrokerService) context1.getBean("broker2"); + assertNotNull(bs1); + + // It should be the same broker; + assertEquals(bs1, bs2); + + // Even if we load up another context, it should still be the same broker. + ClassPathXmlApplicationContext context2 = new ClassPathXmlApplicationContext(config); + + BrokerService bs3 = (BrokerService) context2.getBean("broker1"); + assertNotNull(bs3); + BrokerService bs4 = (BrokerService) context2.getBean("broker2"); + assertNotNull(bs4); + + // It should be the same broker; + assertEquals(bs1, bs3); + assertEquals(bs1, bs4); + + // And it should be started. + assertTrue(bs1.isStarted()); + + // should still be started asfter the 2nd context closes. + context2.close(); + assertTrue(bs1.isStarted()); + + // Should stop once all contexts close. + context1.close(); + assertFalse(bs1.isStarted()); + + } + + /** * Uses ActiveMQConnectionFactory to create the connection context. * Configuration file is /resources/spring.xml * @@ -79,7 +124,7 @@ String config = "spring-embedded.xml"; assertSenderConfig(config); } - + /** * assert method that is used by all the test method to send and receive messages * based on each spring configuration. Added: incubator/activemq/trunk/activemq-core/src/test/resources/spring-embedded-pooled.xml URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/test/resources/spring-embedded-pooled.xml?rev=390470&view=auto ============================================================================== --- incubator/activemq/trunk/activemq-core/src/test/resources/spring-embedded-pooled.xml (added) +++ incubator/activemq/trunk/activemq-core/src/test/resources/spring-embedded-pooled.xml Fri Mar 31 10:12:24 2006 @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + Propchange: incubator/activemq/trunk/activemq-core/src/test/resources/spring-embedded-pooled.xml ------------------------------------------------------------------------------ svn:executable = *