[ 
https://issues.apache.org/jira/browse/ARTEMIS-3365?focusedWorklogId=634153&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-634153
 ]

ASF GitHub Bot logged work on ARTEMIS-3365:
-------------------------------------------

                Author: ASF GitHub Bot
            Created on: 05/Aug/21 11:19
            Start Date: 05/Aug/21 11:19
    Worklog Time Spent: 10m 
      Work Description: brusdev commented on a change in pull request #3634:
URL: https://github.com/apache/activemq-artemis/pull/3634#discussion_r682830416



##########
File path: 
tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/balancing/TargetKeyTest.java
##########
@@ -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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.artemis.tests.integration.balancing;
+
+import org.apache.activemq.artemis.core.remoting.impl.netty.TransportConstants;
+import 
org.apache.activemq.artemis.core.server.balancing.policies.FirstElementPolicy;
+import org.apache.activemq.artemis.core.server.balancing.policies.Policy;
+import 
org.apache.activemq.artemis.core.server.balancing.policies.PolicyFactory;
+import 
org.apache.activemq.artemis.core.server.balancing.policies.PolicyFactoryResolver;
+import org.apache.activemq.artemis.core.server.balancing.targets.Target;
+import org.apache.activemq.artemis.core.server.balancing.targets.TargetKey;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+@RunWith(Parameterized.class)
+public class TargetKeyTest extends BalancingTestBase {
+
+   private static final String MOCK_POLICY_NAME = "MOCK_POLICY";
+
+   @Parameterized.Parameters(name = "protocol: {0}")
+   public static Collection<Object[]> data() {
+      Collection<Object[]> data = new ArrayList<>();
+
+      for (String protocol : Arrays.asList(new String[] {AMQP_PROTOCOL, 
CORE_PROTOCOL, OPENWIRE_PROTOCOL})) {
+         data.add(new Object[] {protocol});
+      }
+
+      return data;
+   }
+
+
+   private final String protocol;
+
+   private final List<String> keys = new ArrayList<>();
+
+
+   public TargetKeyTest(String protocol) {
+      this.protocol = protocol;
+   }
+
+   @Before
+   public void setup() throws Exception {
+      PolicyFactoryResolver.getInstance().registerPolicyFactory(
+         new PolicyFactory() {
+            @Override
+            public String[] getSupportedPolicies() {
+               return new String[] {MOCK_POLICY_NAME};
+            }
+
+            @Override
+            public Policy createPolicy(String policyName) {
+               return new FirstElementPolicy(MOCK_POLICY_NAME) {
+                  @Override
+                  public Target selectTarget(List<Target> targets, String key) 
{
+                     keys.add(key);
+                     return super.selectTarget(targets, key);
+                  }
+               };
+            }
+         });
+   }
+
+   @Test
+   public void testClientIDKey() throws Exception {
+      setupLiveServerWithDiscovery(0, GROUP_ADDRESS, GROUP_PORT, true, true, 
false);
+      setupBalancerServerWithDiscovery(0, TargetKey.CLIENT_ID, 
MOCK_POLICY_NAME, null, true, null, 1);
+      startServers(0);
+
+      ConnectionFactory connectionFactory = createFactory(protocol, false, 
TransportConstants.DEFAULT_HOST,
+         TransportConstants.DEFAULT_PORT + 0, "test", null, null);
+
+      keys.clear();
+
+      try (Connection connection = connectionFactory.createConnection()) {
+         connection.start();
+      }
+
+      Assert.assertEquals(1, keys.size());
+      Assert.assertEquals("test", keys.get(0));
+   }
+
+   @Test
+   public void testSNIHostKey() throws Exception {
+      setupLiveServerWithDiscovery(0, GROUP_ADDRESS, GROUP_PORT, true, true, 
false);
+      
getDefaultServerAcceptor(0).getParams().put(TransportConstants.SSL_ENABLED_PROP_NAME,
 true);
+      
getDefaultServerAcceptor(0).getParams().put(TransportConstants.KEYSTORE_PATH_PROP_NAME,
 "verified-localdomain-keystore.jks");
+      
getDefaultServerAcceptor(0).getParams().put(TransportConstants.KEYSTORE_PASSWORD_PROP_NAME,
 "secureexample");
+
+      setupBalancerServerWithDiscovery(0, TargetKey.SNI_HOST, 
MOCK_POLICY_NAME, null, true, null, 1);
+      startServers(0);
+
+      ConnectionFactory connectionFactory = createFactory(protocol, true, 
"localhost.localdomain",

Review comment:
       I can set SNI value explicitly for CORE client. I'm not sure I can set 
SNI value explicitly for qpid jms client or openwire client.

##########
File path: 
examples/features/broker-balancer/symmetric-redirect/src/main/java/org/apache/activemq/artemis/jms/example/SymmetricRedirectExample.java
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.artemis.jms.example;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+
+import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
+
+/**
+ * This example is demonstrating how incoming client connections are evely 
redirected from one broker towards
+ * worker brokers.
+ */
+public class SymmetricRedirectExample {
+
+   public static void main(final String[] args) throws Exception {
+
+      ConnectionFactory connectionFactoryClient0Server0 = new 
ActiveMQConnectionFactory("tcp://localhost:61616?ha=true&reconnectAttempts=30&clientID=CLIENT_0");
+      ConnectionFactory connectionFactoryClient1Server0 = new 
ActiveMQConnectionFactory("tcp://localhost:61616?ha=true&reconnectAttempts=30&clientID=CLIENT_1");
+      ConnectionFactory connectionFactoryClient0Server1 = new 
ActiveMQConnectionFactory("tcp://localhost:61617?ha=true&reconnectAttempts=30&clientID=CLIENT_0");
+      ConnectionFactory connectionFactoryClient1Server1 = new 
ActiveMQConnectionFactory("tcp://localhost:61617?ha=true&reconnectAttempts=30&clientID=CLIENT_1");
+
+      /**
+       * Step 1. Create a connection for producer0 and producer1, and send a 
few messages.
+       * the server0 will redirect the connection of each producer to a 
different target brokers.
+       */
+      Connection connectionProducer0 = null;
+      Connection connectionProducer1 = null;
+
+      try {
+         connectionProducer0 = 
connectionFactoryClient0Server0.createConnection();
+         connectionProducer1 = 
connectionFactoryClient1Server0.createConnection();
+
+         for (Connection connectionProducer : new Connection[] 
{connectionProducer0, connectionProducer1}) {
+            Session session = connectionProducer.createSession(false, 
Session.AUTO_ACKNOWLEDGE);
+
+            Queue queue = session.createQueue("exampleQueue" + 
connectionProducer.getClientID());
+            MessageProducer sender = session.createProducer(queue);
+            for (int i = 0; i < 100; i++) {
+               TextMessage message = session.createTextMessage("Hello world n" 
+ i + " - " + connectionProducer.getClientID());
+               System.out.println("Sending message " + message.getText() + "/" 
+ connectionProducer.getClientID());
+               sender.send(message);
+            }
+         }
+      } finally {
+         if (connectionProducer0 != null) {
+            connectionProducer0.close();
+         }
+
+         if (connectionProducer1 != null) {
+            connectionProducer1.close();
+         }
+      }
+
+      /**
+       * Step 2. create a connection for consumer0 and consumer1, and receive 
a few messages.
+       * the server0 will redirect the connection to the same target broker of 
the respective producer
+       * because the consumer and the producer share the same clientID.

Review comment:
       I meant clientID prefix, I'll fix it.

##########
File path: 
examples/features/broker-balancer/evenly-redirect/src/main/java/org/apache/activemq/artemis/jms/example/EvenlyRedirectExample.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.artemis.jms.example;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+
+import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
+
+/**
+ * This example is demonstrating how incoming client connections are evely 
redirected from one broker towards
+ * worker brokers.
+ */
+public class EvenlyRedirectExample {
+
+   public static void main(final String[] args) throws Exception {
+
+      ConnectionFactory connectionFactoryClient0 = new 
ActiveMQConnectionFactory("tcp://localhost:61616?ha=true&reconnectAttempts=30&clientID=CLIENT_0");
+      ConnectionFactory connectionFactoryClient1 = new 
ActiveMQConnectionFactory("tcp://localhost:61616?ha=true&reconnectAttempts=30&clientID=CLIENT_1");
+
+      /**
+       * Step 1. Create a connection for producer0 and producer1, and send a 
few messages.
+       * the server0 will redirect the connection of each producer to a 
different target brokers.
+       */
+      Connection connectionProducer0 = null;
+      Connection connectionProducer1 = null;
+
+      try {
+         connectionProducer0 = connectionFactoryClient0.createConnection();
+         connectionProducer1 = connectionFactoryClient1.createConnection();
+
+         for (Connection connectionProducer : new Connection[] 
{connectionProducer0, connectionProducer1}) {
+            Session session = connectionProducer.createSession(false, 
Session.AUTO_ACKNOWLEDGE);
+
+            Queue queue = session.createQueue("exampleQueue");
+            MessageProducer sender = session.createProducer(queue);
+            for (int i = 0; i < 100; i++) {
+               sender.send(session.createTextMessage("Hello world n" + i + " - 
" + connectionProducer.getClientID()));
+            }
+         }
+      } finally {
+         if (connectionProducer0 != null) {
+            connectionProducer0.close();
+         }
+
+         if (connectionProducer1 != null) {
+            connectionProducer1.close();
+         }
+      }
+
+      /**
+       * Step 2. create a connection for consumer0 and consumer1, and receive 
a few messages.
+       * the server0 will redirect the connection to the same target broker of 
the respective producer
+       * because the consumer and the producer share the same clientID.

Review comment:
       The broker balancer defined int the evenly-redirect example uses a cache 
based on the CLIENT_ID key to provide affinity. I'll change the example to 
prove it.

##########
File path: 
examples/features/broker-balancer/evenly-redirect/src/main/java/org/apache/activemq/artemis/jms/example/EvenlyRedirectExample.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.artemis.jms.example;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+
+import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
+
+/**
+ * This example is demonstrating how incoming client connections are evely 
redirected from one broker towards
+ * worker brokers.
+ */
+public class EvenlyRedirectExample {
+
+   public static void main(final String[] args) throws Exception {
+
+      ConnectionFactory connectionFactoryClient0 = new 
ActiveMQConnectionFactory("tcp://localhost:61616?ha=true&reconnectAttempts=30&clientID=CLIENT_0");
+      ConnectionFactory connectionFactoryClient1 = new 
ActiveMQConnectionFactory("tcp://localhost:61616?ha=true&reconnectAttempts=30&clientID=CLIENT_1");
+
+      /**
+       * Step 1. Create a connection for producer0 and producer1, and send a 
few messages.
+       * the server0 will redirect the connection of each producer to a 
different target brokers.
+       */
+      Connection connectionProducer0 = null;
+      Connection connectionProducer1 = null;
+
+      try {
+         connectionProducer0 = connectionFactoryClient0.createConnection();
+         connectionProducer1 = connectionFactoryClient1.createConnection();
+
+         for (Connection connectionProducer : new Connection[] 
{connectionProducer0, connectionProducer1}) {
+            Session session = connectionProducer.createSession(false, 
Session.AUTO_ACKNOWLEDGE);
+
+            Queue queue = session.createQueue("exampleQueue");
+            MessageProducer sender = session.createProducer(queue);
+            for (int i = 0; i < 100; i++) {
+               sender.send(session.createTextMessage("Hello world n" + i + " - 
" + connectionProducer.getClientID()));
+            }
+         }
+      } finally {
+         if (connectionProducer0 != null) {
+            connectionProducer0.close();
+         }
+
+         if (connectionProducer1 != null) {
+            connectionProducer1.close();
+         }
+      }
+
+      /**
+       * Step 2. create a connection for consumer0 and consumer1, and receive 
a few messages.
+       * the server0 will redirect the connection to the same target broker of 
the respective producer
+       * because the consumer and the producer share the same clientID.

Review comment:
       The broker balancer defined in the evenly-redirect example uses a cache 
based on the CLIENT_ID prefix key to provide affinity. I'll change the example 
to prove it.

##########
File path: 
tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/balancing/TargetKeyTest.java
##########
@@ -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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.artemis.tests.integration.balancing;
+
+import org.apache.activemq.artemis.core.remoting.impl.netty.TransportConstants;
+import 
org.apache.activemq.artemis.core.server.balancing.policies.FirstElementPolicy;
+import org.apache.activemq.artemis.core.server.balancing.policies.Policy;
+import 
org.apache.activemq.artemis.core.server.balancing.policies.PolicyFactory;
+import 
org.apache.activemq.artemis.core.server.balancing.policies.PolicyFactoryResolver;
+import org.apache.activemq.artemis.core.server.balancing.targets.Target;
+import org.apache.activemq.artemis.core.server.balancing.targets.TargetKey;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+@RunWith(Parameterized.class)
+public class TargetKeyTest extends BalancingTestBase {
+
+   private static final String MOCK_POLICY_NAME = "MOCK_POLICY";
+
+   @Parameterized.Parameters(name = "protocol: {0}")
+   public static Collection<Object[]> data() {
+      Collection<Object[]> data = new ArrayList<>();
+
+      for (String protocol : Arrays.asList(new String[] {AMQP_PROTOCOL, 
CORE_PROTOCOL, OPENWIRE_PROTOCOL})) {
+         data.add(new Object[] {protocol});
+      }
+
+      return data;
+   }
+
+
+   private final String protocol;
+
+   private final List<String> keys = new ArrayList<>();
+
+
+   public TargetKeyTest(String protocol) {
+      this.protocol = protocol;
+   }
+
+   @Before
+   public void setup() throws Exception {
+      PolicyFactoryResolver.getInstance().registerPolicyFactory(
+         new PolicyFactory() {
+            @Override
+            public String[] getSupportedPolicies() {
+               return new String[] {MOCK_POLICY_NAME};
+            }
+
+            @Override
+            public Policy createPolicy(String policyName) {
+               return new FirstElementPolicy(MOCK_POLICY_NAME) {
+                  @Override
+                  public Target selectTarget(List<Target> targets, String key) 
{
+                     keys.add(key);
+                     return super.selectTarget(targets, key);
+                  }
+               };
+            }
+         });
+   }
+
+   @Test
+   public void testClientIDKey() throws Exception {
+      setupLiveServerWithDiscovery(0, GROUP_ADDRESS, GROUP_PORT, true, true, 
false);
+      setupBalancerServerWithDiscovery(0, TargetKey.CLIENT_ID, 
MOCK_POLICY_NAME, null, true, null, 1);
+      startServers(0);
+
+      ConnectionFactory connectionFactory = createFactory(protocol, false, 
TransportConstants.DEFAULT_HOST,
+         TransportConstants.DEFAULT_PORT + 0, "test", null, null);
+
+      keys.clear();
+
+      try (Connection connection = connectionFactory.createConnection()) {
+         connection.start();
+      }
+
+      Assert.assertEquals(1, keys.size());
+      Assert.assertEquals("test", keys.get(0));
+   }
+
+   @Test
+   public void testSNIHostKey() throws Exception {
+      setupLiveServerWithDiscovery(0, GROUP_ADDRESS, GROUP_PORT, true, true, 
false);
+      
getDefaultServerAcceptor(0).getParams().put(TransportConstants.SSL_ENABLED_PROP_NAME,
 true);
+      
getDefaultServerAcceptor(0).getParams().put(TransportConstants.KEYSTORE_PATH_PROP_NAME,
 "verified-localdomain-keystore.jks");
+      
getDefaultServerAcceptor(0).getParams().put(TransportConstants.KEYSTORE_PASSWORD_PROP_NAME,
 "secureexample");
+
+      setupBalancerServerWithDiscovery(0, TargetKey.SNI_HOST, 
MOCK_POLICY_NAME, null, true, null, 1);
+      startServers(0);
+
+      ConnectionFactory connectionFactory = createFactory(protocol, true, 
"localhost.localdomain",

Review comment:
       I have just pushed a commit to skip this test if `localhost.localdomain` 
is not valid.

##########
File path: 
tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/balancing/TargetKeyTest.java
##########
@@ -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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.artemis.tests.integration.balancing;
+
+import org.apache.activemq.artemis.core.remoting.impl.netty.TransportConstants;
+import 
org.apache.activemq.artemis.core.server.balancing.policies.FirstElementPolicy;
+import org.apache.activemq.artemis.core.server.balancing.policies.Policy;
+import 
org.apache.activemq.artemis.core.server.balancing.policies.PolicyFactory;
+import 
org.apache.activemq.artemis.core.server.balancing.policies.PolicyFactoryResolver;
+import org.apache.activemq.artemis.core.server.balancing.targets.Target;
+import org.apache.activemq.artemis.core.server.balancing.targets.TargetKey;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+@RunWith(Parameterized.class)
+public class TargetKeyTest extends BalancingTestBase {
+
+   private static final String MOCK_POLICY_NAME = "MOCK_POLICY";
+
+   @Parameterized.Parameters(name = "protocol: {0}")
+   public static Collection<Object[]> data() {
+      Collection<Object[]> data = new ArrayList<>();
+
+      for (String protocol : Arrays.asList(new String[] {AMQP_PROTOCOL, 
CORE_PROTOCOL, OPENWIRE_PROTOCOL})) {
+         data.add(new Object[] {protocol});
+      }
+
+      return data;
+   }
+
+
+   private final String protocol;
+
+   private final List<String> keys = new ArrayList<>();
+
+
+   public TargetKeyTest(String protocol) {
+      this.protocol = protocol;
+   }
+
+   @Before
+   public void setup() throws Exception {
+      PolicyFactoryResolver.getInstance().registerPolicyFactory(
+         new PolicyFactory() {
+            @Override
+            public String[] getSupportedPolicies() {
+               return new String[] {MOCK_POLICY_NAME};
+            }
+
+            @Override
+            public Policy createPolicy(String policyName) {
+               return new FirstElementPolicy(MOCK_POLICY_NAME) {
+                  @Override
+                  public Target selectTarget(List<Target> targets, String key) 
{
+                     keys.add(key);
+                     return super.selectTarget(targets, key);
+                  }
+               };
+            }
+         });
+   }
+
+   @Test
+   public void testClientIDKey() throws Exception {
+      setupLiveServerWithDiscovery(0, GROUP_ADDRESS, GROUP_PORT, true, true, 
false);
+      setupBalancerServerWithDiscovery(0, TargetKey.CLIENT_ID, 
MOCK_POLICY_NAME, null, true, null, 1);
+      startServers(0);
+
+      ConnectionFactory connectionFactory = createFactory(protocol, false, 
TransportConstants.DEFAULT_HOST,
+         TransportConstants.DEFAULT_PORT + 0, "test", null, null);
+
+      keys.clear();
+
+      try (Connection connection = connectionFactory.createConnection()) {
+         connection.start();
+      }
+
+      Assert.assertEquals(1, keys.size());
+      Assert.assertEquals("test", keys.get(0));
+   }
+
+   @Test
+   public void testSNIHostKey() throws Exception {
+      setupLiveServerWithDiscovery(0, GROUP_ADDRESS, GROUP_PORT, true, true, 
false);
+      
getDefaultServerAcceptor(0).getParams().put(TransportConstants.SSL_ENABLED_PROP_NAME,
 true);
+      
getDefaultServerAcceptor(0).getParams().put(TransportConstants.KEYSTORE_PATH_PROP_NAME,
 "verified-localdomain-keystore.jks");

Review comment:
       keystore replaced

##########
File path: examples/features/broker-balancer/symmetric-redirect/readme.md
##########
@@ -0,0 +1,5 @@
+# AMQP Broker Connection with Receivers

Review comment:
       title fixed

##########
File path: examples/features/broker-balancer/symmetric-redirect/readme.md
##########
@@ -0,0 +1,5 @@
+# AMQP Broker Connection with Receivers
+
+To run the example, simply type **mvn verify** from this directory, or **mvn 
-PnoServer verify** if you want to create and start the broker manually.
+
+This example demonstrates how you can distribute incoming client connections 
across two brokers using a symmetric architecture.

Review comment:
       I have added a few words about the 'symmetric architecture'

##########
File path: examples/features/broker-balancer/pom.xml
##########
@@ -0,0 +1,62 @@
+<?xml version='1.0'?>
+<!--
+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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+   <modelVersion>4.0.0</modelVersion>
+
+   <parent>
+      <groupId>org.apache.activemq.examples.clustered</groupId>
+      <artifactId>broker-features</artifactId>
+      <version>2.18.0-SNAPSHOT</version>
+   </parent>
+
+   <groupId>org.apache.activemq.examples</groupId>
+   <artifactId>broker-balancer</artifactId>
+   <packaging>pom</packaging>
+   <name>ActiveMQ Artemis Broker Balancer Examples</name>
+
+   <!-- Properties -->
+   <properties>
+      <!--
+      Explicitly declaring the source encoding eliminates the following
+      message: [WARNING] Using platform encoding (UTF-8 actually) to copy
+      filtered resources, i.e. build is platform dependent!
+      -->
+      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

Review comment:
       removed

##########
File path: 
examples/features/broker-balancer/evenly-redirect/src/main/java/org/apache/activemq/artemis/jms/example/EvenlyRedirectExample.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.artemis.jms.example;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+
+import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
+
+/**
+ * This example is demonstrating how incoming client connections are evely 
redirected from one broker towards
+ * worker brokers.
+ */
+public class EvenlyRedirectExample {
+
+   public static void main(final String[] args) throws Exception {
+
+      ConnectionFactory connectionFactoryClient0 = new 
ActiveMQConnectionFactory("tcp://localhost:61616?ha=true&reconnectAttempts=30&clientID=CLIENT_0");
+      ConnectionFactory connectionFactoryClient1 = new 
ActiveMQConnectionFactory("tcp://localhost:61616?ha=true&reconnectAttempts=30&clientID=CLIENT_1");
+
+      /**
+       * Step 1. Create a connection for producer0 and producer1, and send a 
few messages.
+       * the server0 will redirect the connection of each producer to a 
different target brokers.
+       */
+      Connection connectionProducer0 = null;
+      Connection connectionProducer1 = null;
+
+      try {
+         connectionProducer0 = connectionFactoryClient0.createConnection();
+         connectionProducer1 = connectionFactoryClient1.createConnection();
+
+         for (Connection connectionProducer : new Connection[] 
{connectionProducer0, connectionProducer1}) {
+            Session session = connectionProducer.createSession(false, 
Session.AUTO_ACKNOWLEDGE);
+
+            Queue queue = session.createQueue("exampleQueue");
+            MessageProducer sender = session.createProducer(queue);
+            for (int i = 0; i < 100; i++) {
+               sender.send(session.createTextMessage("Hello world n" + i + " - 
" + connectionProducer.getClientID()));
+            }
+         }
+      } finally {
+         if (connectionProducer0 != null) {
+            connectionProducer0.close();
+         }
+
+         if (connectionProducer1 != null) {
+            connectionProducer1.close();
+         }
+      }
+
+      /**
+       * Step 2. create a connection for consumer0 and consumer1, and receive 
a few messages.
+       * the server0 will redirect the connection to the same target broker of 
the respective producer
+       * because the consumer and the producer share the same clientID.

Review comment:
       I missed to commit the updated example. Now it is updated and it uses 
`<target-key-filter>^.{3}</target-key-filter>`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 634153)
    Time Spent: 7h 20m  (was: 7h 10m)

> Broker Balancers
> ----------------
>
>                 Key: ARTEMIS-3365
>                 URL: https://issues.apache.org/jira/browse/ARTEMIS-3365
>             Project: ActiveMQ Artemis
>          Issue Type: New Feature
>            Reporter: Domenico Francesco Bruscino
>            Assignee: Domenico Francesco Bruscino
>            Priority: Major
>          Time Spent: 7h 20m
>  Remaining Estimate: 0h
>
> This feature adds the broker balancers to distribute the incoming client 
> connections across multiple brokers.
> It provides a native redirection for supported clients and a new management 
> API for other clients. The native redirection can be enabled per acceptor and 
> is supported only for CORE and AMQP clients.
> See the [draft 
> documentation|https://github.com/brusdev/activemq-artemis/blob/broker_balancers/docs/user-manual/en/broker-balancers.md]
>  for further details. 



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to