Search in sources :

Example 71 with ClientSessionFactory

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

the class LargeMessageTest method internalTestSendRollback.

private void internalTestSendRollback(final boolean isXA, final boolean durable) throws Exception {
    ClientSession session = null;
    ActiveMQServer server = createServer(true, isNetty(), storeType);
    server.start();
    ClientSessionFactory sf = addSessionFactory(createSessionFactory(locator));
    session = sf.createSession(isXA, false, false);
    session.createQueue(ADDRESS, ADDRESS, true);
    Xid xid = null;
    if (isXA) {
        xid = RandomUtil.randomXid();
        session.start(xid, XAResource.TMNOFLAGS);
    }
    ClientProducer producer = session.createProducer(ADDRESS);
    Message clientFile = createLargeClientMessageStreaming(session, 50000, durable);
    for (int i = 0; i < 1; i++) {
        producer.send(clientFile);
    }
    if (isXA) {
        session.end(xid, XAResource.TMSUCCESS);
        session.prepare(xid);
        session.close();
        server.stop();
        server.start();
        sf = createSessionFactory(locator);
        session = sf.createSession(isXA, false, false);
        session.rollback(xid);
    } else {
        session.rollback();
    }
    session.close();
    validateNoFilesOnLargeDir();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) Xid(javax.transaction.xa.Xid) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) 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)

Example 72 with ClientSessionFactory

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

the class LargeMessageTest method testDLAOnExpiry.

@Test
public void testDLAOnExpiry() throws Exception {
    final int messageSize = (int) (3.5 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE);
    ClientSession session = null;
    ActiveMQServer server = createServer(true, isNetty(), storeType);
    server.start();
    ClientSessionFactory sf = addSessionFactory(createSessionFactory(locator));
    SimpleString ADDRESS_DLA = ADDRESS.concat("-dla");
    SimpleString ADDRESS_EXPIRY = ADDRESS.concat("-expiry");
    AddressSettings addressSettings = new AddressSettings().setDeadLetterAddress(ADDRESS_DLA).setExpiryAddress(ADDRESS_EXPIRY).setMaxDeliveryAttempts(1);
    server.getAddressSettingsRepository().addMatch("*", addressSettings);
    session = sf.createSession(false, false, false);
    session.createQueue(ADDRESS, ADDRESS, true);
    session.createQueue(ADDRESS_DLA, ADDRESS_DLA, true);
    session.createQueue(ADDRESS_EXPIRY, ADDRESS_EXPIRY, true);
    ClientProducer producer = session.createProducer(ADDRESS);
    Message clientFile = createLargeClientMessageStreaming(session, messageSize, true);
    clientFile.setExpiration(System.currentTimeMillis());
    producer.send(clientFile);
    session.commit();
    session.start();
    ClientConsumer consumerExpired = session.createConsumer(ADDRESS);
    // to kick expiry quicker than waiting reaper thread
    Assert.assertNull(consumerExpired.receiveImmediate());
    consumerExpired.close();
    ClientConsumer consumerExpiry = session.createConsumer(ADDRESS_EXPIRY);
    ClientMessage msg1 = consumerExpiry.receive(5000);
    Assert.assertNotNull(msg1);
    msg1.acknowledge();
    for (int j = 0; j < messageSize; j++) {
        Assert.assertEquals(ActiveMQTestBase.getSamplebyte(j), msg1.getBodyBuffer().readByte());
    }
    session.rollback();
    consumerExpiry.close();
    for (int i = 0; i < 10; i++) {
        consumerExpiry = session.createConsumer(ADDRESS_DLA);
        msg1 = consumerExpiry.receive(5000);
        Assert.assertNotNull(msg1);
        msg1.acknowledge();
        for (int j = 0; j < messageSize; j++) {
            Assert.assertEquals(ActiveMQTestBase.getSamplebyte(j), msg1.getBodyBuffer().readByte());
        }
        session.rollback();
        consumerExpiry.close();
    }
    session.close();
    server.stop();
    server = createServer(true, isNetty(), storeType);
    server.start();
    sf = createSessionFactory(locator);
    session = sf.createSession(false, false, false);
    session.start();
    consumerExpiry = session.createConsumer(ADDRESS_DLA);
    msg1 = consumerExpiry.receive(5000);
    Assert.assertNotNull(msg1);
    msg1.acknowledge();
    for (int i = 0; i < messageSize; i++) {
        Assert.assertEquals(ActiveMQTestBase.getSamplebyte(i), msg1.getBodyBuffer().readByte());
    }
    session.commit();
    consumerExpiry.close();
    session.commit();
    session.close();
    validateNoFilesOnLargeDir();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) 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) Test(org.junit.Test)

Example 73 with ClientSessionFactory

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

the class LargeMessageTest method testDeliveryCount.

@Test
public void testDeliveryCount() throws Exception {
    final int messageSize = (int) (3.5 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE);
    ClientSession session = null;
    ActiveMQServer server = createServer(true, isNetty(), storeType);
    server.start();
    ClientSessionFactory sf = addSessionFactory(createSessionFactory(locator));
    session = sf.createSession(false, false, false);
    session.createQueue(ADDRESS, ADDRESS, true);
    ClientProducer producer = session.createProducer(ADDRESS);
    Message clientFile = createLargeClientMessageStreaming(session, messageSize, true);
    producer.send(clientFile);
    session.commit();
    session.start();
    ClientConsumer consumer = session.createConsumer(ADDRESS);
    ClientMessage msg = consumer.receive(10000);
    Assert.assertNotNull(msg);
    msg.acknowledge();
    Assert.assertEquals(1, msg.getDeliveryCount());
    log.info("body buffer is " + msg.getBodyBuffer());
    for (int i = 0; i < messageSize; i++) {
        Assert.assertEquals(ActiveMQTestBase.getSamplebyte(i), msg.getBodyBuffer().readByte());
    }
    session.rollback();
    session.close();
    session = sf.createSession(false, false, false);
    session.start();
    consumer = session.createConsumer(ADDRESS);
    msg = consumer.receive(10000);
    Assert.assertNotNull(msg);
    msg.acknowledge();
    for (int i = 0; i < messageSize; i++) {
        Assert.assertEquals(ActiveMQTestBase.getSamplebyte(i), msg.getBodyBuffer().readByte());
    }
    Assert.assertEquals(2, msg.getDeliveryCount());
    msg.acknowledge();
    consumer.close();
    session.commit();
    validateNoFilesOnLargeDir();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) 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) Test(org.junit.Test)

Example 74 with ClientSessionFactory

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

the class MessageCounterTest method testMessageCounter.

@Test
public void testMessageCounter() throws Exception {
    locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true);
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(null, null, false, false, false, false, 0);
    session.createQueue(QUEUE, QUEUE, null, false);
    ClientProducer producer = session.createProducer(QUEUE);
    final int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = createTextMessage(session, "m" + i);
        producer.send(message);
    }
    session.commit();
    session.start();
    Assert.assertEquals(100, getMessageCount(server.getPostOffice(), QUEUE.toString()));
    ClientConsumer consumer = session.createConsumer(QUEUE, null, false);
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = consumer.receive(1000);
        Assert.assertNotNull(message);
        message.acknowledge();
        session.commit();
        Assert.assertEquals("m" + i, message.getBodyBuffer().readString());
    }
    session.close();
    Assert.assertEquals(0, getMessageCount(server.getPostOffice(), QUEUE.toString()));
}
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) Test(org.junit.Test)

Example 75 with ClientSessionFactory

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

the class MessageGroupingTest method dotestMultipleGroupingXACommit.

private void dotestMultipleGroupingXACommit() throws Exception {
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sessionFactory = createSessionFactory(locator);
    ClientSession clientSession = sessionFactory.createSession(true, false, false);
    ClientProducer clientProducer = this.clientSession.createProducer(qName);
    ClientConsumer consumer = clientSession.createConsumer(qName);
    ClientConsumer consumer2 = clientSession.createConsumer(qName);
    clientSession.start();
    Xid xid = new XidImpl("bq".getBytes(), 4, "gtid".getBytes());
    clientSession.start(xid, XAResource.TMNOFLAGS);
    SimpleString groupId = new SimpleString("grp1");
    SimpleString groupId2 = new SimpleString("grp2");
    int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = createTextMessage(clientSession, "m" + i);
        if (i % 2 == 0 || i == 0) {
            message.putStringProperty(Message.HDR_GROUP_ID, groupId);
        } else {
            message.putStringProperty(Message.HDR_GROUP_ID, groupId2);
        }
        clientProducer.send(message);
    }
    CountDownLatch latch = new CountDownLatch(numMessages);
    DummyMessageHandler dummyMessageHandler = new DummyMessageHandler(latch, true);
    consumer.setMessageHandler(dummyMessageHandler);
    DummyMessageHandler dummyMessageHandler2 = new DummyMessageHandler(latch, true);
    consumer2.setMessageHandler(dummyMessageHandler2);
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    clientSession.end(xid, XAResource.TMSUCCESS);
    clientSession.prepare(xid);
    clientSession.commit(xid, false);
    Assert.assertEquals(dummyMessageHandler.list.size(), 50);
    int i = 0;
    for (ClientMessage message : dummyMessageHandler.list) {
        Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i);
        i += 2;
    }
    Assert.assertEquals(dummyMessageHandler2.list.size(), 50);
    i = 1;
    for (ClientMessage message : dummyMessageHandler2.list) {
        Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i);
        i += 2;
    }
    consumer.close();
    consumer2.close();
    consumer = this.clientSession.createConsumer(qName);
    Assert.assertNull(consumer.receiveImmediate());
    clientSession.close();
    locator.close();
}
Also used : Xid(javax.transaction.xa.Xid) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) 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) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator)

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)464 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