activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clebertsuco...@apache.org
Subject [1/2] activemq-artemis git commit: ARTEMIS-1895 - Add duplicate metadata failure callback to ActiveMQServerPlugin
Date Mon, 04 Jun 2018 14:01:53 GMT
Repository: activemq-artemis
Updated Branches:
  refs/heads/master 2baf37756 -> 81935cfa0


ARTEMIS-1895 - Add duplicate metadata failure callback to ActiveMQServerPlugin

Add a callback on duplicate metadata which will allow extra
functionality to be added.


Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/40ade119
Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/40ade119
Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/40ade119

Branch: refs/heads/master
Commit: 40ade11981135709e7299a295d1f6c8b9de0f4b3
Parents: 2baf377
Author: Christopher L. Shannon (cshannon) <christopher.l.shannon@gmail.com>
Authored: Wed May 30 08:50:05 2018 -0400
Committer: Clebert Suconic <clebertsuconic@apache.org>
Committed: Mon Jun 4 10:01:46 2018 -0400

----------------------------------------------------------------------
 .../artemis/jms/client/ActiveMQConnection.java  | 25 +++++-----
 .../core/server/impl/ServerSessionImpl.java     |  3 ++
 .../server/plugin/ActiveMQServerPlugin.java     | 12 +++++
 .../client/SessionMetadataAddExceptionTest.java | 48 ++++++++++++++++++++
 4 files changed, 77 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/40ade119/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnection.java
----------------------------------------------------------------------
diff --git a/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnection.java
b/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnection.java
index bf0d236..357b7f6 100644
--- a/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnection.java
+++ b/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnection.java
@@ -16,6 +16,14 @@
  */
 package org.apache.activemq.artemis.jms.client;
 
+import java.lang.ref.WeakReference;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
 import javax.jms.ConnectionConsumer;
 import javax.jms.ConnectionMetaData;
 import javax.jms.Destination;
@@ -32,13 +40,6 @@ import javax.jms.Session;
 import javax.jms.Topic;
 import javax.jms.TopicConnection;
 import javax.jms.TopicSession;
-import java.lang.ref.WeakReference;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
-import java.util.HashSet;
-import java.util.Set;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
 
 import org.apache.activemq.artemis.api.core.ActiveMQException;
 import org.apache.activemq.artemis.api.core.ActiveMQExceptionType;
@@ -242,10 +243,9 @@ public class ActiveMQConnection extends ActiveMQConnectionForContextImpl
impleme
          throw new IllegalStateException("setClientID can only be called directly after the
connection is created");
       }
 
-      validateClientID(initialSession, clientID);
-
-      this.clientID = clientID;
       try {
+         validateClientID(initialSession, clientID);
+         this.clientID = clientID;
          this.addSessionMetaData(initialSession);
       } catch (ActiveMQException e) {
          JMSException ex = new JMSException("Internal error setting metadata jms-client-id");
@@ -257,12 +257,15 @@ public class ActiveMQConnection extends ActiveMQConnectionForContextImpl
impleme
       justCreated = false;
    }
 
-   private void validateClientID(ClientSession validateSession, String clientID) throws InvalidClientIDException
{
+   private void validateClientID(ClientSession validateSession, String clientID)
+         throws InvalidClientIDException, ActiveMQException {
       try {
          validateSession.addUniqueMetaData(ClientSession.JMS_SESSION_CLIENT_ID_PROPERTY,
clientID);
       } catch (ActiveMQException e) {
          if (e.getType() == ActiveMQExceptionType.DUPLICATE_METADATA) {
             throw new InvalidClientIDException("clientID=" + clientID + " was already set
into another connection");
+         } else {
+            throw e;
          }
       }
    }

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/40ade119/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ServerSessionImpl.java
----------------------------------------------------------------------
diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ServerSessionImpl.java
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ServerSessionImpl.java
index 3057041..f00fe19 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ServerSessionImpl.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ServerSessionImpl.java
@@ -1523,6 +1523,9 @@ public class ServerSessionImpl implements ServerSession, FailureListener
{
       ServerSession sessionWithMetaData = server.lookupSession(key, data);
       if (sessionWithMetaData != null && sessionWithMetaData != this) {
          // There is a duplication of this property
+         if (server.hasBrokerPlugins()) {
+            server.callBrokerPlugins(plugin -> plugin.duplicateSessionMetadataFailure(this,
key, data));
+         }
          return false;
       } else {
          addMetaData(key, data);

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/40ade119/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/plugin/ActiveMQServerPlugin.java
----------------------------------------------------------------------
diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/plugin/ActiveMQServerPlugin.java
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/plugin/ActiveMQServerPlugin.java
index db66bfe..97e23b4 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/plugin/ActiveMQServerPlugin.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/plugin/ActiveMQServerPlugin.java
@@ -158,6 +158,18 @@ public interface ActiveMQServerPlugin {
    }
 
    /**
+    * Called when adding session metadata fails because the metadata is a duplicate
+    *
+    * @param session
+    * @param key
+    * @param data
+    * @throws ActiveMQException
+    */
+   default void duplicateSessionMetadataFailure(ServerSession session, String key, String
data) throws ActiveMQException {
+
+   }
+
+   /**
     * After session metadata is added to the session
     *
     * @param session

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/40ade119/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jms/client/SessionMetadataAddExceptionTest.java
----------------------------------------------------------------------
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jms/client/SessionMetadataAddExceptionTest.java
b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jms/client/SessionMetadataAddExceptionTest.java
index 4bc0522..39a7cc0 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jms/client/SessionMetadataAddExceptionTest.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jms/client/SessionMetadataAddExceptionTest.java
@@ -16,7 +16,10 @@
  */
 package org.apache.activemq.artemis.tests.integration.jms.client;
 
+import java.util.concurrent.atomic.AtomicInteger;
+
 import javax.jms.Connection;
+import javax.jms.InvalidClientIDException;
 import javax.jms.JMSException;
 
 import org.apache.activemq.artemis.api.core.ActiveMQException;
@@ -33,9 +36,12 @@ import org.junit.Test;
  * is added
  */
 public class SessionMetadataAddExceptionTest extends JMSTestBase {
+   private AtomicInteger duplicateCount = new AtomicInteger();
 
    @Override
    protected Configuration createDefaultConfig(boolean netty) throws Exception {
+      duplicateCount.set(0);
+
       Configuration config = super.createDefaultConfig(netty);
       config.registerBrokerPlugin(new ActiveMQServerPlugin() {
 
@@ -50,6 +56,18 @@ public class SessionMetadataAddExceptionTest extends JMSTestBase {
             }
          }
 
+         @Override
+         public void duplicateSessionMetadataFailure(ServerSession session, String key, String
data)
+               throws ActiveMQException {
+
+            //count number of times method called
+            duplicateCount.incrementAndGet();
+
+            if (data.equals("valid2")) {
+               throw new ActiveMQException("failure");
+            }
+         }
+
       });
 
       return config;
@@ -76,6 +94,36 @@ public class SessionMetadataAddExceptionTest extends JMSTestBase {
    }
 
    @Test(timeout = 5000)
+   public void testDuplicateClientIdSet() throws Exception {
+      ActiveMQConnectionFactory activeMQConnectionFactory = (ActiveMQConnectionFactory) cf;
+      Connection con = cf.createConnection();
+      Connection con2 = cf.createConnection();
+      try {
+         con.setClientID("valid");
+         con2.setClientID("valid");
+         fail("Should have failed for duplicate clientId");
+      } catch (InvalidClientIDException e) {
+         assertEquals(1, duplicateCount.get());
+      } finally {
+         activeMQConnectionFactory.close();
+      }
+   }
+
+   @Test(timeout = 5000, expected = JMSException.class)
+   public void testDuplicateClientIdSetActiveMQException() throws Exception {
+      ActiveMQConnectionFactory activeMQConnectionFactory = (ActiveMQConnectionFactory) cf;
+      Connection con = cf.createConnection();
+      Connection con2 = cf.createConnection();
+      try {
+         con.setClientID("valid2");
+         con2.setClientID("valid2");
+         fail("Should have failed");
+      } finally {
+         activeMQConnectionFactory.close();
+      }
+   }
+
+   @Test(timeout = 5000)
    public void testValidIdSetConnection() throws Exception {
       Connection con = cf.createConnection();
       try {


Mime
View raw message