Search in sources :

Example 66 with ActiveMQException

use of org.apache.activemq.artemis.api.core.ActiveMQException in project activemq-artemis by apache.

the class JMSUtil method crash.

public static void crash(ActiveMQServer server, ClientSession... sessions) throws Exception {
    final CountDownLatch latch = new CountDownLatch(sessions.length);
    class MyListener implements SessionFailureListener {

        @Override
        public void connectionFailed(final ActiveMQException me, boolean failedOver) {
            latch.countDown();
        }

        @Override
        public void connectionFailed(final ActiveMQException me, boolean failedOver, String scaleDownTargetNodeID) {
            connectionFailed(me, failedOver);
        }

        @Override
        public void beforeReconnect(ActiveMQException exception) {
            System.out.println("MyListener.beforeReconnect");
        }
    }
    for (ClientSession session : sessions) {
        session.addFailureListener(new MyListener());
    }
    ClusterManager clusterManager = server.getClusterManager();
    clusterManager.clear();
    server.fail(true);
    // Wait to be informed of failure
    boolean ok = latch.await(10000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(ok);
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) CountDownLatch(java.util.concurrent.CountDownLatch) SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) ClusterManager(org.apache.activemq.artemis.core.server.cluster.ClusterManager)

Example 67 with ActiveMQException

use of org.apache.activemq.artemis.api.core.ActiveMQException in project activemq-artemis by apache.

the class ClusteredBridgeReconnectTest method testReconnectBridge.

@Test
public void testReconnectBridge() throws Exception {
    setupServer(0, isFileStorage(), isNetty());
    setupServer(1, isFileStorage(), isNetty());
    setupClusterConnection("cluster0", "queues", MessageLoadBalancingType.ON_DEMAND, 1, isNetty(), 0, 1);
    setupClusterConnection("cluster1", "queues", MessageLoadBalancingType.ON_DEMAND, 1, isNetty(), 1, 0);
    startServers(0, 1);
    setupSessionFactory(0, isNetty());
    setupSessionFactory(1, isNetty());
    createQueue(0, "queues.testaddress", "queue0", null, true);
    createQueue(1, "queues.testaddress", "queue0", null, true);
    addConsumer(0, 0, "queue0", null);
    addConsumer(1, 1, "queue0", null);
    waitForBindings(0, "queues.testaddress", 1, 1, true);
    waitForBindings(1, "queues.testaddress", 1, 1, true);
    waitForBindings(0, "queues.testaddress", 1, 1, false);
    waitForBindings(1, "queues.testaddress", 1, 1, false);
    ClientSession session0 = sfs[0].createSession();
    ClientSession session1 = sfs[0].createSession();
    session0.start();
    session1.start();
    ClientProducer producer = session0.createProducer("queues.testaddress");
    int NUMBER_OF_MESSAGES = 100;
    Assert.assertEquals(1, servers[0].getClusterManager().getClusterConnections().size());
    ClusterConnectionImpl connection = servers[0].getClusterManager().getClusterConnections().toArray(new ClusterConnectionImpl[0])[0];
    Assert.assertEquals(1, connection.getRecords().size());
    MessageFlowRecord record = connection.getRecords().values().toArray(new MessageFlowRecord[1])[0];
    ClusterConnectionBridge bridge = (ClusterConnectionBridge) record.getBridge();
    for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
        ClientMessage msg = session0.createMessage(true);
        producer.send(msg);
        session0.commit();
        if (i == 17) {
            bridge.getSessionFactory().getConnection().fail(new ActiveMQException("failed once!"));
        }
    }
    int cons0Count = 0, cons1Count = 0;
    while (true) {
        ClientMessage msg = consumers[0].getConsumer().receive(1000);
        if (msg == null) {
            break;
        }
        cons0Count++;
        msg.acknowledge();
        session0.commit();
    }
    while (true) {
        ClientMessage msg = consumers[1].getConsumer().receive(1000);
        if (msg == null) {
            break;
        }
        cons1Count++;
        msg.acknowledge();
        session1.commit();
    }
    Assert.assertEquals("cons0 = " + cons0Count + ", cons1 = " + cons1Count, NUMBER_OF_MESSAGES, cons0Count + cons1Count);
    session0.commit();
    session1.commit();
    connection = servers[0].getClusterManager().getClusterConnections().toArray(new ClusterConnectionImpl[0])[0];
    Assert.assertEquals(1, connection.getRecords().size());
    Assert.assertNotNull(bridge.getSessionFactory());
    stopServers(0, 1);
}
Also used : MessageFlowRecord(org.apache.activemq.artemis.core.server.cluster.MessageFlowRecord) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClusterConnectionBridge(org.apache.activemq.artemis.core.server.cluster.impl.ClusterConnectionBridge) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ClusterConnectionImpl(org.apache.activemq.artemis.core.server.cluster.impl.ClusterConnectionImpl) Test(org.junit.Test)

Example 68 with ActiveMQException

use of org.apache.activemq.artemis.api.core.ActiveMQException in project activemq-artemis by apache.

the class SessionClosedOnRemotingConnectionFailureTest method testSessionClosedOnRemotingConnectionFailure.

@Test
public void testSessionClosedOnRemotingConnectionFailure() throws Exception {
    ClientSession session = addClientSession(sf.createSession());
    session.createQueue("fooaddress", RoutingType.ANYCAST, "fooqueue");
    ClientProducer prod = session.createProducer("fooaddress");
    ClientConsumer cons = session.createConsumer("fooqueue");
    session.start();
    prod.send(session.createMessage(false));
    Assert.assertNotNull(cons.receive());
    // Now fail the underlying connection
    RemotingConnection connection = ((ClientSessionInternal) session).getConnection();
    connection.fail(new ActiveMQNotConnectedException());
    Assert.assertTrue(session.isClosed());
    Assert.assertTrue(prod.isClosed());
    Assert.assertTrue(cons.isClosed());
    try {
        prod.send(session.createMessage(false));
        Assert.fail("Should throw exception");
    } catch (ActiveMQObjectClosedException oce) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
    try {
        cons.receive();
        Assert.fail("Should throw exception");
    } catch (ActiveMQObjectClosedException oce) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
    session.close();
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQObjectClosedException(org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 69 with ActiveMQException

use of org.apache.activemq.artemis.api.core.ActiveMQException in project activemq-artemis by apache.

the class SessionTest method testFailureListenerRemoved.

@Test
public void testFailureListenerRemoved() throws Exception {
    cf = createSessionFactory(locator);
    try {
        ClientSession clientSession = cf.createSession(false, true, true);
        class MyFailureListener implements SessionFailureListener {

            boolean called = false;

            @Override
            public void connectionFailed(final ActiveMQException me, boolean failedOver) {
                called = true;
            }

            @Override
            public void connectionFailed(final ActiveMQException me, boolean failedOver, String scaleDownTargetNodeID) {
                connectionFailed(me, failedOver);
            }

            @Override
            public void beforeReconnect(final ActiveMQException me) {
            }
        }
        MyFailureListener listener = new MyFailureListener();
        clientSession.addFailureListener(listener);
        Assert.assertTrue(clientSession.removeFailureListener(listener));
        clientSession.close();
        server.stop();
        Assert.assertFalse(listener.called);
    } finally {
        ((ClientSessionFactoryInternal) cf).causeExit();
        cf.close();
    }
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener) Test(org.junit.Test)

Example 70 with ActiveMQException

use of org.apache.activemq.artemis.api.core.ActiveMQException in project activemq-artemis by apache.

the class SlowConsumerTest method testSlowConsumerNotification.

@Test
public void testSlowConsumerNotification() throws Exception {
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = addClientSession(sf.createSession(false, true, true, false));
    AddressSettings addressSettings = new AddressSettings();
    addressSettings.setSlowConsumerCheckPeriod(2);
    addressSettings.setSlowConsumerThreshold(10);
    addressSettings.setSlowConsumerPolicy(SlowConsumerPolicy.NOTIFY);
    if (!isPaging) {
        addressSettings.setAddressFullMessagePolicy(AddressFullMessagePolicy.BLOCK);
        addressSettings.setMaxSizeBytes(-1);
    }
    server.getAddressSettingsRepository().removeMatch(QUEUE.toString());
    server.getAddressSettingsRepository().addMatch(QUEUE.toString(), addressSettings);
    assertPaging();
    ClientProducer producer = addClientProducer(session.createProducer(QUEUE));
    final int numMessages = 25;
    for (int i = 0; i < numMessages; i++) {
        producer.send(createTextMessage(session, "m" + i));
    }
    SimpleString notifQueue = RandomUtil.randomSimpleString();
    session.createQueue(ActiveMQDefaultConfiguration.getDefaultManagementNotificationAddress(), notifQueue, null, false);
    ClientConsumer notifConsumer = session.createConsumer(notifQueue.toString(), ManagementHelper.HDR_NOTIFICATION_TYPE + "='" + CoreNotificationType.CONSUMER_SLOW + "'");
    final CountDownLatch notifLatch = new CountDownLatch(1);
    notifConsumer.setMessageHandler(new MessageHandler() {

        @Override
        public void onMessage(ClientMessage message) {
            assertEquals(CoreNotificationType.CONSUMER_SLOW.toString(), message.getObjectProperty(ManagementHelper.HDR_NOTIFICATION_TYPE).toString());
            assertEquals(QUEUE.toString(), message.getObjectProperty(ManagementHelper.HDR_ADDRESS).toString());
            assertEquals(Integer.valueOf(1), message.getIntProperty(ManagementHelper.HDR_CONSUMER_COUNT));
            if (isNetty) {
                assertTrue(message.getSimpleStringProperty(ManagementHelper.HDR_REMOTE_ADDRESS).toString().startsWith("/127.0.0.1"));
            } else {
                assertEquals(SimpleString.toSimpleString("invm:0"), message.getSimpleStringProperty(ManagementHelper.HDR_REMOTE_ADDRESS));
            }
            assertNotNull(message.getSimpleStringProperty(ManagementHelper.HDR_CONNECTION_NAME));
            assertNotNull(message.getSimpleStringProperty(ManagementHelper.HDR_CONSUMER_NAME));
            assertNotNull(message.getSimpleStringProperty(ManagementHelper.HDR_SESSION_NAME));
            try {
                message.acknowledge();
            } catch (ActiveMQException e) {
                e.printStackTrace();
            }
            notifLatch.countDown();
        }
    });
    ClientConsumer consumer = addClientConsumer(session.createConsumer(QUEUE));
    session.start();
    assertTrue(notifLatch.await(15, TimeUnit.SECONDS));
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) MessageHandler(org.apache.activemq.artemis.api.core.client.MessageHandler) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Aggregations

ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)254 Test (org.junit.Test)140 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)121 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)84 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)79 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)78 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)59 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)54 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)45 CountDownLatch (java.util.concurrent.CountDownLatch)40 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)36 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)31 HashSet (java.util.HashSet)29 ActiveMQJAASSecurityManager (org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager)27 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)23 Role (org.apache.activemq.artemis.core.security.Role)22 ActiveMQSecurityException (org.apache.activemq.artemis.api.core.ActiveMQSecurityException)20 Set (java.util.Set)16 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)16 Packet (org.apache.activemq.artemis.core.protocol.core.Packet)15