mirror of https://github.com/apache/activemq.git
git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@390470 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8999aa10e0
commit
ea1dad712a
|
@ -323,6 +323,9 @@ public class BrokerService implements Service {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean isStarted() {
|
||||
return started.get();
|
||||
}
|
||||
|
||||
// Service interface
|
||||
// -------------------------------------------------------------------------
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -22,6 +22,7 @@ import java.util.List;
|
|||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.apache.activemq.broker.BrokerService;
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
|
@ -34,6 +35,50 @@ public class SpringTest extends TestCase {
|
|||
protected SpringConsumer consumer;
|
||||
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 @@ public class SpringTest extends TestCase {
|
|||
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.
|
||||
|
|
|
@ -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 -->
|
Loading…
Reference in New Issue