Search in sources :

Example 1 with ClientProducerImpl

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

the class TemporaryQueueTest method testBlockingWithTemporaryQueue.

@Test
public void testBlockingWithTemporaryQueue() throws Exception {
    AddressSettings setting = new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.BLOCK).setMaxSizeBytes(1024 * 1024);
    server.getAddressSettingsRepository().addMatch("TestAD", setting);
    ClientSessionFactory consumerCF = createSessionFactory(locator);
    ClientSession consumerSession = consumerCF.createSession(true, true);
    consumerSession.addMetaData("consumer", "consumer");
    consumerSession.createTemporaryQueue("TestAD", "Q1");
    consumerSession.createConsumer("Q1");
    consumerSession.start();
    final ClientProducerImpl prod = (ClientProducerImpl) session.createProducer("TestAD");
    final AtomicInteger errors = new AtomicInteger(0);
    final AtomicInteger msgs = new AtomicInteger(0);
    final int TOTAL_MSG = 1000;
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                for (int i = 0; i < TOTAL_MSG; i++) {
                    ClientMessage msg = session.createMessage(false);
                    msg.getBodyBuffer().writeBytes(new byte[1024]);
                    prod.send(msg);
                    msgs.incrementAndGet();
                }
            } catch (Throwable e) {
                e.printStackTrace();
                errors.incrementAndGet();
            }
            System.out.println("done");
        }
    };
    t.start();
    while (msgs.get() == 0) {
        Thread.sleep(100);
    }
    int blockedTime = 0;
    // https://issues.apache.org/jira/browse/ARTEMIS-368
    while (t.isAlive() && errors.get() == 0 && (!prod.getProducerCredits().isBlocked() || blockedTime < 60)) {
        if (prod.getProducerCredits().isBlocked()) {
            blockedTime++;
        } else {
            blockedTime = 0;
        }
        Thread.sleep(100);
    }
    assertEquals(0, errors.get());
    ClientSessionFactory newConsumerCF = createSessionFactory(locator);
    ClientSession newConsumerSession = newConsumerCF.createSession(true, true);
    newConsumerSession.createTemporaryQueue("TestAD", "Q2");
    ClientConsumer newConsumer = newConsumerSession.createConsumer("Q2");
    newConsumerSession.start();
    int toReceive = TOTAL_MSG - msgs.get();
    for (ServerSession sessionIterator : server.getSessions()) {
        if (sessionIterator.getMetaData("consumer") != null) {
            System.out.println("Failing session");
            ServerSessionImpl impl = (ServerSessionImpl) sessionIterator;
            impl.getRemotingConnection().fail(new ActiveMQDisconnectedException("failure e"));
        }
    }
    int secondReceive = 0;
    ClientMessage msg = null;
    while (secondReceive < toReceive && (msg = newConsumer.receive(5000)) != null) {
        msg.acknowledge();
        secondReceive++;
    }
    assertNull(newConsumer.receiveImmediate());
    assertEquals(toReceive, secondReceive);
    t.join();
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) ActiveMQDisconnectedException(org.apache.activemq.artemis.api.core.ActiveMQDisconnectedException) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientProducerImpl(org.apache.activemq.artemis.core.client.impl.ClientProducerImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ServerSessionImpl(org.apache.activemq.artemis.core.server.impl.ServerSessionImpl) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) Test(org.junit.Test)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ActiveMQDisconnectedException (org.apache.activemq.artemis.api.core.ActiveMQDisconnectedException)1 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)1 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)1 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)1 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)1 ClientProducerImpl (org.apache.activemq.artemis.core.client.impl.ClientProducerImpl)1 ServerSession (org.apache.activemq.artemis.core.server.ServerSession)1 ServerSessionImpl (org.apache.activemq.artemis.core.server.impl.ServerSessionImpl)1 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)1 Test (org.junit.Test)1