Search in sources :

Example 1 with ClientProducer

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

the class TransformTest method publish.

public static void publish(String destination, Serializable object, String contentType) throws Exception {
    ClientSession session = manager.getQueueManager().getSessionFactory().createSession();
    try {
        ClientProducer producer = session.createProducer(destination);
        ClientMessage message = session.createMessage(Message.OBJECT_TYPE, false);
        if (contentType == null) {
            ActiveMQ.setEntity(message, object);
        } else
            ActiveMQ.setEntity(message, object, contentType);
        producer.send(message);
        session.start();
    } finally {
        session.close();
    }
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer)

Example 2 with ClientProducer

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

the class PostMessage method publish.

public void publish(HttpHeaders headers, byte[] body, String dup, boolean durable, Long ttl, Long expiration, Integer priority) throws Exception {
    Pooled pooled = getPooled();
    try {
        ClientProducer producer = pooled.producer;
        ClientMessage message = createActiveMQMessage(headers, body, durable, ttl, expiration, priority, pooled.session);
        message.putStringProperty(Message.HDR_DUPLICATE_DETECTION_ID.toString(), dup);
        producer.send(message);
        ActiveMQRestLogger.LOGGER.debug("Sent message: " + message);
        pool.add(pooled);
    } catch (Exception ex) {
        try {
            pooled.session.close();
        } catch (ActiveMQException e) {
        }
        addPooled();
        throw ex;
    }
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 3 with ClientProducer

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

the class ConsumerWindowSizeTest method testMultipleImmediate.

@Test
public void testMultipleImmediate() throws Exception {
    final int NUMBER_OF_MESSAGES = 200;
    ActiveMQServer server = createServer(false, isNetty());
    server.start();
    locator.setConsumerWindowSize(0);
    final ClientSessionFactory sf = createSessionFactory(locator);
    {
        ClientSession session = sf.createSession(false, false, false);
        session.createQueue("testWindow", "testWindow", true);
        session.close();
    }
    Thread[] threads = new Thread[10];
    final AtomicInteger errors = new AtomicInteger(0);
    final CountDownLatch latchStart = new CountDownLatch(1);
    final AtomicInteger received = new AtomicInteger(0);
    for (int i = 0; i < threads.length; i++) {
        threads[i] = new Thread() {

            @Override
            public void run() {
                try {
                    ClientSession session = sf.createSession(false, false);
                    ClientConsumer consumer = session.createConsumer("testWindow");
                    session.start();
                    latchStart.await(10, TimeUnit.SECONDS);
                    while (true) {
                        ClientMessage msg = consumer.receiveImmediate();
                        if (msg == null) {
                            break;
                        }
                        msg.acknowledge();
                        session.commit();
                        received.incrementAndGet();
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                    errors.incrementAndGet();
                }
            }
        };
        threads[i].start();
    }
    ClientSession senderSession = sf.createSession(false, false);
    ClientProducer producer = senderSession.createProducer("testWindow");
    ClientMessage sent = senderSession.createMessage(true);
    sent.putStringProperty("hello", "world");
    for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
        producer.send(sent);
        senderSession.commit();
    }
    latchStart.countDown();
    for (Thread t : threads) {
        t.join();
    }
    Assert.assertEquals(0, errors.get());
    Assert.assertEquals(NUMBER_OF_MESSAGES, received.get());
}
Also used : ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 4 with ClientProducer

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

the class ConsumerWindowSizeTest method internalTestSlowConsumerNoBuffer.

private void internalTestSlowConsumerNoBuffer(final boolean largeMessages) throws Exception {
    ActiveMQServer server = createServer(false, isNetty());
    ClientSession sessionB = null;
    ClientSession session = null;
    try {
        final int numberOfMessages = 100;
        server.start();
        locator.setConsumerWindowSize(0);
        if (largeMessages) {
            locator.setMinLargeMessageSize(100);
        }
        ClientSessionFactory sf = createSessionFactory(locator);
        session = sf.createSession(false, true, true);
        SimpleString ADDRESS = addressA;
        session.createQueue(ADDRESS, ADDRESS, true);
        sessionB = sf.createSession(false, true, true);
        sessionB.start();
        session.start();
        ClientConsumerInternal consNeverUsed = (ClientConsumerInternal) sessionB.createConsumer(ADDRESS);
        ClientProducer prod = session.createProducer(ADDRESS);
        // This will force a credit to be sent, but if the message wasn't received we need to take out that credit from
        // the server
        // or the client will be buffering messages
        Assert.assertNull(consNeverUsed.receive(1));
        ClientMessage msg = createTextMessage(session, "This one will expire");
        if (largeMessages) {
            msg.getBodyBuffer().writeBytes(new byte[600]);
        }
        msg.setExpiration(System.currentTimeMillis() + 100);
        prod.send(msg);
        msg = createTextMessage(session, "First-on-non-buffered");
        prod.send(msg);
        Thread.sleep(110);
        // It will be able to receive another message, but it shouldn't send a credit again, as the credit was already
        // sent
        msg = consNeverUsed.receive(TIMEOUT * 1000);
        Assert.assertNotNull(msg);
        Assert.assertEquals("First-on-non-buffered", getTextMessage(msg));
        msg.acknowledge();
        ClientConsumer cons1 = session.createConsumer(ADDRESS);
        for (int i = 0; i < numberOfMessages; i++) {
            msg = createTextMessage(session, "Msg" + i);
            if (largeMessages) {
                msg.getBodyBuffer().writeBytes(new byte[600]);
            }
            prod.send(msg);
        }
        for (int i = 0; i < numberOfMessages; i++) {
            msg = cons1.receive(1000);
            Assert.assertNotNull("expected message at i = " + i, msg);
            Assert.assertEquals("Msg" + i, getTextMessage(msg));
            msg.acknowledge();
        }
        Assert.assertEquals(0, consNeverUsed.getBufferSize());
        session.close();
        session = null;
        sessionB.close();
        sessionB = null;
        Assert.assertEquals(0, getMessageCount(server, ADDRESS.toString()));
    } finally {
        try {
            if (session != null) {
                session.close();
            }
            if (sessionB != null) {
                sessionB.close();
            }
        } catch (Exception ignored) {
        }
    }
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ClientConsumerInternal(org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal) 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) IOException(java.io.IOException)

Example 5 with ClientProducer

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

the class ConsumerWindowSizeTest method testSendWindowSize.

/*
   * tests send window size. we do this by having 2 receivers on the q. since we roundrobin the consumer for delivery we
   * know if consumer 1 has received n messages then consumer 2 must have also have received n messages or at least up
   * to its window size
   * */
@Test
public void testSendWindowSize() throws Exception {
    ActiveMQServer messagingService = createServer(false, isNetty());
    locator.setBlockOnNonDurableSend(false);
    messagingService.start();
    int numMessage = 100;
    locator.setConsumerWindowSize(numMessage * getMessageEncodeSize(addressA));
    ClientSessionFactory cf = createSessionFactory(locator);
    ClientSession sendSession = cf.createSession(false, true, true);
    ClientSession receiveSession = cf.createSession(false, true, true);
    sendSession.createQueue(addressA, queueA, false);
    ClientConsumer receivingConsumer = receiveSession.createConsumer(queueA);
    ClientSession session = cf.createSession(false, true, true);
    ClientProducer cp = sendSession.createProducer(addressA);
    ClientConsumer cc = session.createConsumer(queueA);
    session.start();
    receiveSession.start();
    for (int i = 0; i < numMessage * 4; i++) {
        cp.send(sendSession.createMessage(false));
    }
    for (int i = 0; i < numMessage * 2; i++) {
        ClientMessage m = receivingConsumer.receive(5000);
        Assert.assertNotNull(m);
        m.acknowledge();
    }
    receiveSession.close();
    for (int i = 0; i < numMessage * 2; i++) {
        ClientMessage m = cc.receive(5000);
        Assert.assertNotNull(m);
        m.acknowledge();
    }
    session.close();
    sendSession.close();
    Assert.assertEquals(0, getMessageCount(messagingService, queueA.toString()));
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) 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)

Aggregations

ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)859 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)716 Test (org.junit.Test)702 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)658 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)644 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)478 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)395 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)173 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)166 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)106 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)103 CountDownLatch (java.util.concurrent.CountDownLatch)102 Configuration (org.apache.activemq.artemis.core.config.Configuration)93 Queue (org.apache.activemq.artemis.core.server.Queue)85 Xid (javax.transaction.xa.Xid)70 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)64 HashMap (java.util.HashMap)63 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)55 ArrayList (java.util.ArrayList)51 QueueControl (org.apache.activemq.artemis.api.core.management.QueueControl)50