Search in sources :

Example 6 with CoreQueueConfiguration

use of org.apache.activemq.artemis.core.config.CoreQueueConfiguration in project activemq-artemis by apache.

the class BridgeWithDiscoveryGroupStartTest method testStartStop.

@Test
public void testStartStop() throws Exception {
    Map<String, Object> server0Params = new HashMap<>();
    ActiveMQServer server0 = createClusteredServerWithParams(isNetty(), 0, true, server0Params);
    Map<String, Object> server1Params = new HashMap<>();
    if (isNetty()) {
        server1Params.put("port", TransportConstants.DEFAULT_PORT + 1);
    } else {
        server1Params.put(org.apache.activemq.artemis.core.remoting.impl.invm.TransportConstants.SERVER_ID_PROP_NAME, 1);
    }
    ActiveMQServer server1 = createClusteredServerWithParams(isNetty(), 1, true, server1Params);
    ServerLocator locator = null;
    try {
        Map<String, TransportConfiguration> connectors = new HashMap<>();
        TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params);
        TransportConfiguration server1tc = new TransportConfiguration(getConnector(), server1Params);
        connectors.put(server1tc.getName(), server1tc);
        server0.getConfiguration().setConnectorConfigurations(connectors);
        final String testAddress = "testAddress";
        final String queueName0 = "queue0";
        final String forwardAddress = "forwardAddress";
        final String queueName1 = "queue1";
        final String groupAddress = getUDPDiscoveryAddress();
        final int port = getUDPDiscoveryPort();
        ArrayList<String> list = new ArrayList<>();
        list.add(server1tc.getName());
        UDPBroadcastEndpointFactory endpoint = new UDPBroadcastEndpointFactory().setGroupAddress(groupAddress).setGroupPort(port);
        BroadcastGroupConfiguration bcConfig = new BroadcastGroupConfiguration().setName("bg1").setBroadcastPeriod(250).setConnectorInfos(list).setEndpointFactory(endpoint);
        server0.getConfiguration().getBroadcastGroupConfigurations().add(bcConfig);
        DiscoveryGroupConfiguration dcConfig = new DiscoveryGroupConfiguration().setName("dg1").setRefreshTimeout(5000).setDiscoveryInitialWaitTimeout(5000).setBroadcastEndpointFactory(endpoint);
        server0.getConfiguration().getDiscoveryGroupConfigurations().put(dcConfig.getName(), dcConfig);
        final String bridgeName = "bridge1";
        ArrayList<String> staticConnectors = new ArrayList<>();
        staticConnectors.add(server1tc.getName());
        BridgeConfiguration bridgeConfiguration = new BridgeConfiguration().setName(bridgeName).setQueueName(queueName0).setForwardingAddress(forwardAddress).setRetryInterval(1000).setReconnectAttempts(0).setReconnectAttemptsOnSameNode(0).setConfirmationWindowSize(1024).setStaticConnectors(staticConnectors);
        List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
        bridgeConfigs.add(bridgeConfiguration);
        server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);
        CoreQueueConfiguration queueConfig0 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName0);
        List<CoreQueueConfiguration> queueConfigs0 = new ArrayList<>();
        queueConfigs0.add(queueConfig0);
        server0.getConfiguration().setQueueConfigurations(queueConfigs0);
        CoreQueueConfiguration queueConfig1 = new CoreQueueConfiguration().setAddress(forwardAddress).setName(queueName1);
        List<CoreQueueConfiguration> queueConfigs1 = new ArrayList<>();
        queueConfigs1.add(queueConfig1);
        server1.getConfiguration().setQueueConfigurations(queueConfigs1);
        server1.start();
        server0.start();
        locator = ActiveMQClient.createServerLocatorWithoutHA(server0tc, server1tc);
        ClientSessionFactory sf0 = locator.createSessionFactory(server0tc);
        ClientSessionFactory sf1 = locator.createSessionFactory(server1tc);
        ClientSession session0 = sf0.createSession(false, true, true);
        ClientSession session1 = sf1.createSession(false, true, true);
        ClientProducer producer0 = session0.createProducer(new SimpleString(testAddress));
        ClientConsumer consumer1 = session1.createConsumer(queueName1);
        session1.start();
        final int numMessages = 10;
        final SimpleString propKey = new SimpleString("testkey");
        for (int i = 0; i < numMessages; i++) {
            ClientMessage message = session0.createMessage(false);
            message.putIntProperty(propKey, i);
            producer0.send(message);
        }
        for (int i = 0; i < numMessages; i++) {
            ClientMessage message = consumer1.receive(BridgeWithDiscoveryGroupStartTest.TIMEOUT);
            Assert.assertNotNull(message);
            Assert.assertEquals(i, message.getObjectProperty(propKey));
            message.acknowledge();
        }
        Assert.assertNull(consumer1.receiveImmediate());
        Bridge bridge = server0.getClusterManager().getBridges().get(bridgeName);
        bridge.stop();
        bridge.flushExecutor();
        for (int i = 0; i < numMessages; i++) {
            ClientMessage message = session0.createMessage(false);
            message.putIntProperty(propKey, i);
            producer0.send(message);
        }
        Assert.assertNull(consumer1.receiveImmediate());
        bridge.start();
        for (int i = 0; i < numMessages; i++) {
            ClientMessage message = consumer1.receive(BridgeWithDiscoveryGroupStartTest.TIMEOUT);
            Assert.assertNotNull(message);
            Assert.assertEquals(i, message.getObjectProperty(propKey));
            message.acknowledge();
        }
        Assert.assertNull(consumer1.receiveImmediate());
        session0.close();
        session1.close();
        sf0.close();
        sf1.close();
    } finally {
        if (locator != null) {
            locator.close();
        }
        server0.stop();
        server1.stop();
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) CoreQueueConfiguration(org.apache.activemq.artemis.core.config.CoreQueueConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) UDPBroadcastEndpointFactory(org.apache.activemq.artemis.api.core.UDPBroadcastEndpointFactory) BroadcastGroupConfiguration(org.apache.activemq.artemis.api.core.BroadcastGroupConfiguration) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) BridgeConfiguration(org.apache.activemq.artemis.core.config.BridgeConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) DiscoveryGroupConfiguration(org.apache.activemq.artemis.api.core.DiscoveryGroupConfiguration) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) Bridge(org.apache.activemq.artemis.core.server.cluster.Bridge) Test(org.junit.Test)

Example 7 with CoreQueueConfiguration

use of org.apache.activemq.artemis.core.config.CoreQueueConfiguration in project activemq-artemis by apache.

the class BridgeReconnectTest method testFailoverDeploysBridge.

/**
 * Backups must successfully deploy its bridges on fail-over.
 *
 * @see https://bugzilla.redhat.com/show_bug.cgi?id=900764
 */
@Test
public void testFailoverDeploysBridge() throws Exception {
    NodeManager nodeManager = new InVMNodeManager(false);
    server0 = createActiveMQServer(0, server0Params, isNetty(), nodeManager);
    server2 = createBackupActiveMQServer(2, server2Params, isNetty(), 0, nodeManager);
    TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params, "server0tc");
    TransportConfiguration server2tc = new TransportConfiguration(getConnector(), server2Params, "server2tc");
    connectors.put(server2tc.getName(), server2tc);
    server0.getConfiguration().setConnectorConfigurations(connectors);
    server1.getConfiguration().setConnectorConfigurations(connectors);
    server2.getConfiguration().setConnectorConfigurations(connectors);
    reconnectAttempts = -1;
    BridgeConfiguration bridgeConfiguration = createBridgeConfig();
    bridgeConfiguration.setQueueName(queueName);
    List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
    bridgeConfigs.add(bridgeConfiguration);
    server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);
    server2.getConfiguration().setBridgeConfigurations(bridgeConfigs);
    CoreQueueConfiguration queueConfig0 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName);
    List<CoreQueueConfiguration> queueConfigs0 = new ArrayList<>();
    queueConfigs0.add(queueConfig0);
    server1.getConfiguration().setQueueConfigurations(queueConfigs0);
    CoreQueueConfiguration queueConfig1 = new CoreQueueConfiguration().setAddress(forwardAddress).setName(queueName);
    List<CoreQueueConfiguration> queueConfigs1 = new ArrayList<>();
    queueConfigs1.add(queueConfig1);
    server0.getConfiguration().setQueueConfigurations(queueConfigs1);
    server2.getConfiguration().setQueueConfigurations(queueConfigs1);
    startServers();
    waitForServerStart(server0);
    server0.fail(true);
    waitForServerStart(server2);
    locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(server0tc, server2tc));
    ClientSessionFactory csf0 = addSessionFactory(locator.createSessionFactory(server2tc));
    session0 = csf0.createSession(false, true, true);
    Map<String, Bridge> bridges = server2.getClusterManager().getBridges();
    assertTrue("backup must deploy bridge on failover", !bridges.isEmpty());
}
Also used : InVMNodeManager(org.apache.activemq.artemis.core.server.impl.InVMNodeManager) NodeManager(org.apache.activemq.artemis.core.server.NodeManager) InVMNodeManager(org.apache.activemq.artemis.core.server.impl.InVMNodeManager) BridgeConfiguration(org.apache.activemq.artemis.core.config.BridgeConfiguration) ArrayList(java.util.ArrayList) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) CoreQueueConfiguration(org.apache.activemq.artemis.core.config.CoreQueueConfiguration) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Bridge(org.apache.activemq.artemis.core.server.cluster.Bridge) Test(org.junit.Test)

Example 8 with CoreQueueConfiguration

use of org.apache.activemq.artemis.core.config.CoreQueueConfiguration in project activemq-artemis by apache.

the class BridgeReconnectTest method testDeliveringCountOnBridgeConnectionFailure.

@Test
public void testDeliveringCountOnBridgeConnectionFailure() throws Exception {
    server0 = createActiveMQServer(0, isNetty(), server0Params);
    TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params, "server0tc");
    server0.getConfiguration().setConnectorConfigurations(connectors);
    BridgeConfiguration bridgeConfiguration = createBridgeConfig();
    List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
    bridgeConfigs.add(bridgeConfiguration);
    server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);
    CoreQueueConfiguration queueConfig0 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName);
    List<CoreQueueConfiguration> queueConfigs0 = new ArrayList<>();
    queueConfigs0.add(queueConfig0);
    server0.getConfiguration().setQueueConfigurations(queueConfigs0);
    CoreQueueConfiguration queueConfig1 = new CoreQueueConfiguration().setAddress(forwardAddress).setName(queueName);
    List<CoreQueueConfiguration> queueConfigs1 = new ArrayList<>();
    queueConfigs1.add(queueConfig1);
    server1.getConfiguration().setQueueConfigurations(queueConfigs1);
    startServers();
    locator = addServerLocator(ActiveMQClient.createServerLocatorWithHA(server0tc, server1tc));
    ClientSessionFactory csf0 = locator.createSessionFactory(server0tc);
    session0 = csf0.createSession(false, true, true);
    ClientSessionFactory csf1 = locator.createSessionFactory(server1tc);
    session1 = csf1.createSession(false, true, true);
    ClientProducer prod0 = session0.createProducer(testAddress);
    session1.start();
    Bridge bridge = server0.getClusterManager().getBridges().get(bridgeName);
    RemotingConnection forwardingConnection = getForwardingConnection(bridge);
    InVMConnector.failOnCreateConnection = true;
    InVMConnector.numberOfFailures = reconnectAttempts - 1;
    // forwardingConnection.fail(new ActiveMQNotConnectedException());
    final int numMessages = NUM_MESSAGES;
    SimpleString propKey = new SimpleString("propkey");
    final Queue queue = (Queue) server0.getPostOffice().getBinding(new SimpleString(queueName)).getBindable();
    System.out.println("DeliveringCount: " + queue.getDeliveringCount());
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session0.createMessage(false);
        message.putIntProperty(propKey, i);
        prod0.send(message);
        if (i == 50) {
            forwardingConnection.fail(new ActiveMQException(ActiveMQExceptionType.UNBLOCKED));
        }
    }
    for (int i = 0; i < 100 && queue.getDeliveringCount() != 0; i++) {
        Thread.sleep(10);
    }
    System.out.println("Check.. DeliveringCount: " + queue.getDeliveringCount());
    assertEquals("Delivering count of a source queue should be zero on connection failure", 0, queue.getDeliveringCount());
    closeServers();
    assertNoMoreConnections();
}
Also used : BridgeConfiguration(org.apache.activemq.artemis.core.config.BridgeConfiguration) ArrayList(java.util.ArrayList) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) CoreQueueConfiguration(org.apache.activemq.artemis.core.config.CoreQueueConfiguration) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Queue(org.apache.activemq.artemis.core.server.Queue) Bridge(org.apache.activemq.artemis.core.server.cluster.Bridge) Test(org.junit.Test)

Example 9 with CoreQueueConfiguration

use of org.apache.activemq.artemis.core.config.CoreQueueConfiguration in project activemq-artemis by apache.

the class BridgeTest method testBridgeWithLargeMessage.

@Test
public void testBridgeWithLargeMessage() throws Exception {
    ActiveMQServer server0 = null;
    ActiveMQServer server1 = null;
    final int PAGE_MAX = 1024 * 1024;
    final int PAGE_SIZE = 10 * 1024;
    ServerLocator locator = null;
    try {
        Map<String, Object> server0Params = new HashMap<>();
        server0 = createClusteredServerWithParams(isNetty(), 0, true, PAGE_SIZE, PAGE_MAX, server0Params);
        Map<String, Object> server1Params = new HashMap<>();
        addTargetParameters(server1Params);
        server1 = createClusteredServerWithParams(isNetty(), 1, true, server1Params);
        final String testAddress = "testAddress";
        final String queueName0 = "queue0";
        final String forwardAddress = "forwardAddress";
        final String queueName1 = "queue1";
        Map<String, TransportConfiguration> connectors = new HashMap<>();
        TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params);
        TransportConfiguration server1tc = new TransportConfiguration(getConnector(), server1Params);
        connectors.put(server1tc.getName(), server1tc);
        server0.getConfiguration().setConnectorConfigurations(connectors);
        ArrayList<String> staticConnectors = new ArrayList<>();
        staticConnectors.add(server1tc.getName());
        BridgeConfiguration bridgeConfiguration = new BridgeConfiguration().setName("bridge1").setQueueName(queueName0).setForwardingAddress(forwardAddress).setRetryInterval(1000).setReconnectAttemptsOnSameNode(-1).setUseDuplicateDetection(false).setConfirmationWindowSize(1024).setStaticConnectors(staticConnectors);
        List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
        bridgeConfigs.add(bridgeConfiguration);
        server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);
        CoreQueueConfiguration queueConfig0 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName0);
        List<CoreQueueConfiguration> queueConfigs0 = new ArrayList<>();
        queueConfigs0.add(queueConfig0);
        server0.getConfiguration().setQueueConfigurations(queueConfigs0);
        CoreQueueConfiguration queueConfig1 = new CoreQueueConfiguration().setAddress(forwardAddress).setName(queueName1);
        List<CoreQueueConfiguration> queueConfigs1 = new ArrayList<>();
        queueConfigs1.add(queueConfig1);
        server1.getConfiguration().setQueueConfigurations(queueConfigs1);
        server1.start();
        server0.start();
        locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(server0tc, server1tc));
        ClientSessionFactory sf0 = locator.createSessionFactory(server0tc);
        ClientSessionFactory sf1 = locator.createSessionFactory(server1tc);
        ClientSession session0 = sf0.createSession(false, true, true);
        ClientSession session1 = sf1.createSession(false, true, true);
        ClientProducer producer0 = session0.createProducer(new SimpleString(testAddress));
        ClientConsumer consumer1 = session1.createConsumer(queueName1);
        session1.start();
        final int numMessages = 50;
        final SimpleString propKey = new SimpleString("testkey");
        final int LARGE_MESSAGE_SIZE = 1024;
        for (int i = 0; i < numMessages; i++) {
            ClientMessage message = session0.createMessage(true);
            message.setBodyInputStream(createFakeLargeStream(LARGE_MESSAGE_SIZE));
            message.putIntProperty(propKey, i);
            producer0.send(message);
        }
        session0.commit();
        for (int i = 0; i < numMessages; i++) {
            ClientMessage message = consumer1.receive(5000);
            Assert.assertNotNull(message);
            Assert.assertEquals(i, message.getObjectProperty(propKey));
            ActiveMQBuffer buff = message.getBodyBuffer();
            for (int posMsg = 0; posMsg < LARGE_MESSAGE_SIZE; posMsg++) {
                assertEquals(getSamplebyte(posMsg), buff.readByte());
            }
            message.acknowledge();
        }
        session1.commit();
        Assert.assertNull(consumer1.receiveImmediate());
        session0.close();
        session1.close();
        sf0.close();
        sf1.close();
    } finally {
        if (locator != null) {
            locator.close();
        }
        try {
            server0.stop();
        } catch (Throwable ignored) {
        }
        try {
            server1.stop();
        } catch (Throwable ignored) {
        }
    }
    assertEquals(0, loadQueues(server0).size());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BridgeConfiguration(org.apache.activemq.artemis.core.config.BridgeConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) CoreQueueConfiguration(org.apache.activemq.artemis.core.config.CoreQueueConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 10 with CoreQueueConfiguration

use of org.apache.activemq.artemis.core.config.CoreQueueConfiguration in project activemq-artemis by apache.

the class BridgeTest method testSawtoothLoad.

@Test
public void testSawtoothLoad() throws Exception {
    Map<String, Object> server0Params = new HashMap<>();
    ActiveMQServer server0 = createClusteredServerWithParams(isNetty(), 0, true, server0Params);
    server0.getConfiguration().setThreadPoolMaxSize(10);
    Map<String, Object> server1Params = new HashMap<>();
    addTargetParameters(server1Params);
    ActiveMQServer server1 = createClusteredServerWithParams(isNetty(), 1, true, server1Params);
    server1.getConfiguration().setThreadPoolMaxSize(10);
    final String testAddress = "testAddress";
    final String queueName0 = "queue0";
    final String forwardAddress = "forwardAddress";
    final String queueName1 = "queue1";
    Map<String, TransportConfiguration> connectors = new HashMap<>();
    final TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params);
    final TransportConfiguration server1tc = new TransportConfiguration(getConnector(), server1Params);
    connectors.put(server1tc.getName(), server1tc);
    server0.getConfiguration().setConnectorConfigurations(connectors);
    ArrayList<String> staticConnectors = new ArrayList<>();
    staticConnectors.add(server1tc.getName());
    BridgeConfiguration bridgeConfiguration = new BridgeConfiguration().setName("bridge1").setQueueName(queueName0).setForwardingAddress(forwardAddress).setRetryInterval(1000).setReconnectAttemptsOnSameNode(-1).setUseDuplicateDetection(false).setConfirmationWindowSize(0).setStaticConnectors(staticConnectors).setProducerWindowSize(1);
    List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
    bridgeConfigs.add(bridgeConfiguration);
    server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);
    CoreQueueConfiguration queueConfig0 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName0);
    List<CoreQueueConfiguration> queueConfigs0 = new ArrayList<>();
    queueConfigs0.add(queueConfig0);
    server0.getConfiguration().setQueueConfigurations(queueConfigs0);
    CoreQueueConfiguration queueConfig1 = new CoreQueueConfiguration().setAddress(forwardAddress).setName(queueName1);
    List<CoreQueueConfiguration> queueConfigs1 = new ArrayList<>();
    queueConfigs1.add(queueConfig1);
    server1.getConfiguration().setQueueConfigurations(queueConfigs1);
    try {
        server1.start();
        server0.start();
        final int numMessages = 300;
        final int totalrepeats = 3;
        final AtomicInteger errors = new AtomicInteger(0);
        // We shouldn't have more than 10K messages pending
        final Semaphore semop = new Semaphore(10000);
        class ConsumerThread extends Thread {

            @Override
            public void run() {
                try {
                    ServerLocator locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(server1tc));
                    ClientSessionFactory sf = createSessionFactory(locator);
                    ClientSession session = sf.createSession(false, false);
                    session.start();
                    ClientConsumer consumer = session.createConsumer(queueName1);
                    for (int i = 0; i < numMessages; i++) {
                        ClientMessage message = consumer.receive(5000);
                        Assert.assertNotNull(message);
                        message.acknowledge();
                        semop.release();
                        if (i % 1000 == 0) {
                            session.commit();
                        }
                    }
                    session.commit();
                    session.close();
                    sf.close();
                    locator.close();
                } catch (Throwable e) {
                    e.printStackTrace();
                    errors.incrementAndGet();
                }
            }
        }
        class ProducerThread extends Thread {

            final int nmsg;

            ProducerThread(int nmsg) {
                this.nmsg = nmsg;
            }

            @Override
            public void run() {
                ServerLocator locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(server0tc));
                locator.setBlockOnDurableSend(false).setBlockOnNonDurableSend(false);
                ClientSessionFactory sf = null;
                ClientSession session = null;
                ClientProducer producer = null;
                try {
                    sf = createSessionFactory(locator);
                    session = sf.createSession(false, true, true);
                    producer = session.createProducer(new SimpleString(testAddress));
                    for (int i = 0; i < nmsg; i++) {
                        assertEquals(0, errors.get());
                        ClientMessage message = session.createMessage(true);
                        message.putIntProperty("seq", i);
                        if (i % 100 == 0) {
                            message.setPriority((byte) (RandomUtil.randomPositiveInt() % 9));
                        } else {
                            message.setPriority((byte) 5);
                        }
                        message.getBodyBuffer().writeBytes(new byte[50]);
                        producer.send(message);
                        assertTrue(semop.tryAcquire(1, 10, TimeUnit.SECONDS));
                    }
                } catch (Throwable e) {
                    e.printStackTrace(System.out);
                    errors.incrementAndGet();
                } finally {
                    try {
                        session.close();
                        sf.close();
                        locator.close();
                    } catch (Exception ignored) {
                        errors.incrementAndGet();
                    }
                }
            }
        }
        for (int repeat = 0; repeat < totalrepeats; repeat++) {
            ArrayList<Thread> threads = new ArrayList<>();
            threads.add(new ConsumerThread());
            threads.add(new ProducerThread(numMessages / 2));
            threads.add(new ProducerThread(numMessages / 2));
            for (Thread t : threads) {
                t.start();
            }
            for (Thread t : threads) {
                t.join();
            }
            assertEquals(0, errors.get());
        }
    } finally {
        try {
            server0.stop();
        } catch (Exception ignored) {
        }
        try {
            server1.stop();
        } catch (Exception ignored) {
        }
    }
    assertEquals(0, loadQueues(server0).size());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) CoreQueueConfiguration(org.apache.activemq.artemis.core.config.CoreQueueConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Semaphore(java.util.concurrent.Semaphore) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) BridgeConfiguration(org.apache.activemq.artemis.core.config.BridgeConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) IOException(java.io.IOException) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) Test(org.junit.Test)

Aggregations

CoreQueueConfiguration (org.apache.activemq.artemis.core.config.CoreQueueConfiguration)63 ArrayList (java.util.ArrayList)40 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)39 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)39 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)33 BridgeConfiguration (org.apache.activemq.artemis.core.config.BridgeConfiguration)32 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)31 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)31 Test (org.junit.Test)31 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)30 HashMap (java.util.HashMap)26 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)26 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)22 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)16 Configuration (org.apache.activemq.artemis.core.config.Configuration)9 Before (org.junit.Before)9 CoreAddressConfiguration (org.apache.activemq.artemis.core.config.CoreAddressConfiguration)8 Bridge (org.apache.activemq.artemis.core.server.cluster.Bridge)8 InVMAcceptorFactory (org.apache.activemq.artemis.core.remoting.impl.invm.InVMAcceptorFactory)5 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)4