Search in sources :

Example 36 with ClientSessionFactory

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

the class CoreClientTest method testCoreClient.

private void testCoreClient(final boolean netty, ServerLocator serverLocator) throws Exception {
    final SimpleString QUEUE = new SimpleString("CoreClientTestQueue");
    ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(createDefaultConfig(netty), false));
    server.start();
    ServerLocator locator = serverLocator == null ? createNonHALocator(netty) : serverLocator;
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, true);
    session.createQueue(QUEUE, QUEUE, null, false);
    ClientProducer producer = session.createProducer(QUEUE);
    final int numMessages = 1000;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session.createMessage(ActiveMQTextMessage.TYPE, false, 0, System.currentTimeMillis(), (byte) 1);
        message.putStringProperty("foo", "bar");
        // One way around the setting destination problem is as follows -
        // Remove destination as an attribute from client producer.
        // The destination always has to be set explicitly before sending a message
        message.setAddress(QUEUE);
        message.getBodyBuffer().writeString("testINVMCoreClient");
        producer.send(message);
    }
    CoreClientTest.log.info("sent messages");
    ClientConsumer consumer = session.createConsumer(QUEUE);
    session.start();
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message2 = consumer.receive();
        ActiveMQBuffer buffer = message2.getBodyBuffer();
        Assert.assertEquals("testINVMCoreClient", buffer.readString());
        message2.acknowledge();
    }
    sf.close();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) 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) 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)

Example 37 with ClientSessionFactory

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

the class ExpireTestOnRestartTest method testRestartWithExpire.

// The biggest problem on this test was the exceptions that happened. I couldn't find any wrong state beyond the exceptions
@Test
public void testRestartWithExpire() throws Exception {
    int NUMBER_OF_EXPIRED_MESSAGES = 1000;
    ServerLocator locator = createInVMNonHALocator();
    locator.setBlockOnDurableSend(false);
    ClientSessionFactory factory = locator.createSessionFactory();
    ClientSession session = factory.createSession(true, true);
    session.createQueue("test", "test", true);
    session.createQueue("exp", "exp", true);
    ClientProducer prod = session.createProducer("test");
    for (int i = 0; i < 10; i++) {
        ClientMessage message = session.createMessage(true);
        message.getBodyBuffer().writeBytes(new byte[1024 * 10]);
        prod.send(message);
    }
    for (int i = 0; i < NUMBER_OF_EXPIRED_MESSAGES; i++) {
        ClientMessage message = session.createMessage(true);
        message.putIntProperty("i", i);
        message.getBodyBuffer().writeBytes(new byte[1024 * 10]);
        message.setExpiration(System.currentTimeMillis() + 5000);
        prod.send(message);
    }
    session.commit();
    session.close();
    server.stop();
    server.getConfiguration().setMessageExpiryScanPeriod(1);
    // enough time for expiration of the messages
    Thread.sleep(5500);
    server.start();
    Queue queue = server.locateQueue(SimpleString.toSimpleString("test"));
    factory = locator.createSessionFactory();
    session = factory.createSession(false, false);
    ClientConsumer cons = session.createConsumer("test");
    session.start();
    for (int i = 0; i < 10; i++) {
        ClientMessage msg = cons.receive(5000);
        assertNotNull(msg);
        msg.acknowledge();
    }
    assertNull(cons.receiveImmediate());
    cons.close();
    long timeout = System.currentTimeMillis() + 60000;
    while (queue.getPageSubscription().getPagingStore().isPaging() && timeout > System.currentTimeMillis()) {
        Thread.sleep(1);
    }
    assertFalse(queue.getPageSubscription().getPagingStore().isPaging());
    cons = session.createConsumer("exp");
    for (int i = 0; i < NUMBER_OF_EXPIRED_MESSAGES; i++) {
        ClientMessage msg = cons.receive(5000);
        assertNotNull(msg);
        msg.acknowledge();
    }
    session.commit();
    int extras = 0;
    ClientMessage msg;
    while ((msg = cons.receiveImmediate()) != null) {
        System.out.println(msg);
        extras++;
    }
    assertEquals("Received extra messages on expire address", 0, extras);
    session.commit();
    session.close();
    locator.close();
}
Also used : 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 38 with ClientSessionFactory

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

the class ExpiryLargeMessageTest method testExpiryMessagesThenDLQ.

// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testExpiryMessagesThenDLQ() throws Exception {
    ActiveMQServer server = createServer(true);
    server.getConfiguration().setMessageExpiryScanPeriod(600000);
    AddressSettings setting = new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE).setMaxDeliveryAttempts(5).setMaxSizeBytes(50 * 1024).setPageSizeBytes(10 * 1024).setExpiryAddress(EXPIRY).setDeadLetterAddress(DLQ);
    server.getAddressSettingsRepository().addMatch(MY_QUEUE.toString(), setting);
    server.getAddressSettingsRepository().addMatch(EXPIRY.toString(), setting);
    server.start();
    server.createQueue(EXPIRY, RoutingType.ANYCAST, EXPIRY, null, true, false);
    server.createQueue(DLQ, RoutingType.ANYCAST, DLQ, null, true, false);
    server.createQueue(MY_QUEUE, RoutingType.ANYCAST, MY_QUEUE, null, true, false);
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sf = addSessionFactory(createSessionFactory(locator));
    ClientSession session = sf.createSession(true, true, 0);
    byte[] bufferSample = new byte[messageSize];
    for (int i = 0; i < bufferSample.length; i++) {
        bufferSample[i] = getSamplebyte(i);
    }
    ClientProducer producer = session.createProducer(MY_QUEUE);
    long timeToExpiry = System.currentTimeMillis() + 1000;
    for (int i = 0; i < numberOfMessages; i++) {
        ClientMessage message = session.createMessage(true);
        message.putIntProperty("count", i);
        // Send a few regular messages first, then all is just large messages
        if (i % 2 == 0) {
            message.putBooleanProperty("tst-large", false);
            message.getBodyBuffer().writeBytes(bufferSample);
        } else {
            message.putBooleanProperty("tst-large", true);
            message.setBodyInputStream(createFakeLargeStream(messageSize));
        }
        message.setExpiration(timeToExpiry);
        producer.send(message);
    }
    session.close();
    server.stop();
    server.start();
    Queue queueExpiry = server.locateQueue(EXPIRY);
    Queue myQueue = server.locateQueue(MY_QUEUE);
    sf = createSessionFactory(locator);
    Thread.sleep(1500);
    long timeout = System.currentTimeMillis() + 5000;
    while (timeout > System.currentTimeMillis() && getMessageCount(queueExpiry) != numberOfMessages) {
        // What the Expiry Scan would be doing
        myQueue.expireReferences();
        Thread.sleep(50);
    }
    assertEquals(50, getMessageCount(queueExpiry));
    session = sf.createSession(false, false);
    ClientConsumer cons = session.createConsumer(EXPIRY);
    session.start();
    // Consume half of the messages to make sure all the messages are paging (on the second try)
    for (int i = 0; i < numberOfMessages / 2; i++) {
        ClientMessage msg = cons.receive(5000);
        assertNotNull(msg);
        msg.acknowledge();
    }
    session.commit();
    cons.close();
    for (int rep = 0; rep < 6; rep++) {
        cons = session.createConsumer(EXPIRY);
        session.start();
        log.info("Trying " + rep);
        for (int i = 0; i < numberOfMessages / 2; i++) {
            ClientMessage message = cons.receive(5000);
            assertNotNull(message);
            if (i % 10 == 0) {
                System.out.println("Received " + i);
            }
            for (int location = 0; location < messageSize; location++) {
                assertEquals(getSamplebyte(location), message.getBodyBuffer().readByte());
            }
            message.acknowledge();
        }
        session.rollback();
        cons.close();
        session.close();
        sf.close();
        if (rep == 0) {
            // restart the server at the first try
            server.stop();
            server.start();
        }
        sf = createSessionFactory(locator);
        session = sf.createSession(false, false);
        session.start();
    }
    cons = session.createConsumer(EXPIRY);
    session.start();
    assertNull(cons.receiveImmediate());
    cons.close();
    session.close();
    sf.close();
    for (int rep = 0; rep < 2; rep++) {
        sf = createSessionFactory(locator);
        session = sf.createSession(false, false);
        cons = session.createConsumer(DLQ);
        session.start();
        for (int i = 0; i < numberOfMessages / 2; i++) {
            ClientMessage message = cons.receive(5000);
            assertNotNull(message);
            if (i % 10 == 0) {
                System.out.println("Received " + i);
            }
            for (int location = 0; location < messageSize; location++) {
                assertEquals(getSamplebyte(location), message.getBodyBuffer().readByte());
            }
            message.acknowledge();
        }
        if (rep == 0) {
            session.rollback();
            session.close();
            sf.close();
            server.stop();
            server.start();
        }
    }
    session.commit();
    assertNull(cons.receiveImmediate());
    session.close();
    sf.close();
    locator.close();
    validateNoFilesOnLargeDir();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) 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 39 with ClientSessionFactory

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

the class FullQualifiedQueueTest method testMixedQueues.

@Test
public void testMixedQueues() throws Exception {
    server.createQueue(mixedAddress, RoutingType.MULTICAST, multicastQ1, null, true, false, -1, false, true);
    server.createQueue(mixedAddress, RoutingType.MULTICAST, multicastQ2, null, true, false, -1, false, true);
    server.createQueue(mixedAddress, RoutingType.MULTICAST, multicastQ3, null, true, false, -1, false, true);
    server.createQueue(mixedAddress, RoutingType.ANYCAST, anycastQ1, null, true, false, -1, false, true);
    server.createQueue(mixedAddress, RoutingType.ANYCAST, anycastQ2, null, true, false, -1, false, true);
    server.createQueue(mixedAddress, RoutingType.ANYCAST, anycastQ3, null, true, false, -1, false, true);
    ClientSessionFactory cf = createSessionFactory(locator);
    ClientSession session = cf.createSession();
    session.start();
    // send 3 messages
    ClientProducer producer = session.createProducer(mixedAddress);
    final int num = 3;
    sendMessages(session, producer, num);
    ClientConsumer consumer1 = session.createConsumer(toFullQN(mixedAddress, anycastQ1));
    ClientConsumer consumer2 = session.createConsumer(toFullQN(mixedAddress, anycastQ2));
    ClientConsumer consumer3 = session.createConsumer(toFullQN(mixedAddress, anycastQ3));
    ClientConsumer consumer4 = session.createConsumer(toFullQN(mixedAddress, multicastQ1));
    ClientConsumer consumer5 = session.createConsumer(toFullQN(mixedAddress, multicastQ2));
    ClientConsumer consumer6 = session.createConsumer(toFullQN(mixedAddress, multicastQ3));
    session.start();
    // each anycast consumer receives one, each multicast receives three.
    ClientMessage m = consumer1.receive(2000);
    assertNotNull(m);
    System.out.println("consumer1 : " + m);
    m.acknowledge();
    m = consumer2.receive(2000);
    assertNotNull(m);
    System.out.println("consumer2 : " + m);
    m.acknowledge();
    m = consumer3.receive(2000);
    assertNotNull(m);
    System.out.println("consumer3 : " + m);
    m.acknowledge();
    for (int i = 0; i < num; i++) {
        m = consumer4.receive(2000);
        assertNotNull(m);
        System.out.println("consumer4 : " + m);
        m.acknowledge();
        m = consumer5.receive(2000);
        assertNotNull(m);
        System.out.println("consumer5 : " + m);
        m.acknowledge();
        m = consumer6.receive(2000);
        assertNotNull(m);
        System.out.println("consumer6 : " + m);
        m.acknowledge();
    }
    session.commit();
    // queues are empty now
    for (SimpleString q : new SimpleString[] { anycastQ1, anycastQ2, anycastQ3, multicastQ1, multicastQ2, multicastQ3 }) {
        QueueQueryResult query = server.queueQuery(toFullQN(mixedAddress, q));
        assertTrue(query.isExists());
        assertEquals(mixedAddress, query.getAddress());
        assertEquals(toFullQN(mixedAddress, q), query.getName());
        assertEquals(0, query.getMessageCount());
    }
}
Also used : 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) QueueQueryResult(org.apache.activemq.artemis.core.server.QueueQueryResult) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 40 with ClientSessionFactory

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

the class FullQualifiedQueueTest method testAnycastQueues.

@Test
public void testAnycastQueues() throws Exception {
    server.createQueue(anycastAddress, RoutingType.ANYCAST, anycastQ1, null, true, false, -1, false, true);
    server.createQueue(anycastAddress, RoutingType.ANYCAST, anycastQ2, null, true, false, -1, false, true);
    server.createQueue(anycastAddress, RoutingType.ANYCAST, anycastQ3, null, true, false, -1, false, true);
    ClientSessionFactory cf = createSessionFactory(locator);
    ClientSession session = cf.createSession();
    session.start();
    // send 3 messages
    ClientProducer producer = session.createProducer(anycastAddress);
    sendMessages(session, producer, 3);
    ClientConsumer consumer1 = session.createConsumer(toFullQN(anycastAddress, anycastQ1));
    ClientConsumer consumer2 = session.createConsumer(toFullQN(anycastAddress, anycastQ2));
    ClientConsumer consumer3 = session.createConsumer(toFullQN(anycastAddress, anycastQ3));
    session.start();
    // each consumer receives one
    ClientMessage m = consumer1.receive(2000);
    assertNotNull(m);
    m.acknowledge();
    m = consumer2.receive(2000);
    assertNotNull(m);
    m.acknowledge();
    m = consumer3.receive(2000);
    assertNotNull(m);
    m.acknowledge();
    session.commit();
    // queues are empty now
    for (SimpleString q : new SimpleString[] { anycastQ1, anycastQ2, anycastQ3 }) {
        QueueQueryResult query = server.queueQuery(toFullQN(anycastAddress, q));
        assertTrue(query.isExists());
        assertEquals(anycastAddress, query.getAddress());
        assertEquals(toFullQN(anycastAddress, q), query.getName());
        assertEquals(0, query.getMessageCount());
    }
}
Also used : 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) QueueQueryResult(org.apache.activemq.artemis.core.server.QueueQueryResult) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Aggregations

ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)601 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)518 Test (org.junit.Test)465 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)395 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)372 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)350 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)277 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)254 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)206 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)108 Configuration (org.apache.activemq.artemis.core.config.Configuration)78 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)65 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)63 CountDownLatch (java.util.concurrent.CountDownLatch)54 Queue (org.apache.activemq.artemis.core.server.Queue)49 ArrayList (java.util.ArrayList)44 HashMap (java.util.HashMap)43 HashSet (java.util.HashSet)42 ActiveMQJAASSecurityManager (org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager)42 Message (org.apache.activemq.artemis.api.core.Message)41