From 370d56e300baf17606af93a27f4a2fbb4c6c3335 Mon Sep 17 00:00:00 2001 From: Bosanac Dejan Date: Tue, 15 Jun 2010 14:06:43 +0000 Subject: [PATCH] https://issues.apache.org/activemq/browse/AMQ-2770 - durable subscription mbean git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@954893 13f79535-47bb-0310-9956-ffa450edef68 --- .../broker/jmx/DurableSubscriptionView.java | 7 ++ .../ManagedDurableSubscriptionTest.java | 119 ++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 activemq-core/src/test/java/org/apache/activemq/usecases/ManagedDurableSubscriptionTest.java diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/jmx/DurableSubscriptionView.java b/activemq-core/src/main/java/org/apache/activemq/broker/jmx/DurableSubscriptionView.java index 12a311d4a5..eb422f0f58 100755 --- a/activemq-core/src/main/java/org/apache/activemq/broker/jmx/DurableSubscriptionView.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/jmx/DurableSubscriptionView.java @@ -142,4 +142,11 @@ public class DurableSubscriptionView extends SubscriptionView implements Durable } return false; } + + @Override + public boolean isActive() { + return durableSub.isActive(); + } + + } diff --git a/activemq-core/src/test/java/org/apache/activemq/usecases/ManagedDurableSubscriptionTest.java b/activemq-core/src/test/java/org/apache/activemq/usecases/ManagedDurableSubscriptionTest.java new file mode 100644 index 0000000000..c658c22f98 --- /dev/null +++ b/activemq-core/src/test/java/org/apache/activemq/usecases/ManagedDurableSubscriptionTest.java @@ -0,0 +1,119 @@ +/** + * 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.usecases; + +import java.io.File; +import java.lang.management.ManagementFactory; + +import javax.jms.Connection; +import javax.jms.Session; +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.broker.BrokerFactory; +import org.apache.activemq.broker.BrokerService; +import org.apache.activemq.command.ActiveMQTopic; +import org.apache.activemq.store.kahadaptor.KahaPersistenceAdapter; + +public class ManagedDurableSubscriptionTest extends org.apache.activemq.TestSupport { + + BrokerService broker = null; + Connection connection = null; + ActiveMQTopic topic; + + public void testJMXSubscriptions() throws Exception { + // create durable subscription + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + session.createDurableSubscriber(topic, "SubsId"); + session.close(); + + // restart the broker + stopBroker(); + startBroker(); + + MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + ObjectName subscriptionObjectName = broker.getAdminView().getDurableTopicSubscribers()[0]; + + Object active = mbs.getAttribute(subscriptionObjectName, "Active"); + assertTrue("Subscription is active.", Boolean.FALSE.equals(active)); + + // activate + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + session.createDurableSubscriber(topic, "SubsId"); + + active = mbs.getAttribute(subscriptionObjectName, "Active"); + assertTrue("Subscription is INactive.", Boolean.TRUE.equals(active)); + + // deactivate + connection.close(); + connection = null; + + active = mbs.getAttribute(subscriptionObjectName, "Active"); + assertTrue("Subscription is active.", Boolean.FALSE.equals(active)); + + } + + private void startBroker() throws Exception { + broker = BrokerFactory.createBroker("broker:(vm://localhost)"); + broker.setKeepDurableSubsActive(false); + broker.setPersistent(true); + KahaPersistenceAdapter persistenceAdapter = new KahaPersistenceAdapter(); + persistenceAdapter.setDirectory(new File("activemq-data/" + getName())); + broker.setPersistenceAdapter(persistenceAdapter); + broker.setUseJmx(true); + broker.setBrokerName(getName()); + broker.start(); + + connection = createConnection(); + } + + private void stopBroker() throws Exception { + if (connection != null) + connection.close(); + connection = null; + if (broker != null) + broker.stop(); + broker = null; + } + + protected ActiveMQConnectionFactory createConnectionFactory() throws Exception { + return new ActiveMQConnectionFactory("vm://" + getName() + "?waitForStart=5000&create=false"); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + + topic = (ActiveMQTopic) createDestination(); + startBroker(); + } + + @Override + protected void tearDown() throws Exception { + stopBroker(); + super.tearDown(); + } + + @Override + protected Connection createConnection() throws Exception { + Connection rc = super.createConnection(); + rc.setClientID(getName()); + rc.start(); + return rc; + } +}