Search in sources :

Example 26 with ClientMessage

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

the class ActiveMQMessageProducer method doSendx.

private void doSendx(ActiveMQDestination destination, final Message jmsMessage, final int deliveryMode, final int priority, final long timeToLive, CompletionListener completionListener) throws JMSException {
    jmsMessage.setJMSDeliveryMode(deliveryMode);
    jmsMessage.setJMSPriority(priority);
    if (timeToLive == 0) {
        jmsMessage.setJMSExpiration(0);
    } else {
        jmsMessage.setJMSExpiration(System.currentTimeMillis() + timeToLive);
    }
    if (!disableMessageTimestamp) {
        jmsMessage.setJMSTimestamp(System.currentTimeMillis());
    } else {
        jmsMessage.setJMSTimestamp(0);
    }
    SimpleString address = null;
    if (destination == null) {
        if (defaultDestination == null) {
            throw new UnsupportedOperationException("Destination must be specified on send with an anonymous producer");
        }
        destination = defaultDestination;
    } else {
        if (defaultDestination != null) {
            if (!destination.equals(defaultDestination)) {
                throw new UnsupportedOperationException("Where a default destination is specified " + "for the sender and a destination is " + "specified in the arguments to the send, " + "these destinations must be equal");
            }
        }
        address = destination.getSimpleAddress();
        if (!connection.containsKnownDestination(address)) {
            try {
                ClientSession.AddressQuery query = clientSession.addressQuery(address);
                if (!query.isExists()) {
                    if (destination.isQueue() && query.isAutoCreateQueues()) {
                        clientSession.createAddress(address, RoutingType.ANYCAST, true);
                        if (destination.isTemporary()) {
                            // TODO is it right to use the address for the queue name here?
                            clientSession.createTemporaryQueue(address, RoutingType.ANYCAST, address);
                        } else {
                            createQueue(destination, RoutingType.ANYCAST, address, null, true, true, query.getDefaultMaxConsumers(), query.isDefaultPurgeOnNoConsumers(), query.isDefaultExclusive(), query.isDefaultLastValueQueue());
                        }
                    } else if (!destination.isQueue() && query.isAutoCreateAddresses()) {
                        clientSession.createAddress(address, RoutingType.MULTICAST, true);
                    } else if ((destination.isQueue() && !query.isAutoCreateQueues()) || (!destination.isQueue() && !query.isAutoCreateAddresses())) {
                        throw new InvalidDestinationException("Destination " + address + " does not exist");
                    }
                } else {
                    ClientSession.QueueQuery queueQuery = clientSession.queueQuery(address);
                    if (queueQuery.isExists()) {
                        connection.addKnownDestination(address);
                    } else if (destination.isQueue() && query.isAutoCreateQueues()) {
                        if (destination.isTemporary()) {
                            clientSession.createTemporaryQueue(address, RoutingType.ANYCAST, address);
                        } else {
                            createQueue(destination, RoutingType.ANYCAST, address, null, true, true, query.getDefaultMaxConsumers(), query.isDefaultPurgeOnNoConsumers(), query.isDefaultExclusive(), query.isDefaultLastValueQueue());
                        }
                    }
                }
            } catch (ActiveMQQueueExistsException e) {
            // The queue was created by another client/admin between the query check and send create queue packet
            } catch (ActiveMQException e) {
                throw JMSExceptionHelper.convertFromActiveMQException(e);
            }
        }
    }
    ActiveMQMessage activeMQJmsMessage;
    boolean foreign = false;
    // First convert from foreign message if appropriate
    if (!(jmsMessage instanceof ActiveMQMessage)) {
        if (jmsMessage instanceof BytesMessage) {
            activeMQJmsMessage = new ActiveMQBytesMessage((BytesMessage) jmsMessage, clientSession);
        } else if (jmsMessage instanceof MapMessage) {
            activeMQJmsMessage = new ActiveMQMapMessage((MapMessage) jmsMessage, clientSession);
        } else if (jmsMessage instanceof ObjectMessage) {
            activeMQJmsMessage = new ActiveMQObjectMessage((ObjectMessage) jmsMessage, clientSession, options);
        } else if (jmsMessage instanceof StreamMessage) {
            activeMQJmsMessage = new ActiveMQStreamMessage((StreamMessage) jmsMessage, clientSession);
        } else if (jmsMessage instanceof TextMessage) {
            activeMQJmsMessage = new ActiveMQTextMessage((TextMessage) jmsMessage, clientSession);
        } else {
            activeMQJmsMessage = new ActiveMQMessage(jmsMessage, clientSession);
        }
        // Set the destination on the original message
        jmsMessage.setJMSDestination(destination);
        foreign = true;
    } else {
        activeMQJmsMessage = (ActiveMQMessage) jmsMessage;
    }
    if (!disableMessageID) {
        // Generate a JMS id
        UUID uid = UUIDGenerator.getInstance().generateUUID();
        activeMQJmsMessage.getCoreMessage().setUserID(uid);
        activeMQJmsMessage.resetMessageID(null);
    }
    if (foreign) {
        jmsMessage.setJMSMessageID(activeMQJmsMessage.getJMSMessageID());
    }
    activeMQJmsMessage.setJMSDestination(destination);
    try {
        activeMQJmsMessage.doBeforeSend();
    } catch (Exception e) {
        JMSException je = new JMSException(e.getMessage());
        je.initCause(e);
        throw je;
    }
    if (defaultDeliveryDelay > 0) {
        activeMQJmsMessage.setJMSDeliveryTime(System.currentTimeMillis() + defaultDeliveryDelay);
    }
    ClientMessage coreMessage = activeMQJmsMessage.getCoreMessage();
    coreMessage.putStringProperty(ActiveMQConnection.CONNECTION_ID_PROPERTY_NAME, connID);
    coreMessage.setRoutingType(destination.isQueue() ? RoutingType.ANYCAST : RoutingType.MULTICAST);
    try {
        /**
         * Using a completionListener requires wrapping using a {@link CompletionListenerWrapper},
         * so we avoid it if we can.
         */
        if (completionListener != null) {
            clientProducer.send(address, coreMessage, new CompletionListenerWrapper(completionListener, jmsMessage, this));
        } else {
            clientProducer.send(address, coreMessage);
        }
    } catch (ActiveMQInterruptedException e) {
        JMSException jmsException = new JMSException(e.getMessage());
        jmsException.initCause(e);
        throw jmsException;
    } catch (ActiveMQException e) {
        throw JMSExceptionHelper.convertFromActiveMQException(e);
    } catch (java.lang.IllegalStateException e) {
        JMSException je = new IllegalStateException(e.getMessage());
        je.setStackTrace(e.getStackTrace());
        je.initCause(e);
        throw je;
    }
}
Also used : IllegalStateException(javax.jms.IllegalStateException) MapMessage(javax.jms.MapMessage) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ObjectMessage(javax.jms.ObjectMessage) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQQueueExistsException(org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException) UUID(org.apache.activemq.artemis.utils.UUID) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) InvalidDestinationException(javax.jms.InvalidDestinationException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) InvalidDestinationException(javax.jms.InvalidDestinationException) ActiveMQQueueExistsException(org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException) IllegalStateException(javax.jms.IllegalStateException) JMSException(javax.jms.JMSException) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) StreamMessage(javax.jms.StreamMessage) TextMessage(javax.jms.TextMessage)

Example 27 with ClientMessage

use of org.apache.activemq.artemis.api.core.client.ClientMessage 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 28 with ClientMessage

use of org.apache.activemq.artemis.api.core.client.ClientMessage 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 29 with ClientMessage

use of org.apache.activemq.artemis.api.core.client.ClientMessage 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)

Example 30 with ClientMessage

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

the class ConsumerWindowSizeTest method internalTestSlowConsumerNoBuffer2.

private void internalTestSlowConsumerNoBuffer2(final boolean largeMessages) throws Exception {
    ActiveMQServer server = createServer(false, isNetty());
    ClientSession session1 = null;
    ClientSession session2 = null;
    try {
        final int numberOfMessages = 10;
        server.start();
        locator.setConsumerWindowSize(0);
        if (largeMessages) {
            locator.setMinLargeMessageSize(100);
        }
        ClientSessionFactory sf = createSessionFactory(locator);
        session1 = sf.createSession(false, true, true);
        session2 = sf.createSession(false, true, true);
        session1.start();
        session2.start();
        SimpleString ADDRESS = new SimpleString("some-queue");
        session1.createQueue(ADDRESS, ADDRESS, true);
        ClientConsumerInternal cons1 = (ClientConsumerInternal) session1.createConsumer(ADDRESS);
        // Note we make sure we send the messages *before* cons2 is created
        ClientProducer prod = session1.createProducer(ADDRESS);
        for (int i = 0; i < numberOfMessages; i++) {
            ClientMessage msg = createTextMessage(session1, "Msg" + i);
            if (largeMessages) {
                msg.getBodyBuffer().writeBytes(new byte[600]);
            }
            prod.send(msg);
        }
        ClientConsumerInternal cons2 = (ClientConsumerInternal) session2.createConsumer(ADDRESS);
        for (int i = 0; i < numberOfMessages / 2; i++) {
            ClientMessage msg = cons1.receive(1000);
            Assert.assertNotNull("expected message at i = " + i, msg);
            String str = getTextMessage(msg);
            Assert.assertEquals("Msg" + i, str);
            log.info("got msg " + str);
            msg.acknowledge();
            Assert.assertEquals("A slow consumer shouldn't buffer anything on the client side!", 0, cons1.getBufferSize());
        }
        for (int i = numberOfMessages / 2; i < numberOfMessages; i++) {
            ClientMessage msg = cons2.receive(1000);
            Assert.assertNotNull("expected message at i = " + i, msg);
            String str = getTextMessage(msg);
            log.info("got msg " + str);
            Assert.assertEquals("Msg" + i, str);
            msg.acknowledge();
            Assert.assertEquals("A slow consumer shouldn't buffer anything on the client side!", 0, cons2.getBufferSize());
        }
        // just to make sure everything is flushed and no pending packets on the sending buffer, or
        session1.close();
        // the getMessageCount would fail
        session2.close();
        session1 = sf.createSession(false, true, true);
        session1.start();
        session2 = sf.createSession(false, true, true);
        session2.start();
        prod = session1.createProducer(ADDRESS);
        Assert.assertEquals(0, getMessageCount(server, ADDRESS.toString()));
        // This should also work the other way around
        cons1.close();
        cons2.close();
        cons1 = (ClientConsumerInternal) session1.createConsumer(ADDRESS);
        for (int i = 0; i < numberOfMessages; i++) {
            ClientMessage msg = createTextMessage(session1, "Msg" + i);
            if (largeMessages) {
                msg.getBodyBuffer().writeBytes(new byte[600]);
            }
            prod.send(msg);
        }
        cons2 = (ClientConsumerInternal) session2.createConsumer(ADDRESS);
        for (int i = 0; i < numberOfMessages / 2; i++) {
            ClientMessage msg = cons2.receive(1000);
            Assert.assertNotNull("expected message at i = " + i, msg);
            Assert.assertEquals("Msg" + i, msg.getBodyBuffer().readString());
            msg.acknowledge();
            Assert.assertEquals("A slow consumer shouldn't buffer anything on the client side!", 0, cons2.getBufferSize());
        }
        for (int i = numberOfMessages / 2; i < numberOfMessages; i++) {
            ClientMessage msg = cons1.receive(1000);
            Assert.assertNotNull("expected message at i = " + i, msg);
            Assert.assertEquals("Msg" + i, msg.getBodyBuffer().readString());
            msg.acknowledge();
            Assert.assertEquals("A slow consumer shouldn't buffer anything on the client side!", 0, cons1.getBufferSize());
        }
        session1.close();
        session1 = null;
        session2.close();
        session2 = null;
        Assert.assertEquals(0, getMessageCount(server, ADDRESS.toString()));
    } finally {
        try {
            if (session1 != null) {
                session1.close();
            }
            if (session2 != null) {
                session2.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) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) IOException(java.io.IOException)

Aggregations

ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)889 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)708 Test (org.junit.Test)647 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)623 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)606 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)445 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)363 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)167 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)140 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)102 CountDownLatch (java.util.concurrent.CountDownLatch)99 Configuration (org.apache.activemq.artemis.core.config.Configuration)87 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)85 Queue (org.apache.activemq.artemis.core.server.Queue)72 Xid (javax.transaction.xa.Xid)67 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)63 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)54 HashMap (java.util.HashMap)53 ArrayList (java.util.ArrayList)51 DatabaseStorageConfiguration (org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration)47