You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by de...@apache.org on 2010/06/14 12:34:54 UTC

svn commit: r954406 - in /activemq/trunk/activemq-core/src: main/java/org/apache/activemq/broker/region/cursors/ main/java/org/apache/activemq/store/kahadb/ test/java/org/apache/activemq/usecases/

Author: dejanb
Date: Mon Jun 14 10:34:54 2010
New Revision: 954406

URL: http://svn.apache.org/viewvc?rev=954406&view=rev
Log:
https://issues.apache.org/activemq/browse/AMQ-2695 - durable subs and selectors - fix for kahadb

Added:
    activemq/trunk/activemq-core/src/test/java/org/apache/activemq/usecases/DurableSubscriptionSelectorTest.java
    activemq/trunk/activemq-core/src/test/java/org/apache/activemq/usecases/KahaDBDurableSubscriptionSelectorTest.java
Removed:
    activemq/trunk/activemq-core/src/test/java/org/apache/activemq/usecases/SubscriptionSelectorTest.java
Modified:
    activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/region/cursors/TopicStorePrefetch.java
    activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/kahadb/KahaDBStore.java

Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/region/cursors/TopicStorePrefetch.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/region/cursors/TopicStorePrefetch.java?rev=954406&r1=954405&r2=954406&view=diff
==============================================================================
--- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/region/cursors/TopicStorePrefetch.java (original)
+++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/region/cursors/TopicStorePrefetch.java Mon Jun 14 10:34:54 2010
@@ -16,7 +16,6 @@
  */
 package org.apache.activemq.broker.region.cursors;
 
-import java.io.IOException;
 import org.apache.activemq.broker.region.Subscription;
 import org.apache.activemq.broker.region.Topic;
 import org.apache.activemq.command.Message;
@@ -75,8 +74,7 @@ class TopicStorePrefetch extends Abstrac
     @Override
     protected synchronized int getStoreSize() {
         try {
-            this.store.recoverNextMessages(clientId, subscriberName, maxBatchSize, this);
-            return size;
+            return store.getMessageCount(clientId, subscriberName);
         } catch (Exception e) {
             LOG.error(this + " Failed to get the outstanding message count from the store", e);
             throw new RuntimeException(e);

Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/kahadb/KahaDBStore.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/kahadb/KahaDBStore.java?rev=954406&r1=954405&r2=954406&view=diff
==============================================================================
--- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/kahadb/KahaDBStore.java (original)
+++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/kahadb/KahaDBStore.java Mon Jun 14 10:34:54 2010
@@ -38,6 +38,8 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import javax.jms.InvalidSelectorException;
+
 import org.apache.activemq.broker.ConnectionContext;
 import org.apache.activemq.command.ActiveMQDestination;
 import org.apache.activemq.command.ActiveMQQueue;
@@ -51,8 +53,11 @@ import org.apache.activemq.command.Messa
 import org.apache.activemq.command.SubscriptionInfo;
 import org.apache.activemq.command.TransactionId;
 import org.apache.activemq.command.XATransactionId;
+import org.apache.activemq.filter.BooleanExpression;
+import org.apache.activemq.filter.MessageEvaluationContext;
 import org.apache.activemq.openwire.OpenWireFormat;
 import org.apache.activemq.protobuf.Buffer;
+import org.apache.activemq.selector.SelectorParser;
 import org.apache.activemq.store.AbstractMessageStore;
 import org.apache.activemq.store.MessageRecoveryListener;
 import org.apache.activemq.store.MessageStore;
@@ -75,6 +80,7 @@ import org.apache.activemq.store.kahadb.
 import org.apache.activemq.store.kahadb.data.KahaDestination.DestinationType;
 import org.apache.activemq.usage.MemoryUsage;
 import org.apache.activemq.usage.SystemUsage;
+import org.apache.activemq.util.IOExceptionSupport;
 import org.apache.activemq.util.ServiceStopper;
 import org.apache.activemq.wireformat.WireFormat;
 import org.apache.commons.logging.Log;
@@ -612,6 +618,7 @@ public class KahaDBStore extends Message
 
         public int getMessageCount(String clientId, String subscriptionName) throws IOException {
             final String subscriptionKey = subscriptionKey(clientId, subscriptionName);
+            final SubscriptionInfo info = lookupSubscription(clientId, subscriptionName);
             synchronized (indexMutex) {
                 return pageFile.tx().execute(new Transaction.CallableClosure<Integer, IOException>() {
                     public Integer execute(Transaction tx) throws IOException {
@@ -626,8 +633,24 @@ public class KahaDBStore extends Message
                         int counter = 0;
                         for (Iterator<Entry<Long, MessageKeys>> iterator = sd.orderIndex.iterator(tx, cursorPos); iterator
                                 .hasNext();) {
-                            iterator.next();
-                            counter++;
+                            Entry<Long, MessageKeys> entry = iterator.next();
+                            String selector = info.getSelector();
+                            if (selector != null) {
+                                try {
+                                    if (selector != null) { 
+                                        BooleanExpression selectorExpression = SelectorParser.parse(selector);
+                                        MessageEvaluationContext ctx = new MessageEvaluationContext();
+                                        ctx.setMessageReference(loadMessage(entry.getValue().location));
+                                        if (selectorExpression.matches(ctx)) {
+                                            counter++;
+                                        }
+                                    }
+                                } catch (Exception e) {
+                                    throw IOExceptionSupport.create(e);
+                                }
+                            } else {
+                                counter++;
+                            }
                         }
                         return counter;
                     }

Added: activemq/trunk/activemq-core/src/test/java/org/apache/activemq/usecases/DurableSubscriptionSelectorTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/usecases/DurableSubscriptionSelectorTest.java?rev=954406&view=auto
==============================================================================
--- activemq/trunk/activemq-core/src/test/java/org/apache/activemq/usecases/DurableSubscriptionSelectorTest.java (added)
+++ activemq/trunk/activemq-core/src/test/java/org/apache/activemq/usecases/DurableSubscriptionSelectorTest.java Mon Jun 14 10:34:54 2010
@@ -0,0 +1,162 @@
+/**
+ * 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.lang.management.ManagementFactory;
+
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageListener;
+import javax.jms.MessageProducer;
+import javax.jms.Session;
+import javax.jms.TopicSubscriber;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.activemq.ActiveMQConnection;
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.activemq.broker.BrokerService;
+import org.apache.activemq.command.ActiveMQTopic;
+import org.apache.activemq.store.PersistenceAdapter;
+
+abstract public class DurableSubscriptionSelectorTest extends org.apache.activemq.TestSupport {
+
+    MBeanServer mbs;
+    BrokerService broker = null;
+    ActiveMQTopic topic;
+
+    ActiveMQConnection consumerConnection = null, producerConnection = null;
+    Session producerSession;
+    MessageProducer producer;
+
+    private int received = 0;
+
+    public void testSubscription() throws Exception {
+        openConsumer();
+        for (int i = 0; i < 4000; i++) {
+            sendMessage(false);
+        }
+        Thread.sleep(1000);
+
+        assertEquals("Invalid message received.", 0, received);
+
+        closeProducer();
+        closeConsumer();
+        stopBroker();
+
+        startBroker(false);
+        openConsumer();
+
+        sendMessage(true);
+        Thread.sleep(1000);
+
+        assertEquals("Message is not recieved.", 1, received);
+
+        sendMessage(true);
+        Thread.sleep(100);
+
+        assertEquals("Message is not recieved.", 2, received);
+    }
+
+    private void openConsumer() throws Exception {
+        consumerConnection = (ActiveMQConnection) createConnection();
+        consumerConnection.setClientID("cliID");
+        consumerConnection.start();
+        Session session = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+        TopicSubscriber subscriber = session.createDurableSubscriber(topic, "subName", "filter=true", false);
+
+        subscriber.setMessageListener(new MessageListener() {
+            public void onMessage(Message message) {
+                received++;
+            }
+        });
+    }
+
+    private void closeConsumer() throws JMSException {
+        if (consumerConnection != null)
+            consumerConnection.close();
+        consumerConnection = null;
+    }
+
+    private void sendMessage(boolean filter) throws Exception {
+        if (producerConnection == null) {
+            producerConnection = (ActiveMQConnection) createConnection();
+            producerConnection.start();
+            producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+            producer = producerSession.createProducer(topic);
+        }
+
+        Message message = producerSession.createMessage();
+        message.setBooleanProperty("filter", filter);
+        producer.send(message);
+    }
+
+    private void closeProducer() throws JMSException {
+        if (producerConnection != null)
+            producerConnection.close();
+        producerConnection = null;
+    }
+
+    private int getPendingQueueSize() throws Exception {
+        ObjectName[] subs = broker.getAdminView().getDurableTopicSubscribers();
+        for (ObjectName sub: subs) {
+            if ("cliID".equals(mbs.getAttribute(sub, "ClientId"))) {
+                Integer size = (Integer) mbs.getAttribute(sub, "PendingQueueSize");
+                return size != null ? size : 0;
+            }
+        }
+        assertTrue(false);
+        return -1;
+    }
+
+    private void startBroker(boolean deleteMessages) throws Exception {
+        broker = new BrokerService();
+        broker.setBrokerName("test-broker");
+        
+        if (deleteMessages) {
+            broker.setDeleteAllMessagesOnStartup(true);
+        }
+        broker.start();
+    }
+
+    private void stopBroker() throws Exception {
+        if (broker != null)
+            broker.stop();
+        broker = null;
+    }
+    
+    abstract public PersistenceAdapter createPersistenceAdapter() throws Exception;
+
+    protected ActiveMQConnectionFactory createConnectionFactory() throws Exception {
+        return new ActiveMQConnectionFactory("vm://test-broker?jms.watchTopicAdvisories=false&waitForStart=5000&create=false");
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        startBroker(true);
+        topic = (ActiveMQTopic) createDestination();
+        mbs = ManagementFactory.getPlatformMBeanServer();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        stopBroker();
+        super.tearDown();
+    }
+}

Added: activemq/trunk/activemq-core/src/test/java/org/apache/activemq/usecases/KahaDBDurableSubscriptionSelectorTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/usecases/KahaDBDurableSubscriptionSelectorTest.java?rev=954406&view=auto
==============================================================================
--- activemq/trunk/activemq-core/src/test/java/org/apache/activemq/usecases/KahaDBDurableSubscriptionSelectorTest.java (added)
+++ activemq/trunk/activemq-core/src/test/java/org/apache/activemq/usecases/KahaDBDurableSubscriptionSelectorTest.java Mon Jun 14 10:34:54 2010
@@ -0,0 +1,31 @@
+/**
+ * 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 org.apache.activemq.store.PersistenceAdapter;
+import org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter;
+
+public class KahaDBDurableSubscriptionSelectorTest extends
+        DurableSubscriptionSelectorTest {
+
+    @Override
+    public PersistenceAdapter createPersistenceAdapter() throws Exception {
+        return new KahaDBPersistenceAdapter();
+    }
+
+}