Search in sources :

Example 41 with ActiveMQException

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

the class ActiveMQSession method deleteTemporaryTopic.

public void deleteTemporaryTopic(final ActiveMQDestination tempTopic) throws JMSException {
    if (!tempTopic.isTemporary()) {
        throw new InvalidDestinationException("Not a temporary topic " + tempTopic);
    }
    try {
        AddressQuery response = session.addressQuery(tempTopic.getSimpleAddress());
        if (!response.isExists()) {
            throw new InvalidDestinationException("Cannot delete temporary topic " + tempTopic.getName() + " does not exist");
        }
        if (response.getQueueNames().size() > 1) {
            throw new IllegalStateException("Cannot delete temporary topic " + tempTopic.getName() + " since it has subscribers");
        }
        SimpleString address = tempTopic.getSimpleAddress();
        session.deleteQueue(address);
        connection.removeTemporaryQueue(address);
    } catch (ActiveMQException e) {
        throw JMSExceptionHelper.convertFromActiveMQException(e);
    }
}
Also used : IllegalStateException(javax.jms.IllegalStateException) AddressQuery(org.apache.activemq.artemis.api.core.client.ClientSession.AddressQuery) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) InvalidDestinationException(javax.jms.InvalidDestinationException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString)

Example 42 with ActiveMQException

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

the class ActiveMQSession method createTemporaryTopic.

@Override
public TemporaryTopic createTemporaryTopic() throws JMSException {
    // As per spec. section 4.11
    if (sessionType == ActiveMQSession.TYPE_QUEUE_SESSION) {
        throw new IllegalStateException("Cannot create a temporary topic on a QueueSession");
    }
    try {
        ActiveMQTemporaryTopic topic = ActiveMQDestination.createTemporaryTopic(this);
        SimpleString simpleAddress = topic.getSimpleAddress();
        // We create a dummy subscription on the topic, that never receives messages - this is so we can perform JMS
        // checks when routing messages to a topic that
        // does not exist - otherwise we would not be able to distinguish from a non existent topic and one with no
        // subscriptions - core has no notion of a topic
        session.createTemporaryQueue(simpleAddress, simpleAddress, ActiveMQSession.REJECTING_FILTER);
        connection.addTemporaryQueue(simpleAddress);
        return topic;
    } catch (ActiveMQException e) {
        throw JMSExceptionHelper.convertFromActiveMQException(e);
    }
}
Also used : IllegalStateException(javax.jms.IllegalStateException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString)

Example 43 with ActiveMQException

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

the class ActiveMQSession method createConsumer.

private ActiveMQMessageConsumer createConsumer(final ActiveMQDestination dest, final String subscriptionName, String selectorString, final boolean noLocal, ConsumerDurability durability) throws JMSException {
    try {
        selectorString = "".equals(selectorString) ? null : selectorString;
        if (noLocal) {
            connection.setHasNoLocal();
            String filter;
            if (connection.getClientID() != null) {
                filter = ActiveMQConnection.CONNECTION_ID_PROPERTY_NAME.toString() + "<>'" + connection.getClientID() + "'";
            } else {
                filter = ActiveMQConnection.CONNECTION_ID_PROPERTY_NAME.toString() + "<>'" + connection.getUID() + "'";
            }
            if (selectorString != null) {
                selectorString += " AND " + filter;
            } else {
                selectorString = filter;
            }
        }
        SimpleString coreFilterString = null;
        if (selectorString != null) {
            coreFilterString = new SimpleString(SelectorTranslator.convertToActiveMQFilterString(selectorString));
        }
        ClientConsumer consumer;
        SimpleString autoDeleteQueueName = null;
        if (dest.isQueue()) {
            AddressQuery response = session.addressQuery(dest.getSimpleAddress());
            /* The address query will send back exists=true even if the node only has a REMOTE binding for the destination.
             * Therefore, we must check if the queue names list contains the exact name of the address to know whether or
             * not a LOCAL binding for the address exists. If no LOCAL binding exists then it should be created here.
             */
            if (!response.isExists() || !response.getQueueNames().contains(dest.getSimpleAddress())) {
                if (response.isAutoCreateQueues()) {
                    try {
                        createQueue(dest, RoutingType.ANYCAST, dest.getSimpleAddress(), null, true, true, response.getDefaultMaxConsumers(), response.isDefaultPurgeOnNoConsumers(), response.isDefaultExclusive(), response.isDefaultLastValueQueue());
                    } catch (ActiveMQQueueExistsException e) {
                    // The queue was created by another client/admin between the query check and send create queue packet
                    }
                } else {
                    throw new InvalidDestinationException("Destination " + dest.getName() + " does not exist");
                }
            }
            connection.addKnownDestination(dest.getSimpleAddress());
            consumer = session.createConsumer(dest.getSimpleAddress(), coreFilterString, false);
        } else {
            AddressQuery response = session.addressQuery(dest.getSimpleAddress());
            if (!response.isExists()) {
                if (response.isAutoCreateAddresses()) {
                    session.createAddress(dest.getSimpleAddress(), RoutingType.MULTICAST, true);
                } else {
                    throw new InvalidDestinationException("Topic " + dest.getName() + " does not exist");
                }
            }
            connection.addKnownDestination(dest.getSimpleAddress());
            SimpleString queueName;
            if (subscriptionName == null) {
                if (durability != ConsumerDurability.NON_DURABLE)
                    throw new RuntimeException("Subscription name cannot be null for durable topic consumer");
                // Non durable sub
                queueName = new SimpleString(UUID.randomUUID().toString());
                createTemporaryQueue(dest, RoutingType.MULTICAST, queueName, coreFilterString, response.getDefaultMaxConsumers(), response.isDefaultPurgeOnNoConsumers(), response.isDefaultExclusive(), response.isDefaultLastValueQueue());
                consumer = session.createConsumer(queueName, null, false);
                autoDeleteQueueName = queueName;
            } else {
                // Durable sub
                if (durability != ConsumerDurability.DURABLE)
                    throw new RuntimeException("Subscription name must be null for non-durable topic consumer");
                if (connection.getClientID() == null) {
                    throw new IllegalStateException("Cannot create durable subscription - client ID has not been set");
                }
                if (dest.isTemporary()) {
                    throw new InvalidDestinationException("Cannot create a durable subscription on a temporary topic");
                }
                queueName = ActiveMQDestination.createQueueNameForSubscription(true, connection.getClientID(), subscriptionName);
                QueueQuery subResponse = session.queueQuery(queueName);
                if (!subResponse.isExists()) {
                    // durable subscription queues are not technically considered to be auto-created
                    createQueue(dest, RoutingType.MULTICAST, queueName, coreFilterString, true, false, response.getDefaultMaxConsumers(), response.isDefaultPurgeOnNoConsumers(), response.isDefaultExclusive(), response.isDefaultLastValueQueue());
                } else {
                    // Already exists
                    if (subResponse.getConsumerCount() > 0) {
                        throw new IllegalStateException("Cannot create a subscriber on the durable subscription since it already has subscriber(s)");
                    }
                    // From javax.jms.Session Javadoc (and also JMS 1.1 6.11.1):
                    // A client can change an existing durable subscription by
                    // creating a durable TopicSubscriber with the same name and
                    // a new topic and/or message selector.
                    // Changing a durable subscriber is equivalent to unsubscribing
                    // (deleting) the old one and creating a new one.
                    SimpleString oldFilterString = subResponse.getFilterString();
                    boolean selectorChanged = coreFilterString == null && oldFilterString != null || oldFilterString == null && coreFilterString != null || oldFilterString != null && coreFilterString != null && !oldFilterString.equals(coreFilterString);
                    SimpleString oldTopicName = subResponse.getAddress();
                    boolean topicChanged = !oldTopicName.equals(dest.getSimpleAddress());
                    if (selectorChanged || topicChanged) {
                        // Delete the old durable sub
                        session.deleteQueue(queueName);
                        // Create the new one
                        createQueue(dest, RoutingType.MULTICAST, queueName, coreFilterString, true, false, response.getDefaultMaxConsumers(), response.isDefaultPurgeOnNoConsumers(), response.isDefaultExclusive(), response.isDefaultLastValueQueue());
                    }
                }
                consumer = session.createConsumer(queueName, null, false);
            }
        }
        ActiveMQMessageConsumer jbc = new ActiveMQMessageConsumer(options, connection, this, consumer, noLocal, dest, selectorString, autoDeleteQueueName);
        consumers.add(jbc);
        return jbc;
    } catch (ActiveMQException e) {
        throw JMSExceptionHelper.convertFromActiveMQException(e);
    }
}
Also used : IllegalStateException(javax.jms.IllegalStateException) AddressQuery(org.apache.activemq.artemis.api.core.client.ClientSession.AddressQuery) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQQueueExistsException(org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) InvalidDestinationException(javax.jms.InvalidDestinationException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) QueueQuery(org.apache.activemq.artemis.api.core.client.ClientSession.QueueQuery)

Example 44 with ActiveMQException

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

the class ActiveMQSession method createTemporaryQueue.

@Override
public TemporaryQueue createTemporaryQueue() throws JMSException {
    // As per spec. section 4.11
    if (sessionType == ActiveMQSession.TYPE_TOPIC_SESSION) {
        throw new IllegalStateException("Cannot create a temporary queue using a TopicSession");
    }
    try {
        ActiveMQTemporaryQueue queue = ActiveMQDestination.createTemporaryQueue(this);
        SimpleString simpleAddress = queue.getSimpleAddress();
        session.createTemporaryQueue(simpleAddress, RoutingType.ANYCAST, simpleAddress);
        connection.addTemporaryQueue(simpleAddress);
        return queue;
    } catch (ActiveMQException e) {
        throw JMSExceptionHelper.convertFromActiveMQException(e);
    }
}
Also used : IllegalStateException(javax.jms.IllegalStateException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString)

Example 45 with ActiveMQException

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

the class ActiveMQConnection method setClientID.

@Override
public void setClientID(final String clientID) throws JMSException {
    checkClosed();
    if (this.clientID != null) {
        throw new IllegalStateException("Client id has already been set");
    }
    if (!justCreated) {
        throw new IllegalStateException("setClientID can only be called directly after the connection is created");
    }
    validateClientID(initialSession, clientID);
    this.clientID = clientID;
    try {
        this.addSessionMetaData(initialSession);
    } catch (ActiveMQException e) {
        JMSException ex = new JMSException("Internal error setting metadata jms-client-id");
        ex.setLinkedException(e);
        ex.initCause(e);
        throw ex;
    }
    justCreated = false;
}
Also used : IllegalStateException(javax.jms.IllegalStateException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) JMSException(javax.jms.JMSException)

Aggregations

ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)254 Test (org.junit.Test)139 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