Search in sources :

Example 11 with ClientConsumerInternal

use of org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal in project activemq-artemis by apache.

the class ConsumerTest method receive.

private void receive(boolean cancelOnce) throws Throwable {
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, true, false);
    ClientProducer producer = session.createProducer(QUEUE);
    ClientMessage message = session.createMessage(Message.TEXT_TYPE, true, 0, System.currentTimeMillis(), (byte) 4);
    message.getBodyBuffer().writeString("hi");
    message.putStringProperty("hello", "elo");
    producer.send(message);
    session.commit();
    session.close();
    if (durable) {
        server.stop();
        server.start();
    }
    sf = createSessionFactory(locator);
    session = sf.createSession(false, true, true, false);
    ClientConsumer consumer = session.createConsumer(QUEUE);
    session.start();
    if (cancelOnce) {
        final ClientConsumerInternal consumerInternal = (ClientConsumerInternal) consumer;
        Wait.waitFor(() -> consumerInternal.getBufferSize() > 0);
        consumer.close();
        consumer = session.createConsumer(QUEUE);
    }
    ClientMessage message2 = consumer.receive(1000);
    Assert.assertNotNull(message2);
    System.out.println("Id::" + message2.getMessageID());
    System.out.println("Received " + message2);
    System.out.println("Clie:" + ByteUtil.bytesToHex(message2.getBuffer().array(), 4));
    System.out.println("String::" + message2.getReadOnlyBodyBuffer().readString());
    Assert.assertEquals("elo", message2.getStringProperty("hello"));
    Assert.assertEquals("hi", message2.getReadOnlyBodyBuffer().readString());
    Assert.assertNull(consumer.receiveImmediate());
    session.close();
}
Also used : ClientConsumerInternal(org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal) 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)

Example 12 with ClientConsumerInternal

use of org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal in project activemq-artemis by apache.

the class PagingTest method testMoveExpire.

@Test
public void testMoveExpire() throws Exception {
    clearDataRecreateServerDirs();
    Configuration config = // disable compact
    createDefaultInVMConfig().setJournalDirectory(getJournalDir()).setJournalSyncNonTransactional(false).setJournalCompactMinFiles(0).setMessageExpiryScanPeriod(500);
    server = createServer(true, config, PagingTest.PAGE_SIZE, PagingTest.PAGE_MAX);
    AddressSettings defaultSetting = new AddressSettings().setPageSizeBytes(PAGE_SIZE).setMaxSizeBytes(PAGE_MAX).setExpiryAddress(new SimpleString("EXP")).setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE);
    server.getAddressSettingsRepository().clear();
    server.getAddressSettingsRepository().addMatch("#", defaultSetting);
    server.start();
    final int numberOfMessages = 5000;
    locator = createInVMNonHALocator().setConsumerWindowSize(10 * 1024 * 1024).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true);
    ClientSessionFactory sf = locator.createSessionFactory();
    ClientSession session = sf.createSession(false, false, false);
    session.createQueue(PagingTest.ADDRESS, PagingTest.ADDRESS, null, true);
    session.createQueue("EXP", "EXP", null, true);
    Queue queue1 = server.locateQueue(ADDRESS);
    Queue qEXP = server.locateQueue(new SimpleString("EXP"));
    ClientProducer producer = session.createProducer(PagingTest.ADDRESS);
    final int MESSAGE_SIZE = 1024;
    byte[] body = new byte[MESSAGE_SIZE];
    ByteBuffer bb = ByteBuffer.wrap(body);
    for (int j = 1; j <= MESSAGE_SIZE; j++) {
        bb.put(getSamplebyte(j));
    }
    for (int i = 0; i < numberOfMessages; i++) {
        ClientMessage message = session.createMessage(true);
        if (i < 1000) {
            message.setExpiration(System.currentTimeMillis() + 100);
        }
        message.putIntProperty("tst-count", i);
        ActiveMQBuffer bodyLocal = message.getBodyBuffer();
        bodyLocal.writeBytes(body);
        producer.send(message);
        if (i % 1000 == 0) {
            session.commit();
        }
    }
    session.commit();
    producer.close();
    Wait.assertEquals(1000, qEXP::getMessageCount);
    session.start();
    ClientConsumer consumer = session.createConsumer(ADDRESS);
    for (int i = 0; i < numberOfMessages - 1000; i++) {
        ClientMessage message = consumer.receive(5000);
        assertNotNull(message);
        message.acknowledge();
        assertTrue(message.getIntProperty("tst-count") >= 1000);
    }
    session.commit();
    assertNull(consumer.receiveImmediate());
    Wait.assertEquals(0, queue1::getMessageCount);
    consumer.close();
    consumer = session.createConsumer("EXP");
    for (int i = 0; i < 1000; i++) {
        ClientMessage message = consumer.receive(5000);
        assertNotNull(message);
        message.acknowledge();
        assertTrue(message.getIntProperty("tst-count") < 1000);
    }
    assertNull(consumer.receiveImmediate());
    // This is just to hold some messages as being delivered
    ClientConsumerInternal cons = (ClientConsumerInternal) session.createConsumer(ADDRESS);
    session.commit();
    producer.close();
    session.close();
    server.stop();
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) ClientConsumerInternal(org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal) DivertConfiguration(org.apache.activemq.artemis.core.config.DivertConfiguration) StoreConfiguration(org.apache.activemq.artemis.core.config.StoreConfiguration) Configuration(org.apache.activemq.artemis.core.config.Configuration) DatabaseStorageConfiguration(org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ByteBuffer(java.nio.ByteBuffer) 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) Queue(org.apache.activemq.artemis.core.server.Queue) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Aggregations

ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)12 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)12 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)12 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)12 ClientConsumerInternal (org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal)12 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)10 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)9 IOException (java.io.IOException)7 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)6 Test (org.junit.Test)5 ByteBuffer (java.nio.ByteBuffer)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)2 MessageHandler (org.apache.activemq.artemis.api.core.client.MessageHandler)2 Configuration (org.apache.activemq.artemis.core.config.Configuration)2 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)2 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)2 DatabaseStorageConfiguration (org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration)2 Queue (org.apache.activemq.artemis.core.server.Queue)2 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)2