Search in sources :

Example 86 with ClientConsumer

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

the class CommitRollbackTest method testReceiveWithRollbackMultipleConsumersDifferentQueues.

@Test
public void testReceiveWithRollbackMultipleConsumersDifferentQueues() throws Exception {
    ActiveMQServer server = createServer(false);
    server.start();
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory cf = createSessionFactory(locator);
    ClientSession sendSession = cf.createSession(false, true, true);
    ClientSession session = cf.createSession(false, false, false);
    sendSession.createQueue(addressA, queueA, false);
    sendSession.createQueue(addressB, queueB, false);
    ClientProducer cp = sendSession.createProducer(addressA);
    ClientProducer cp2 = sendSession.createProducer(addressB);
    ClientConsumer cc = session.createConsumer(queueA);
    ClientConsumer cc2 = session.createConsumer(queueB);
    int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        cp.send(sendSession.createMessage(false));
        cp2.send(sendSession.createMessage(false));
    }
    session.start();
    for (int i = 0; i < numMessages; i++) {
        ClientMessage cm = cc.receive(5000);
        Assert.assertNotNull(cm);
        cm.acknowledge();
        cm = cc2.receive(5000);
        Assert.assertNotNull(cm);
        cm.acknowledge();
    }
    Queue q = (Queue) server.getPostOffice().getBinding(queueA).getBindable();
    Queue q2 = (Queue) server.getPostOffice().getBinding(queueB).getBindable();
    Assert.assertEquals(numMessages, q.getDeliveringCount());
    cc.close();
    cc2.close();
    session.rollback();
    Assert.assertEquals(0, q2.getDeliveringCount());
    Assert.assertEquals(numMessages, getMessageCount(q));
    Assert.assertEquals(0, q2.getDeliveringCount());
    Assert.assertEquals(numMessages, getMessageCount(q));
    sendSession.close();
    session.close();
}
Also used : 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) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Queue(org.apache.activemq.artemis.core.server.Queue) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 87 with ClientConsumer

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

the class BlockingSendTest method testSinglePersistentBlockingNonSync.

// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testSinglePersistentBlockingNonSync() throws Exception {
    ActiveMQServer server = createServer(true);
    ClientSession session = null;
    ClientSessionFactory factory = null;
    ServerLocator locator = null;
    server.getConfiguration().setJournalSyncNonTransactional(false);
    server.getConfiguration().setJournalBufferTimeout_AIO(15000);
    server.start();
    System.out.println("sync = " + server.getConfiguration().isJournalSyncNonTransactional());
    locator = createInVMNonHALocator().setBlockOnDurableSend(true);
    factory = createSessionFactory(locator);
    session = factory.createSession();
    session.createQueue("address", RoutingType.ANYCAST, "queue");
    ClientProducer prod = session.createProducer("address");
    ClientMessage message = session.createMessage(true);
    prod.send(message);
    ClientConsumer consumer = session.createConsumer("queue");
    session.start();
    ClientMessage msg = consumer.receive(5000);
    Assert.assertNotNull(msg);
    msg.acknowledge();
}
Also used : 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) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) 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) Test(org.junit.Test)

Example 88 with ClientConsumer

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

the class ConsumerFilterTest method testTwoConsumers.

@Test
public void testTwoConsumers() throws Exception {
    ClientConsumer consumer2 = session.createConsumer("foo", "animal='elephant'");
    // Create and consume message that matches the first consumer's filter
    ClientMessage message = session.createMessage(false);
    message.putStringProperty("animal", "giraffe");
    producer.send(message);
    ClientMessage received = consumer.receive(10000);
    assertNotNull(received);
    assertEquals("giraffe", received.getStringProperty("animal"));
    assertNull(consumer.receiveImmediate());
    assertNull(consumer2.receiveImmediate());
    // Create and consume another message that matches the first consumer's filter
    message = session.createMessage(false);
    message.putStringProperty("animal", "giraffe");
    producer.send(message);
    received = consumer.receive(10000);
    assertNotNull(received);
    assertEquals("giraffe", received.getStringProperty("animal"));
    assertNull(consumer.receiveImmediate());
    assertNull(consumer2.receiveImmediate());
    // Create and consume a message that matches the second consumer's filter
    message = session.createMessage(false);
    message.putStringProperty("animal", "elephant");
    producer.send(message);
    received = consumer2.receive(10000);
    assertNotNull(received);
    assertEquals("elephant", received.getStringProperty("animal"));
    assertNull(consumer.receiveImmediate());
    assertNull(consumer2.receiveImmediate());
    // Create and consume another message that matches the second consumer's filter
    message = session.createMessage(false);
    message.putStringProperty("animal", "elephant");
    producer.send(message);
    received = consumer2.receive(1000);
    assertNotNull(received);
    assertEquals("elephant", received.getStringProperty("animal"));
    assertNull(consumer.receiveImmediate());
    assertNull(consumer2.receiveImmediate());
    session.close();
}
Also used : ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) Test(org.junit.Test)

Example 89 with ClientConsumer

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

the class ConsumerStuckTest method testClientStuckTestWithDirectDelivery.

@Test
public void testClientStuckTestWithDirectDelivery() throws Exception {
    ServerLocator locator = createNettyNonHALocator().setConnectionTTL(1000).setClientFailureCheckPeriod(100).setConsumerWindowSize(10 * 1024 * 1024).setCallTimeout(1000);
    ClientSessionFactory sf = locator.createSessionFactory();
    ((ClientSessionFactoryImpl) sf).stopPingingAfterOne();
    RemotingConnectionImpl remotingConnection = (RemotingConnectionImpl) sf.getConnection();
    ClientSession session = sf.createSession(false, true, true, true);
    session.createQueue(QUEUE, QUEUE, null, false);
    final int numMessages = 10000;
    final ClientConsumer consumer = session.createConsumer(QUEUE);
    session.start();
    final NettyConnection nettyConnection = (NettyConnection) remotingConnection.getTransportConnection();
    Thread tReceive = new Thread() {

        @Override
        public void run() {
            boolean first = true;
            try {
                while (!Thread.interrupted()) {
                    ClientMessage received = consumer.receive(500);
                    System.out.println("Received " + received);
                    if (first) {
                        first = false;
                        nettyConnection.getNettyChannel().config().setAutoRead(false);
                    }
                    if (received != null) {
                        received.acknowledge();
                    }
                }
            } catch (Throwable e) {
                Thread.currentThread().interrupt();
                e.printStackTrace();
            }
        }
    };
    tReceive.start();
    Thread sender = new Thread() {

        @Override
        public void run() {
            try (ServerLocator locator = createNettyNonHALocator();
                ClientSessionFactory factory = locator.createSessionFactory();
                ClientSession session = factory.createSession(false, true, true, true);
                ClientProducer producer = session.createProducer(QUEUE)) {
                for (int i = 0; i < numMessages; i++) {
                    ClientMessage message = createTextMessage(session, "m" + i);
                    producer.send(message);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    sender.start();
    try {
        long timeout = System.currentTimeMillis() + 20000;
        while (System.currentTimeMillis() < timeout && server.getSessions().size() != 2) {
            Thread.sleep(10);
        }
        assertEquals(2, server.getSessions().size());
        System.out.println("sessions = " + server.getSessions().size());
        assertEquals(2, server.getConnectionCount());
        timeout = System.currentTimeMillis() + 20000;
        while (System.currentTimeMillis() < timeout && server.getSessions().size() != 1) {
            Thread.sleep(10);
        }
        System.out.println("Size = " + server.getConnectionCount());
        System.out.println("sessions = " + server.getSessions().size());
        if (server.getSessions().size() != 1) {
            System.out.println(threadDump("Thread dump"));
            fail("The cleanup wasn't able to finish cleaning the session. It's probably stuck, look at the thread dump generated by the test for more information");
        }
        sender.join();
        timeout = System.currentTimeMillis() + 20000;
        while (System.currentTimeMillis() < timeout && server.getConnectionCount() != 0) {
            Thread.sleep(10);
        }
        assertEquals(0, server.getConnectionCount());
    } finally {
        nettyConnection.getNettyChannel().config().setAutoRead(true);
        tReceive.interrupt();
        tReceive.join();
    }
}
Also used : NettyConnection(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection) RemotingConnectionImpl(org.apache.activemq.artemis.core.protocol.core.impl.RemotingConnectionImpl) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientSessionFactoryImpl(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl) 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) Test(org.junit.Test)

Example 90 with ClientConsumer

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

the class ActiveMQMessageHandlerTest method testSimpleMessageReceivedOnQueue.

@Test
@BMRules(rules = { @BMRule(name = "interrupt", targetClass = "org.apache.activemq.artemis.core.protocol.core.impl.ActiveMQSessionContext", targetMethod = "xaEnd", targetLocation = "ENTRY", action = "org.apache.activemq.artemis.tests.extras.byteman.ActiveMQMessageHandlerTest.interrupt();") })
public void testSimpleMessageReceivedOnQueue() throws Exception {
    ActiveMQResourceAdapter qResourceAdapter = newResourceAdapter();
    resourceAdapter = qResourceAdapter;
    MyBootstrapContext ctx = new MyBootstrapContext();
    qResourceAdapter.setConnectorClassName(NETTY_CONNECTOR_FACTORY);
    qResourceAdapter.start(ctx);
    ActiveMQActivationSpec spec = new ActiveMQActivationSpec();
    spec.setMaxSession(1);
    spec.setCallTimeout(1000L);
    spec.setResourceAdapter(qResourceAdapter);
    spec.setUseJNDI(false);
    spec.setDestinationType("javax.jms.Queue");
    spec.setDestination(MDBQUEUE);
    CountDownLatch latch = new CountDownLatch(1);
    XADummyEndpoint endpoint = new XADummyEndpoint(latch, false);
    DummyMessageEndpointFactory endpointFactory = new DummyMessageEndpointFactory(endpoint, true);
    qResourceAdapter.endpointActivation(endpointFactory, spec);
    ClientSession session = locator.createSessionFactory().createSession();
    ClientProducer clientProducer = session.createProducer(MDBQUEUEPREFIXED);
    ClientMessage message = session.createMessage(true);
    message.getBodyBuffer().writeString("teststring");
    clientProducer.send(message);
    session.close();
    latch.await(5, TimeUnit.SECONDS);
    assertNotNull(endpoint.lastMessage);
    assertEquals(endpoint.lastMessage.getCoreMessage().getBodyBuffer().readString(), "teststring");
    qResourceAdapter.endpointDeactivation(endpointFactory, spec);
    qResourceAdapter.stop();
    Binding binding = server.getPostOffice().getBinding(SimpleString.toSimpleString(MDBQUEUEPREFIXED));
    assertEquals(1, getMessageCount(((Queue) binding.getBindable())));
    server.stop();
    server.start();
    ClientSessionFactory factory = locator.createSessionFactory();
    session = factory.createSession(true, true);
    session.start();
    ClientConsumer consumer = session.createConsumer(MDBQUEUEPREFIXED);
    assertNotNull(consumer.receive(5000));
    session.close();
}
Also used : Binding(org.apache.activemq.artemis.core.postoffice.Binding) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQResourceAdapter(org.apache.activemq.artemis.ra.ActiveMQResourceAdapter) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ActiveMQActivationSpec(org.apache.activemq.artemis.ra.inflow.ActiveMQActivationSpec) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Aggregations

ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)720 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)642 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)624 Test (org.junit.Test)584 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)569 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)409 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)348 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)154 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)132 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)88 Configuration (org.apache.activemq.artemis.core.config.Configuration)81 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)79 CountDownLatch (java.util.concurrent.CountDownLatch)72 Queue (org.apache.activemq.artemis.core.server.Queue)63 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)62 Xid (javax.transaction.xa.Xid)55 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)52 HashMap (java.util.HashMap)48 DatabaseStorageConfiguration (org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration)46 ArrayList (java.util.ArrayList)44