org.apache.activemq.artemis.core.client.impl.ClientProducerImpl

Here are the examples of the java api class org.apache.activemq.artemis.core.client.impl.ClientProducerImpl taken from open source projects.

1. TemporaryQueueTest#testBlockingWithTemporaryQueue()

View license
@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();
}