Search in sources :

Example 46 with ActiveMQException

use of org.apache.activemq.artemis.api.core.ActiveMQException 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 47 with ActiveMQException

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

the class CreateQueueIdempotentTest method testSequentialCreateQueueIdempotency.

@Test
public void testSequentialCreateQueueIdempotency() throws Exception {
    final SimpleString QUEUE = new SimpleString("SequentialCreateQueueIdempotency");
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sf = addSessionFactory(createSessionFactory(locator));
    ClientSession session = sf.createSession(false, true, true);
    session.createQueue(QUEUE, QUEUE, null, true);
    try {
        session.createQueue(QUEUE, QUEUE, null, true);
        fail("Expected exception, queue already exists");
    } catch (ActiveMQQueueExistsException qee) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQQueueExistsException(org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 48 with ActiveMQException

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

the class AcknowledgeTest method testAsyncConsumerAck.

@Test
public void testAsyncConsumerAck() throws Exception {
    ActiveMQServer server = createServer(false);
    server.start();
    ServerLocator locator = createInVMNonHALocator().setBlockOnAcknowledge(true).setAckBatchSize(0);
    ClientSessionFactory cf = createSessionFactory(locator);
    ClientSession sendSession = cf.createSession(false, true, true);
    final ClientSession session = cf.createSession(false, true, true);
    sendSession.createQueue(addressA, queueA, false);
    ClientProducer cp = sendSession.createProducer(addressA);
    ClientConsumer cc = session.createConsumer(queueA);
    int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        cp.send(sendSession.createMessage(false));
    }
    final CountDownLatch latch = new CountDownLatch(numMessages);
    session.start();
    cc.setMessageHandler(new MessageHandler() {

        @Override
        public void onMessage(final ClientMessage message) {
            try {
                message.acknowledge();
            } catch (ActiveMQException e) {
                try {
                    session.close();
                } catch (ActiveMQException e1) {
                    e1.printStackTrace();
                }
            }
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Queue q = (Queue) server.getPostOffice().getBinding(queueA).getBindable();
    Assert.assertEquals(0, q.getDeliveringCount());
    sendSession.close();
    session.close();
}
Also used : MessageHandler(org.apache.activemq.artemis.api.core.client.MessageHandler) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) 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) Queue(org.apache.activemq.artemis.core.server.Queue) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 49 with ActiveMQException

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

the class AcknowledgeTest method testAsyncConsumerAckLastMessageOnly.

@Test
public void testAsyncConsumerAckLastMessageOnly() throws Exception {
    ActiveMQServer server = createServer(false);
    server.start();
    ServerLocator locator = createInVMNonHALocator().setBlockOnAcknowledge(true).setAckBatchSize(0);
    ClientSessionFactory cf = createSessionFactory(locator);
    ClientSession sendSession = cf.createSession(false, true, true);
    final ClientSession session = cf.createSession(false, true, true);
    sendSession.createQueue(addressA, queueA, false);
    ClientProducer cp = sendSession.createProducer(addressA);
    ClientConsumer cc = session.createConsumer(queueA);
    int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        cp.send(sendSession.createMessage(false));
    }
    final CountDownLatch latch = new CountDownLatch(numMessages);
    session.start();
    cc.setMessageHandler(new MessageHandler() {

        @Override
        public void onMessage(final ClientMessage message) {
            if (latch.getCount() == 1) {
                try {
                    message.acknowledge();
                } catch (ActiveMQException e) {
                    try {
                        session.close();
                    } catch (ActiveMQException e1) {
                        e1.printStackTrace();
                    }
                }
            }
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Queue q = (Queue) server.getPostOffice().getBinding(queueA).getBindable();
    Assert.assertEquals(0, q.getDeliveringCount());
    sendSession.close();
    session.close();
}
Also used : MessageHandler(org.apache.activemq.artemis.api.core.client.MessageHandler) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) 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) Queue(org.apache.activemq.artemis.core.server.Queue) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 50 with ActiveMQException

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

the class CreateQueueTest method testUnsupportedRoutingType.

@Test
public void testUnsupportedRoutingType() throws Exception {
    ClientSession sendSession = cf.createSession(false, true, true);
    server.getAddressSettingsRepository().addMatch(addressA.toString(), new AddressSettings().setAutoCreateAddresses(false));
    server.getAddressSettingsRepository().addMatch(addressB.toString(), new AddressSettings().setAutoCreateAddresses(false));
    EnumSet<RoutingType> routingTypes = EnumSet.of(RoutingType.ANYCAST);
    sendSession.createAddress(addressA, routingTypes, false);
    try {
        sendSession.createQueue(addressA, RoutingType.MULTICAST, queueA);
        fail("Creating a queue here should fail since the queue routing type differs from what is supported on the address.");
    } catch (Exception e) {
        assertTrue(e instanceof ActiveMQException);
        ActiveMQException ae = (ActiveMQException) e;
        assertEquals(ActiveMQExceptionType.INTERNAL_ERROR, ae.getType());
    }
    routingTypes = EnumSet.of(RoutingType.MULTICAST);
    sendSession.createAddress(addressB, routingTypes, false);
    try {
        sendSession.createQueue(addressB, RoutingType.ANYCAST, queueB);
        fail("Creating a queue here should fail since the queue routing type differs from what is supported on the address.");
    } catch (Exception e) {
        assertTrue(e instanceof ActiveMQException);
        ActiveMQException ae = (ActiveMQException) e;
        assertEquals(ActiveMQExceptionType.INTERNAL_ERROR, ae.getType());
    }
    sendSession.close();
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) RoutingType(org.apache.activemq.artemis.api.core.RoutingType) Test(org.junit.Test)

Aggregations

ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)254 Test (org.junit.Test)140 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)121 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)84 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)79 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)78 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)59 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)54 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)45 CountDownLatch (java.util.concurrent.CountDownLatch)40 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)36 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)31 HashSet (java.util.HashSet)29 ActiveMQJAASSecurityManager (org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager)27 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)23 Role (org.apache.activemq.artemis.core.security.Role)22 ActiveMQSecurityException (org.apache.activemq.artemis.api.core.ActiveMQSecurityException)20 Set (java.util.Set)16 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)16 Packet (org.apache.activemq.artemis.core.protocol.core.Packet)15