Search in sources :

Example 16 with IllegalStateException

use of javax.jms.IllegalStateException 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 17 with IllegalStateException

use of javax.jms.IllegalStateException 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 18 with IllegalStateException

use of javax.jms.IllegalStateException 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 19 with IllegalStateException

use of javax.jms.IllegalStateException 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)

Example 20 with IllegalStateException

use of javax.jms.IllegalStateException 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)

Aggregations

IllegalStateException (javax.jms.IllegalStateException)44 Session (javax.jms.Session)19 Connection (javax.jms.Connection)12 QueueSession (javax.jms.QueueSession)12 Test (org.junit.Test)12 TopicSession (javax.jms.TopicSession)11 XAQueueSession (javax.jms.XAQueueSession)11 XATopicSession (javax.jms.XATopicSession)11 ActiveMQSession (org.apache.activemq.artemis.jms.client.ActiveMQSession)11 JMSException (javax.jms.JMSException)10 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)8 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)8 InvalidDestinationException (javax.jms.InvalidDestinationException)5 IOException (java.io.IOException)4 Queue (javax.jms.Queue)3 QueueBrowser (javax.jms.QueueBrowser)3 TopicSubscriber (javax.jms.TopicSubscriber)3 QueueQuery (org.apache.activemq.artemis.api.core.client.ClientSession.QueueQuery)3 RMQJMSException (com.rabbitmq.jms.util.RMQJMSException)2 TemporaryQueue (javax.jms.TemporaryQueue)2