tidied up the XBean factory bean to avoid it owning and closing the contained context.

also by default the broker is started when using the XML file

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@357018 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Strachan 2005-12-15 14:15:18 +00:00
parent db0855c342
commit a37a074c81
15 changed files with 332 additions and 33 deletions

View File

@ -49,7 +49,8 @@ public class BrokerFactoryBean implements FactoryBean, InitializingBean, Disposa
private Resource config; private Resource config;
private XBeanBrokerService broker; private XBeanBrokerService broker;
private boolean start=false; private boolean start = false;
private ResourceXmlApplicationContext context;
public BrokerFactoryBean() { public BrokerFactoryBean() {
} }
@ -74,7 +75,7 @@ public class BrokerFactoryBean implements FactoryBean, InitializingBean, Disposa
if (config == null) { if (config == null) {
throw new IllegalArgumentException("config property must be set"); throw new IllegalArgumentException("config property must be set");
} }
ResourceXmlApplicationContext context = new ResourceXmlApplicationContext(config); context = new ResourceXmlApplicationContext(config);
try { try {
broker = (XBeanBrokerService) context.getBean("broker"); broker = (XBeanBrokerService) context.getBean("broker");
@ -96,12 +97,15 @@ public class BrokerFactoryBean implements FactoryBean, InitializingBean, Disposa
if (broker == null) { if (broker == null) {
throw new IllegalArgumentException("The configuration has no BrokerService instance for resource: " + config); throw new IllegalArgumentException("The configuration has no BrokerService instance for resource: " + config);
} }
broker.setAbstractApplicationContext(context); if (start) {
if( start )
broker.start(); broker.start();
}
} }
public void destroy() throws Exception { public void destroy() throws Exception {
if (context != null) {
context.close();
}
if (broker != null) { if (broker != null) {
broker.stop(); broker.stop();
} }
@ -126,6 +130,5 @@ public class BrokerFactoryBean implements FactoryBean, InitializingBean, Disposa
public void setStart(boolean start) { public void setStart(boolean start) {
this.start = start; this.start = start;
} }
} }

View File

@ -63,7 +63,8 @@ public class XBeanBrokerFactory implements BrokerFactoryHandler {
throw new IllegalArgumentException("The configuration has no BrokerService instance for resource: " + config); throw new IllegalArgumentException("The configuration has no BrokerService instance for resource: " + config);
} }
broker.setAbstractApplicationContext(context); // TODO warning resources from the context may not be closed down!
return broker; return broker;
} }

View File

@ -1,9 +1,8 @@
package org.activemq.xbean; package org.activemq.xbean;
import org.activemq.broker.BrokerService; import org.activemq.broker.BrokerService;
import org.springframework.beans.BeansException; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.support.AbstractApplicationContext;
/** /**
* Represents a running broker service which consists of a number of transport * Represents a running broker service which consists of a number of transport
@ -16,37 +15,33 @@ import org.springframework.context.support.AbstractApplicationContext;
* *
* @version $Revision: 1.1 $ * @version $Revision: 1.1 $
*/ */
public class XBeanBrokerService extends BrokerService implements InitializingBean { public class XBeanBrokerService extends BrokerService implements InitializingBean, DisposableBean {
private boolean start=false; private boolean start = true;
private AbstractApplicationContext applicationContext;
public XBeanBrokerService() { public XBeanBrokerService() {
} }
public void setAbstractApplicationContext(AbstractApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
if( start ) { if (start) {
start(); start();
} }
} }
public void stop() throws Exception { public void destroy() throws Exception {
super.stop(); stop();
if( applicationContext!=null ) {
applicationContext.destroy();
applicationContext=null;
}
} }
public boolean isStart() { public boolean isStart() {
return start; return start;
} }
/**
* Sets whether or not the broker is started along with the ApplicationContext it is defined within.
* Normally you would want the broker to start up along with the ApplicationContext but sometimes when working
* with JUnit tests you may wish to start and stop the broker explicitly yourself.
*/
public void setStart(boolean start) { public void setStart(boolean start) {
this.start = start; this.start = start;
} }
} }

View File

@ -50,7 +50,7 @@ public class EmbeddedBrokerTestSupport extends TestCase {
if (broker == null) { if (broker == null) {
broker = createBroker(); broker = createBroker();
} }
broker.start(); startBroker();
connectionFactory = createConnectionFactory(); connectionFactory = createConnectionFactory();
@ -133,6 +133,10 @@ public class EmbeddedBrokerTestSupport extends TestCase {
return answer; return answer;
} }
protected void startBroker() throws Exception {
broker.start();
}
/** /**
* @return whether or not persistence should be used * @return whether or not persistence should be used
*/ */

View File

@ -45,8 +45,8 @@ public class RetroactiveConsumerTestWithSimpleMessageListTest extends EmbeddedBr
public void testSendThenConsume() throws Exception { public void testSendThenConsume() throws Exception {
// lets some messages // lets some messages
connection = createConnection(); connection = createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination); MessageProducer producer = session.createProducer(destination);
for (int i = 0; i < messageCount; i++) { for (int i = 0; i < messageCount; i++) {
TextMessage message = session.createTextMessage("Message: " + i + " sent at: " + new Date()); TextMessage message = session.createTextMessage("Message: " + i + " sent at: " + new Date());
@ -65,7 +65,7 @@ public class RetroactiveConsumerTestWithSimpleMessageListTest extends EmbeddedBr
consumer.setMessageListener(listener); consumer.setMessageListener(listener);
listener.waitForMessagesToArrive(messageCount); listener.waitForMessagesToArrive(messageCount);
listener.assertMessagesReceived(messageCount); listener.assertMessagesReceived(messageCount);
} }
protected void setUp() throws Exception { protected void setUp() throws Exception {
@ -74,7 +74,6 @@ public class RetroactiveConsumerTestWithSimpleMessageListTest extends EmbeddedBr
super.setUp(); super.setUp();
} }
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
if (session != null) { if (session != null) {
session.close(); session.close();
@ -100,6 +99,10 @@ public class RetroactiveConsumerTestWithSimpleMessageListTest extends EmbeddedBr
return factory.getBroker(); return factory.getBroker();
} }
protected void startBroker() throws Exception {
// broker already started by XBean
}
protected String getBrokerXml() { protected String getBrokerXml() {
return "org/activemq/test/retroactive/activemq-fixed-buffer.xml"; return "org/activemq/test/retroactive/activemq-fixed-buffer.xml";
} }

View File

@ -0,0 +1,30 @@
/**
*
* Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
*
* 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.activemq.xbean;
/**
*
* @version $Revision$
*/
public class MultipleTestsWithEmbeddedBrokerAndPersistenceTest extends MultipleTestsWithEmbeddedBrokerTest {
protected boolean isPersistent() {
return true;
}
}

View File

@ -0,0 +1,55 @@
/**
*
* Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
*
* 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.activemq.xbean;
import org.activemq.EmbeddedBrokerTestSupport;
import javax.jms.Connection;
/**
*
* @author Neil Clayton
* @version $Revision$
*/
public class MultipleTestsWithEmbeddedBrokerTest extends EmbeddedBrokerTestSupport {
protected Connection connection;
public void test1() throws Exception {
}
public void test2() throws Exception {
}
protected void setUp() throws Exception {
System.out.println("### starting up the test case: " + getName());
super.setUp();
connection = connectionFactory.createConnection();
connection.start();
System.out.println("### started up the test case: " + getName());
}
protected void tearDown() throws Exception {
connection.close();
super.tearDown();
System.out.println("### closed down the test case: " + getName());
System.out.println();
}
}

View File

@ -0,0 +1,78 @@
/**
*
* Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
*
* 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.activemq.xbean;
import org.activemq.ActiveMQConnectionFactory;
import org.activemq.broker.BrokerService;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import junit.framework.TestCase;
/**
*
* @author Neil Clayton
* @version $Revision$
*/
public class MultipleTestsWithSpringFactoryBeanTest extends TestCase {
protected AbstractApplicationContext context;
protected BrokerService service;
private Connection connection;
public void test1() throws Exception {
}
public void test2() throws Exception {
}
protected void setUp() throws Exception {
System.out.println("### starting up the test case: " + getName());
super.setUp();
context = new ClassPathXmlApplicationContext("org/activemq/xbean/spring2.xml");
service = (BrokerService) context.getBean("broker");
// already started
service.start();
connection = createConnectionFactory().createConnection();
connection.start();
System.out.println("### started up the test case: " + getName());
}
protected void tearDown() throws Exception {
connection.close();
// stopped as part of the context
service.stop();
context.close();
super.tearDown();
System.out.println("### closed down the test case: " + getName());
System.out.println();
}
protected ConnectionFactory createConnectionFactory() {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
factory.setBrokerURL("vm://localhost");
return factory;
}
}

View File

@ -0,0 +1,45 @@
/**
*
* Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
*
* 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.activemq.xbean;
import org.activemq.broker.BrokerService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @version $Revision$
*/
public class MultipleTestsWithSpringXBeanFactoryBeanTest extends MultipleTestsWithEmbeddedBrokerTest {
private ClassPathXmlApplicationContext context;
protected BrokerService createBroker() throws Exception {
context = new ClassPathXmlApplicationContext("org/activemq/xbean/spring2.xml");
return (BrokerService) context.getBean("broker");
}
protected void tearDown() throws Exception {
super.tearDown();
if (context != null) {
context.destroy();
}
}
}

View File

@ -0,0 +1,37 @@
/**
*
* Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
*
* 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.activemq.xbean;
import org.activemq.broker.BrokerService;
import org.springframework.core.io.ClassPathResource;
/**
* @version $Revision$
*/
public class MultipleTestsWithXBeanFactoryBeanTest extends MultipleTestsWithEmbeddedBrokerTest {
protected BrokerService createBroker() throws Exception {
BrokerFactoryBean factory = new BrokerFactoryBean();
factory.setConfig(new ClassPathResource("org/activemq/xbean/activemq2.xml"));
factory.afterPropertiesSet();
return factory.getBroker();
}
}

View File

@ -79,7 +79,8 @@ public class XBeanConfigTest extends TestCase {
brokerService = createBroker(); brokerService = createBroker();
broker = brokerService.getBroker(); broker = brokerService.getBroker();
brokerService.start(); // started automatically
//brokerService.start();
context = new ConnectionContext(); context = new ConnectionContext();
context.setBroker(broker); context.setBroker(broker);

View File

@ -15,7 +15,10 @@
</transportConnectors> </transportConnectors>
<networkConnectors> <networkConnectors>
<!--
<networkConnector uri="multicast://default?initialReconnectDelay=100" />
<networkConnector uri="static://(tcp://localhost:61616)" /> <networkConnector uri="static://(tcp://localhost:61616)" />
-->
</networkConnectors> </networkConnectors>
</broker> </broker>

View File

@ -0,0 +1,37 @@
<beans xmlns="http://activemq.org/config/1.0">
<!-- for easier integration with the JUnit tests, lets not start the broker when the ApplicationContext starts -->
<broker useJmx="true" start="false">
<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry topic="FOO.>">
<dispatchPolicy>
<strictOrderDispatchPolicy />
</dispatchPolicy>
<subscriptionRecoveryPolicy>
<lastImageSubscriptionRecoveryPolicy />
</subscriptionRecoveryPolicy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
<persistenceAdapter>
<journaledJDBC journalLogFiles="5" dataDirectory="../data" />
</persistenceAdapter>
<transportConnectors>
<transportConnector uri="tcp://localhost:61616" />
</transportConnectors>
<!--
<networkConnectors>
<networkConnector
uri="multicast://default?initialReconnectDelay=100" />
</networkConnectors>
-->
</broker>
</beans>

View File

@ -8,7 +8,6 @@
<bean id="broker" class="org.activemq.xbean.BrokerFactoryBean"> <bean id="broker" class="org.activemq.xbean.BrokerFactoryBean">
<property name="config" value="classpath:org/activemq/xbean/activemq.xml" /> <property name="config" value="classpath:org/activemq/xbean/activemq.xml" />
<property name="start" value="true" />
</bean> </bean>
<!-- JMS ConnectionFactory to use --> <!-- JMS ConnectionFactory to use -->

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect">
<bean id="broker" class="org.activemq.xbean.BrokerFactoryBean">
<property name="config" value="classpath:org/activemq/xbean/activemq2.xml" />
</bean>
</beans>