activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
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 GMT
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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+
+<!-- START SNIPPET: spring -->
+<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
+
+<beans>
+
+    <bean id="broker1" class="org.apache.activemq.xbean.PooledBrokerFactoryBean">
+      <property name="config" value="classpath:activemq.xml"/>
+    </bean>
+
+    <bean id="broker2" class="org.apache.activemq.xbean.PooledBrokerFactoryBean">
+      <property name="config" value="classpath:activemq.xml"/>
+    </bean>
+    
+</beans>
+
+<!-- END SNIPPET: spring -->

Propchange: incubator/activemq/trunk/activemq-core/src/test/resources/spring-embedded-pooled.xml
------------------------------------------------------------------------------
    svn:executable = *



Mime
View raw message