Search in sources :

Example 6 with ActiveMQQueueExistsException

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

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

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

the class PredefinedQueueTest method testFailOnCreatePredefinedQueues.

@Test
public void testFailOnCreatePredefinedQueues() throws Exception {
    final String testAddress = "testAddress";
    final String queueName1 = "queue1";
    final String queueName2 = "queue2";
    final String queueName3 = "queue3";
    CoreQueueConfiguration queue1 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName1);
    CoreQueueConfiguration queue2 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName2);
    CoreQueueConfiguration queue3 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName3);
    List<CoreQueueConfiguration> queueConfs = new ArrayList<>();
    queueConfs.add(queue1);
    queueConfs.add(queue2);
    queueConfs.add(queue3);
    configuration.setQueueConfigurations(queueConfs);
    ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(configuration, false));
    server.start();
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = addClientSession(sf.createSession(false, true, true));
    try {
        session.createQueue(testAddress, queueName1, "", false);
        Assert.fail("Should throw exception");
    } catch (ActiveMQQueueExistsException se) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
    try {
        session.createQueue(testAddress, queueName2, null, false);
        Assert.fail("Should throw exception");
    } catch (ActiveMQQueueExistsException se) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
    try {
        session.createQueue(testAddress, queueName3, null, false);
        Assert.fail("Should throw exception");
    } catch (ActiveMQQueueExistsException se) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
}
Also used : 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) ArrayList(java.util.ArrayList) ActiveMQQueueExistsException(org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException) CoreQueueConfiguration(org.apache.activemq.artemis.core.config.CoreQueueConfiguration) 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 9 with ActiveMQQueueExistsException

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

the class ArtemisBrokerWrapper method makeSureQueueExists.

public void makeSureQueueExists(String qname) throws Exception {
    synchronized (testQueues) {
        SimpleString coreQ = testQueues.get(qname);
        if (coreQ == null) {
            coreQ = new SimpleString(qname);
            try {
                this.server.createQueue(coreQ, RoutingType.MULTICAST, coreQ, null, false, false);
                testQueues.put(qname, coreQ);
            } catch (ActiveMQQueueExistsException e) {
            // ignore
            }
        }
    }
}
Also used : ActiveMQQueueExistsException(org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString)

Example 10 with ActiveMQQueueExistsException

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

the class MQTTSubscriptionManager method findOrCreateQueue.

private Queue findOrCreateQueue(BindingQueryResult bindingQueryResult, AddressInfo addressInfo, SimpleString queue, int qos) throws Exception {
    if (addressInfo.getRoutingTypes().contains(RoutingType.MULTICAST)) {
        return session.getServerSession().createQueue(addressInfo.getName(), queue, RoutingType.MULTICAST, managementFilter, false, MQTTUtil.DURABLE_MESSAGES && qos >= 0, false);
    }
    if (addressInfo.getRoutingTypes().contains(RoutingType.ANYCAST)) {
        if (!bindingQueryResult.getQueueNames().isEmpty()) {
            SimpleString name = null;
            for (SimpleString qName : bindingQueryResult.getQueueNames()) {
                if (name == null) {
                    name = qName;
                } else if (qName.equals(addressInfo.getName())) {
                    name = qName;
                }
            }
            return session.getServer().locateQueue(name);
        } else {
            try {
                return session.getServerSession().createQueue(addressInfo.getName(), addressInfo.getName(), RoutingType.ANYCAST, managementFilter, false, MQTTUtil.DURABLE_MESSAGES && qos >= 0, false);
            } catch (ActiveMQQueueExistsException e) {
                return session.getServer().locateQueue(addressInfo.getName());
            }
        }
    }
    Set<RoutingType> routingTypeSet = new HashSet();
    routingTypeSet.add(RoutingType.MULTICAST);
    routingTypeSet.add(RoutingType.ANYCAST);
    throw ActiveMQMessageBundle.BUNDLE.invalidRoutingTypeForAddress(addressInfo.getRoutingType(), addressInfo.getName().toString(), routingTypeSet);
}
Also used : ActiveMQQueueExistsException(org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) RoutingType(org.apache.activemq.artemis.api.core.RoutingType) HashSet(java.util.HashSet)

Aggregations

ActiveMQQueueExistsException (org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException)16 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)13 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)7 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)5 InvalidDestinationException (javax.jms.InvalidDestinationException)4 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)4 RoutingType (org.apache.activemq.artemis.api.core.RoutingType)3 AddressQuery (org.apache.activemq.artemis.api.core.client.ClientSession.AddressQuery)3 QueueQueryResult (org.apache.activemq.artemis.core.server.QueueQueryResult)3 IllegalStateException (javax.jms.IllegalStateException)2 ActiveMQAddressExistsException (org.apache.activemq.artemis.api.core.ActiveMQAddressExistsException)2 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)2 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)2 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)2 CoreQueueConfiguration (org.apache.activemq.artemis.core.config.CoreQueueConfiguration)2 AddressQueryResult (org.apache.activemq.artemis.core.server.AddressQueryResult)2 BindingQueryResult (org.apache.activemq.artemis.core.server.BindingQueryResult)2 CompositeAddress (org.apache.activemq.artemis.utils.CompositeAddress)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1