activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r584780 - in /activemq/activemq-cpp/trunk/src: main/ main/activemq/connector/openwire/ main/activemq/connector/stomp/ main/activemq/util/ test/ test/activemq/util/
Date Mon, 15 Oct 2007 13:07:13 GMT
Author: tabish
Date: Mon Oct 15 06:07:12 2007
New Revision: 584780

URL: http://svn.apache.org/viewvc?rev=584780&view=rev
Log:
https://issues.apache.org/activemq/browse/AMQCPP-146

Add new LongSequenceGenerator to the utils package and use it in the connectors.

Added:
    activemq/activemq-cpp/trunk/src/main/activemq/util/LongSequenceGenerator.cpp
    activemq/activemq-cpp/trunk/src/main/activemq/util/LongSequenceGenerator.h
    activemq/activemq-cpp/trunk/src/test/activemq/util/LongSequenceGeneratorTest.cpp
    activemq/activemq-cpp/trunk/src/test/activemq/util/LongSequenceGeneratorTest.h
Modified:
    activemq/activemq-cpp/trunk/src/main/Makefile.am
    activemq/activemq-cpp/trunk/src/main/activemq/connector/openwire/OpenWireConnector.cpp
    activemq/activemq-cpp/trunk/src/main/activemq/connector/openwire/OpenWireConnector.h
    activemq/activemq-cpp/trunk/src/main/activemq/connector/stomp/StompConnector.cpp
    activemq/activemq-cpp/trunk/src/main/activemq/connector/stomp/StompConnector.h
    activemq/activemq-cpp/trunk/src/test/Makefile.am

Modified: activemq/activemq-cpp/trunk/src/main/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/Makefile.am?rev=584780&r1=584779&r2=584780&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/main/Makefile.am Mon Oct 15 06:07:12 2007
@@ -98,6 +98,7 @@
     activemq/util/Math.cpp \
     activemq/util/Random.cpp \
     activemq/util/PrimitiveMap.cpp \
+    activemq/util/LongSequenceGenerator.cpp \    
     activemq/util/URISupport.cpp
 
 h_sources = \
@@ -289,6 +290,7 @@
     activemq/util/Date.h \
     activemq/util/Map.h \
     activemq/util/Math.h \
+    activemq/util/LongSequenceGenerator.h \    
     activemq/util/PrimitiveMap.h \
     activemq/util/Set.h \
     activemq/util/URISupport.h \

Modified: activemq/activemq-cpp/trunk/src/main/activemq/connector/openwire/OpenWireConnector.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/connector/openwire/OpenWireConnector.cpp?rev=584780&r1=584779&r2=584780&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/connector/openwire/OpenWireConnector.cpp
(original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/connector/openwire/OpenWireConnector.cpp
Mon Oct 15 06:07:12 2007
@@ -84,12 +84,6 @@
     this->messageListener = NULL;
     this->brokerInfo = NULL;
     this->brokerWireFormatInfo = NULL;
-    this->nextConsumerId = 1;
-    this->nextProducerId = 1;
-    this->nextProducerSequenceId = 1;
-    this->nextTransactionId = 1;
-    this->nextSessionId = 1;
-    this->nextTempDestinationId = 1;
     this->properties.copy( &properties );
     this->wireFormat = dynamic_cast<OpenWireFormat*>(
         wireFormatFactory.createWireFormat( properties ) );
@@ -111,8 +105,8 @@
 ////////////////////////////////////////////////////////////////////////////////
 OpenWireConnector::~OpenWireConnector()
 {
-    try
-    {
+    try {
+
         close();
 
         delete transport;
@@ -125,72 +119,6 @@
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-long long OpenWireConnector::getNextConsumerId()
-{
-    synchronized( &mutex )
-    {
-        return nextConsumerId++;
-    }
-
-    return 0;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-long long OpenWireConnector::getNextProducerId()
-{
-    synchronized( &mutex )
-    {
-        return nextProducerId++;
-    }
-
-    return 0;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-long long OpenWireConnector::getNextProducerSequenceId()
-{
-    synchronized( &mutex )
-    {
-        return nextProducerSequenceId++;
-    }
-
-    return 0;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-long long OpenWireConnector::getNextTransactionId()
-{
-    synchronized( &mutex )
-    {
-        return nextTransactionId++;
-    }
-
-    return 0;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-long long OpenWireConnector::getNextSessionId()
-{
-    synchronized( &mutex )
-    {
-        return nextSessionId++;
-    }
-
-    return 0;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-long long OpenWireConnector::getNextTempDestinationId()
-{
-    synchronized( &mutex )
-    {
-        return nextTempDestinationId++;
-    }
-
-    return 0;
-}
-
-////////////////////////////////////////////////////////////////////////////////
 void OpenWireConnector::enforceConnected() throw ( ConnectorException )
 {
     if( state != CONNECTION_STATE_CONNECTED )
@@ -231,16 +159,17 @@
 
     try
     {
-        synchronized( &mutex )
-        {
-            if( state == CONNECTION_STATE_CONNECTED )
-            {
-                // Send the disconnect message to the broker.
-                disconnect();
+        if( state == CONNECTION_STATE_DISCONNECTED ){
+            return;
+        }
 
-                // Close the transport.
-                transport->close();
-            }
+        synchronized( &mutex ) {
+
+            // Send the disconnect message to the broker.
+            disconnect();
+
+            // Close the transport now that we've sent the last messages..
+            transport->close();
         }
     }
     AMQ_CATCH_RETHROW( ActiveMQException )
@@ -324,7 +253,7 @@
         commands::SessionInfo* info = new commands::SessionInfo();
         commands::SessionId* sessionId = new commands::SessionId();
         sessionId->setConnectionId( connectionInfo.getConnectionId()->getValue() );
-        sessionId->setValue( getNextSessionId() );
+        sessionId->setValue( sessionIds.getNextSequenceId() );
         info->setSessionId( sessionId );
         OpenWireSessionInfo* session = new OpenWireSessionInfo( this );
 
@@ -613,7 +542,7 @@
 
         consumerId->setConnectionId( session->getConnectionId() );
         consumerId->setSessionId( session->getSessionId() );
-        consumerId->setValue( getNextConsumerId() );
+        consumerId->setValue( consumerIds.getNextSequenceId() );
 
         // Cast the destination to an OpenWire destination, so we can
         // get all the goodies.
@@ -714,7 +643,7 @@
 
         producerId->setConnectionId( session->getConnectionId() );
         producerId->setSessionId( session->getSessionId() );
-        producerId->setValue( getNextProducerId() );
+        producerId->setValue( producerIds.getNextSequenceId() );
 
         // Producers are allowed to have NULL destinations.  In this case, the
         // destination is specified by the messages as they are sent.
@@ -883,7 +812,7 @@
             dynamic_cast<commands::ProducerId*>(
                 producer->getProducerInfo()->getProducerId()->cloneDataStructure()
) );
 
-        id->setProducerSequenceId( getNextProducerSequenceId() );
+        id->setProducerSequenceId( producerSequenceIds.getNextSequenceId() );
 
         amqMessage->setMessageId( id );
 
@@ -1534,7 +1463,7 @@
 {
     try {
         return connectionInfo.getConnectionId()->getValue() + ":" +
-               util::Long::toString( getNextTempDestinationId() );
+               util::Long::toString( tempDestinationIds.getNextSequenceId() );
     }
     AMQ_CATCH_RETHROW( ConnectorException )
     AMQ_CATCHALL_THROW( OpenWireConnectorException )
@@ -1549,7 +1478,7 @@
     id->setConnectionId(
         dynamic_cast<commands::ConnectionId*>(
             connectionInfo.getConnectionId()->cloneDataStructure() ) );
-    id->setValue( getNextTransactionId() );
+    id->setValue( transactionIds.getNextSequenceId() );
 
     return id;
 }

Modified: activemq/activemq-cpp/trunk/src/main/activemq/connector/openwire/OpenWireConnector.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/connector/openwire/OpenWireConnector.h?rev=584780&r1=584779&r2=584780&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/connector/openwire/OpenWireConnector.h (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/connector/openwire/OpenWireConnector.h Mon
Oct 15 06:07:12 2007
@@ -48,6 +48,7 @@
 #include <activemq/concurrent/Mutex.h>
 #include <activemq/util/Properties.h>
 #include <activemq/core/ActiveMQConstants.h>
+#include <activemq/util/LongSequenceGenerator.h>
 
 #include <activemq/connector/openwire/OpenWireCommandReader.h>
 #include <activemq/connector/openwire/OpenWireCommandWriter.h>
@@ -147,32 +148,32 @@
         /**
          * Next avaliable Producer Id
          */
-        long long nextProducerId;
+        util::LongSequenceGenerator producerIds;
 
         /**
          * Next avaliable Producer Sequence Id
          */
-        long long nextProducerSequenceId;
+        util::LongSequenceGenerator producerSequenceIds;
 
         /**
          * Next avaliable Consumer Id
          */
-        long long nextConsumerId;
+        util::LongSequenceGenerator consumerIds;
 
         /**
          * Next avaliable Transaction Id
          */
-        long long nextTransactionId;
+        util::LongSequenceGenerator transactionIds;
 
         /**
          * Next available Session Id.
          */
-        long long nextSessionId;
+        util::LongSequenceGenerator sessionIds;
 
         /**
          * Next Temporary Destination Id
          */
-        long long nextTempDestinationId;
+        util::LongSequenceGenerator tempDestinationIds;
 
         /**
          * Properties for the connector.
@@ -579,13 +580,6 @@
             const exceptions::ActiveMQException& ex );
 
     private:
-
-        long long getNextConsumerId();
-        long long getNextProducerId();
-        long long getNextTransactionId();
-        long long getNextSessionId();
-        long long getNextTempDestinationId();
-        long long getNextProducerSequenceId();
 
         // Check for Connected State and Throw an exception if not.
         void enforceConnected() throw ( ConnectorException );

Modified: activemq/activemq-cpp/trunk/src/main/activemq/connector/stomp/StompConnector.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/connector/stomp/StompConnector.cpp?rev=584780&r1=584779&r2=584780&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/connector/stomp/StompConnector.cpp (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/connector/stomp/StompConnector.cpp Mon Oct
15 06:07:12 2007
@@ -64,8 +64,6 @@
     this->exceptionListener = NULL;
     this->messageListener = NULL;
     this->sessionManager = NULL;
-    this->nextProducerId = 1;
-    this->nextTransactionId = 1;
     this->properties.copy( &properties );
 
     // Create the connection negotiator and wrap our transport
@@ -98,28 +96,6 @@
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-long long StompConnector::getNextProducerId()
-{
-    synchronized( &mutex )
-    {
-        return nextProducerId++;
-    }
-
-    return 0;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-long long StompConnector::getNextTransactionId()
-{
-    synchronized( &mutex )
-    {
-        return nextTransactionId++;
-    }
-
-    return 0;
-}
-
-////////////////////////////////////////////////////////////////////////////////
 void StompConnector::enforceConnected() throw ( ConnectorException )
 {
     if( state != CONNECTION_STATE_CONNECTED )
@@ -179,11 +155,12 @@
             return;
         }
 
-        synchronized( &mutex )
-        {
+        synchronized( &mutex ) {
+
             // Send the disconnect message to the broker.
             disconnect();
 
+            // Close the transport now that we've sent the last messages..
             transport->close();
         }
     }
@@ -362,7 +339,7 @@
         ProducerInfo* producer = new StompProducerInfo( this );
 
         producer->setDestination( destination );
-        producer->setProducerId( getNextProducerId() );
+        producer->setProducerId( producerIds.getNextSequenceId() );
         producer->setSessionInfo( session );
 
         return producer;
@@ -563,7 +540,7 @@
 
         TransactionInfo* transaction = new StompTransactionInfo( this );
 
-        transaction->setTransactionId( getNextTransactionId() );
+        transaction->setTransactionId( transactionIds.getNextSequenceId() );
 
         session->setTransactionInfo( transaction );
 

Modified: activemq/activemq-cpp/trunk/src/main/activemq/connector/stomp/StompConnector.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/connector/stomp/StompConnector.h?rev=584780&r1=584779&r2=584780&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/connector/stomp/StompConnector.h (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/connector/stomp/StompConnector.h Mon Oct
15 06:07:12 2007
@@ -32,6 +32,7 @@
 #include <activemq/core/ActiveMQConstants.h>
 #include <activemq/exceptions/IllegalArgumentException.h>
 #include <activemq/logger/LoggerDefines.h>
+#include <activemq/util/LongSequenceGenerator.h>
 
 namespace activemq{
 namespace connector{
@@ -107,12 +108,12 @@
         /**
          * Next avaliable Producer Id
          */
-        long long nextProducerId;
+        util::LongSequenceGenerator producerIds;
 
         /**
          * Next avaliable Transaction Id
          */
-        long long nextTransactionId;
+        util::LongSequenceGenerator transactionIds;
 
         /**
          * Properties for the connector.
@@ -532,9 +533,6 @@
             commands::CommandConstants::CommandId commandId );
 
     private:
-
-        long long getNextProducerId();
-        long long getNextTransactionId();
 
         // Check for Connected State and Throw an exception if not.
         void enforceConnected() throw ( ConnectorException );

Added: activemq/activemq-cpp/trunk/src/main/activemq/util/LongSequenceGenerator.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/util/LongSequenceGenerator.cpp?rev=584780&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/util/LongSequenceGenerator.cpp (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/util/LongSequenceGenerator.cpp Mon Oct 15
06:07:12 2007
@@ -0,0 +1,46 @@
+/*
+ * 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.
+ */
+
+#include "LongSequenceGenerator.h"
+#include <activemq/concurrent/Concurrent.h>
+
+using namespace activemq;
+using namespace activemq::util;
+using namespace activemq::concurrent;
+
+////////////////////////////////////////////////////////////////////////////////
+LongSequenceGenerator::LongSequenceGenerator() {
+    this->lastSequenceId = 0;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int LongSequenceGenerator::getNextSequenceId() {
+    synchronized( &mutex ) {
+        return this->lastSequenceId++;
+    }
+
+    return 0;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int LongSequenceGenerator::getLastSequenceId() {
+    synchronized( &mutex ) {
+        return this->lastSequenceId;
+    }
+
+    return 0;
+}

Added: activemq/activemq-cpp/trunk/src/main/activemq/util/LongSequenceGenerator.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/util/LongSequenceGenerator.h?rev=584780&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/util/LongSequenceGenerator.h (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/util/LongSequenceGenerator.h Mon Oct 15
06:07:12 2007
@@ -0,0 +1,57 @@
+/*
+ * 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.
+ */
+
+#ifndef _ACTIVEMQ_UTIL_LONGSEQUENCEGENERATOR_H_
+#define _ACTIVEMQ_UTIL_LONGSEQUENCEGENERATOR_H_
+
+#include <activemq/util/Config.h>
+#include <activemq/concurrent/Mutex.h>
+
+namespace activemq{
+namespace util{
+
+    /**
+     * This class is used to generate a sequence of long long values that
+     * is incremented each time a new value is requested.  This class is
+     * thread safe so the ids can be requested in different threads safely.
+     */
+    class LongSequenceGenerator {
+    private:
+
+        long long lastSequenceId;
+        activemq::concurrent::Mutex mutex;
+
+    public:
+
+        LongSequenceGenerator();
+        virtual ~LongSequenceGenerator() {}
+
+        /**
+         * @returns the next id in the sequence.
+         */
+        int getNextSequenceId();
+
+        /**
+         * @returns the last id that was generated.
+         */
+        int getLastSequenceId();
+
+    };
+
+}}
+
+#endif /*_ACTIVEMQ_UTIL_LONGSEQUENCEGENERATOR_H_*/

Modified: activemq/activemq-cpp/trunk/src/test/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/Makefile.am?rev=584780&r1=584779&r2=584780&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/test/Makefile.am Mon Oct 15 06:07:12 2007
@@ -92,6 +92,7 @@
   activemq/util/QueueTest.cpp \
   activemq/util/StringTokenizerTest.cpp \
   activemq/util/URISupportTest.cpp \
+  activemq/util/LongSequenceGeneratorTest.cpp \
   activemq/util/RandomTest.cpp \
   main.cpp
 

Added: activemq/activemq-cpp/trunk/src/test/activemq/util/LongSequenceGeneratorTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/util/LongSequenceGeneratorTest.cpp?rev=584780&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/util/LongSequenceGeneratorTest.cpp (added)
+++ activemq/activemq-cpp/trunk/src/test/activemq/util/LongSequenceGeneratorTest.cpp Mon Oct
15 06:07:12 2007
@@ -0,0 +1,37 @@
+/*
+ * 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.
+ */
+
+#include "LongSequenceGeneratorTest.h"
+
+CPPUNIT_TEST_SUITE_REGISTRATION( activemq::util::LongSequenceGeneratorTest );
+
+using namespace activemq;
+using namespace activemq::util;
+
+////////////////////////////////////////////////////////////////////////////////
+void LongSequenceGeneratorTest::test() {
+
+    LongSequenceGenerator sequence;
+
+    long long result1 = sequence.getNextSequenceId();
+    long long result2 = sequence.getNextSequenceId();
+
+    CPPUNIT_ASSERT( result1 < result2 );
+    CPPUNIT_ASSERT( result2 < sequence.getLastSequenceId() );
+    CPPUNIT_ASSERT( result2 < sequence.getNextSequenceId() );
+
+}

Added: activemq/activemq-cpp/trunk/src/test/activemq/util/LongSequenceGeneratorTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/util/LongSequenceGeneratorTest.h?rev=584780&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/util/LongSequenceGeneratorTest.h (added)
+++ activemq/activemq-cpp/trunk/src/test/activemq/util/LongSequenceGeneratorTest.h Mon Oct
15 06:07:12 2007
@@ -0,0 +1,46 @@
+/*
+ * 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.
+ */
+
+#ifndef _ACTIVEMQ_UTIL_LONGSEQUENCEGENERATORTEST_H_
+#define _ACTIVEMQ_UTIL_LONGSEQUENCEGENERATORTEST_H_
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <activemq/util/LongSequenceGenerator.h>
+
+namespace activemq{
+namespace util{
+
+    class LongSequenceGeneratorTest : public CppUnit::TestFixture
+    {
+        CPPUNIT_TEST_SUITE( LongSequenceGeneratorTest );
+        CPPUNIT_TEST( test );
+        CPPUNIT_TEST_SUITE_END();
+
+    public:
+
+        LongSequenceGeneratorTest() {}
+        virtual ~LongSequenceGeneratorTest() {}
+
+        void test();
+
+    };
+
+}}
+
+#endif /*_ACTIVEMQ_UTIL_LONGSEQUENCEGENERATORTEST_H_*/



Mime
View raw message