Search in sources :

Example 6 with AddressInfo

use of org.apache.activemq.artemis.core.server.impl.AddressInfo in project activemq-artemis by apache.

the class MQTTSubscriptionManager method createQueueForSubscription.

/**
 * Creates a Queue if it doesn't already exist, based on a topic and address.  Returning the queue name.
 */
private Queue createQueueForSubscription(String address, int qos) throws Exception {
    // Check to see if a subscription queue already exists.
    SimpleString queue = getQueueNameForTopic(address);
    Queue q = session.getServer().locateQueue(queue);
    // The queue does not exist so we need to create it.
    if (q == null) {
        SimpleString sAddress = SimpleString.toSimpleString(address);
        // Check we can auto create queues.
        BindingQueryResult bindingQueryResult = session.getServerSession().executeBindingQuery(sAddress);
        if (!bindingQueryResult.isAutoCreateQueues()) {
            throw ActiveMQMessageBundle.BUNDLE.noSuchQueue(sAddress);
        }
        // Check that the address exists, if not we try to auto create it.
        AddressInfo addressInfo = session.getServerSession().getAddress(sAddress);
        if (addressInfo == null) {
            if (!bindingQueryResult.isAutoCreateAddresses()) {
                throw ActiveMQMessageBundle.BUNDLE.addressDoesNotExist(SimpleString.toSimpleString(address));
            }
            addressInfo = session.getServerSession().createAddress(SimpleString.toSimpleString(address), RoutingType.MULTICAST, true);
        }
        return findOrCreateQueue(bindingQueryResult, addressInfo, queue, qos);
    }
    return q;
}
Also used : BindingQueryResult(org.apache.activemq.artemis.core.server.BindingQueryResult) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Queue(org.apache.activemq.artemis.core.server.Queue) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo)

Example 7 with AddressInfo

use of org.apache.activemq.artemis.core.server.impl.AddressInfo in project activemq-artemis by apache.

the class OpenWireConnection method addDestination.

public void addDestination(DestinationInfo info) throws Exception {
    boolean created = false;
    ActiveMQDestination dest = info.getDestination();
    if (!protocolManager.isSupportAdvisory() && AdvisorySupport.isAdvisoryTopic(dest)) {
        return;
    }
    SimpleString qName = SimpleString.toSimpleString(dest.getPhysicalName());
    if (server.locateQueue(qName) == null) {
        AddressSettings addressSettings = server.getAddressSettingsRepository().getMatch(dest.getPhysicalName());
        AddressInfo addressInfo = new AddressInfo(qName, dest.isTopic() ? RoutingType.MULTICAST : RoutingType.ANYCAST);
        if (AdvisorySupport.isAdvisoryTopic(dest) && protocolManager.isSuppressInternalManagementObjects()) {
            addressInfo.setInternal(true);
        }
        if (dest.isQueue() && (addressSettings.isAutoCreateQueues() || dest.isTemporary())) {
            try {
                internalSession.createQueue(addressInfo, qName, null, dest.isTemporary(), !dest.isTemporary(), !dest.isTemporary());
                created = true;
            } catch (ActiveMQQueueExistsException exists) {
            // The queue may have been created by another thread in the mean time.  Catch and do nothing.
            }
        } else if (dest.isTopic() && (addressSettings.isAutoCreateAddresses() || dest.isTemporary())) {
            try {
                internalSession.createAddress(addressInfo, !dest.isTemporary());
                created = true;
            } catch (ActiveMQAddressExistsException exists) {
            // The address may have been created by another thread in the mean time.  Catch and do nothing.
            }
        }
    }
    if (dest.isTemporary()) {
        // Openwire needs to store the DestinationInfo in order to send
        // Advisory messages to clients
        this.state.addTempDestination(info);
    }
    if (created && !AdvisorySupport.isAdvisoryTopic(dest)) {
        AMQConnectionContext context = getContext();
        DestinationInfo advInfo = new DestinationInfo(context.getConnectionId(), DestinationInfo.ADD_OPERATION_TYPE, dest);
        ActiveMQTopic topic = AdvisorySupport.getDestinationAdvisoryTopic(dest);
        protocolManager.fireAdvisory(context, topic, advInfo);
    }
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) DestinationInfo(org.apache.activemq.command.DestinationInfo) ActiveMQTopic(org.apache.activemq.command.ActiveMQTopic) ActiveMQAddressExistsException(org.apache.activemq.artemis.api.core.ActiveMQAddressExistsException) AMQConnectionContext(org.apache.activemq.artemis.core.protocol.openwire.amq.AMQConnectionContext) ActiveMQQueueExistsException(org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo)

Example 8 with AddressInfo

use of org.apache.activemq.artemis.core.server.impl.AddressInfo in project activemq-artemis by apache.

the class AMQConsumer method createTopicSubscription.

private SimpleString createTopicSubscription(boolean isDurable, String clientID, String physicalName, String subscriptionName, SimpleString selector, SimpleString address) throws Exception {
    SimpleString queueName;
    AddressInfo addressInfo = session.getCoreServer().getAddressInfo(address);
    if (addressInfo != null) {
        addressInfo.addRoutingType(RoutingType.MULTICAST);
    } else {
        addressInfo = new AddressInfo(address, RoutingType.MULTICAST);
    }
    addressInfo.setInternal(internalAddress);
    if (isDurable) {
        queueName = org.apache.activemq.artemis.jms.client.ActiveMQDestination.createQueueNameForSubscription(true, clientID, subscriptionName);
        QueueQueryResult result = session.getCoreSession().executeQueueQuery(queueName);
        if (result.isExists()) {
            // Already exists
            if (result.getConsumerCount() > 0) {
                throw new IllegalStateException("Cannot create a subscriber on the durable subscription since it already has subscriber(s)");
            }
            SimpleString oldFilterString = result.getFilterString();
            boolean selectorChanged = selector == null && oldFilterString != null || oldFilterString == null && selector != null || oldFilterString != null && selector != null && !oldFilterString.equals(selector);
            SimpleString oldTopicName = result.getAddress();
            boolean topicChanged = !oldTopicName.equals(address);
            if (selectorChanged || topicChanged) {
                // Delete the old durable sub
                session.getCoreSession().deleteQueue(queueName);
                // Create the new one
                session.getCoreSession().createQueue(addressInfo, queueName, selector, false, true);
            }
        } else {
            session.getCoreSession().createQueue(addressInfo, queueName, selector, false, true);
        }
    } else {
        queueName = new SimpleString(UUID.randomUUID().toString());
        session.getCoreSession().createQueue(addressInfo, queueName, selector, true, false);
    }
    return queueName;
}
Also used : SimpleString(org.apache.activemq.artemis.api.core.SimpleString) QueueQueryResult(org.apache.activemq.artemis.core.server.QueueQueryResult) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo)

Example 9 with AddressInfo

use of org.apache.activemq.artemis.core.server.impl.AddressInfo in project activemq-artemis by apache.

the class FindDestinationTest method testFindTopic.

@Test
public void testFindTopic() throws Exception {
    server.getActiveMQServer().addAddressInfo(new AddressInfo(SimpleString.toSimpleString("testTopic"), RoutingType.MULTICAST));
    server.getActiveMQServer().createQueue(new SimpleString("testTopic"), RoutingType.MULTICAST, new SimpleString("testTopic"), null, false, false);
    ClientRequest request = new ClientRequest(TestPortProvider.generateURL("/topics/testTopic"));
    ClientResponse<?> response = request.head();
    response.releaseConnection();
    Assert.assertEquals(200, response.getStatus());
    Link sender = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), response, "create");
    Link subscriptions = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), response, "pull-subscriptions");
    ClientResponse<?> res = subscriptions.request().post();
    Assert.assertEquals(201, res.getStatus());
    Link sub1 = res.getLocationLink();
    res.releaseConnection();
    Assert.assertNotNull(sub1);
    Link consumeNext1 = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), res, "consume-next");
    Assert.assertNotNull(consumeNext1);
    System.out.println("consumeNext1: " + consumeNext1);
    res = subscriptions.request().post();
    Assert.assertEquals(201, res.getStatus());
    Link sub2 = res.getLocationLink();
    res.releaseConnection();
    Assert.assertNotNull(sub2);
    Link consumeNext2 = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), res, "consume-next");
    Assert.assertNotNull(consumeNext1);
    res = sender.request().body("text/plain", Integer.toString(1)).post();
    res.releaseConnection();
    Assert.assertEquals(201, res.getStatus());
    res = sender.request().body("text/plain", Integer.toString(2)).post();
    res.releaseConnection();
    Assert.assertEquals(201, res.getStatus());
    res = consumeNext1.request().post(String.class);
    Assert.assertEquals(200, res.getStatus());
    Assert.assertEquals("1", res.getEntity(String.class));
    res.releaseConnection();
    consumeNext1 = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), res, "consume-next");
    res = consumeNext1.request().post(String.class);
    Assert.assertEquals(200, res.getStatus());
    Assert.assertEquals("2", res.getEntity(String.class));
    res.releaseConnection();
    res = consumeNext2.request().post(String.class);
    Assert.assertEquals(200, res.getStatus());
    Assert.assertEquals("1", res.getEntity(String.class));
    res.releaseConnection();
    consumeNext2 = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), res, "consume-next");
    res = consumeNext2.request().post(String.class);
    Assert.assertEquals(200, res.getStatus());
    Assert.assertEquals("2", res.getEntity(String.class));
    res.releaseConnection();
    res = sub1.request().delete();
    res.releaseConnection();
    Assert.assertEquals(204, res.getStatus());
    res = sub2.request().delete();
    res.releaseConnection();
    Assert.assertEquals(204, res.getStatus());
}
Also used : SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientRequest(org.jboss.resteasy.client.ClientRequest) Link(org.jboss.resteasy.spi.Link) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo) Test(org.junit.Test)

Example 10 with AddressInfo

use of org.apache.activemq.artemis.core.server.impl.AddressInfo in project activemq-artemis by apache.

the class JMSServerManagerImpl method internalCreateTopic.

private synchronized boolean internalCreateTopic(final String address, final String topicName, final boolean autoCreated) throws Exception {
    if (topics.get(address) != null) {
        return false;
    } else {
        // Create the JMS topic with topicName as the logical name of the topic *and* address as its address
        ActiveMQTopic activeMQTopic = ActiveMQDestination.createTopic(address, topicName);
        server.addOrUpdateAddressInfo(new AddressInfo(SimpleString.toSimpleString(activeMQTopic.getAddress()), RoutingType.MULTICAST));
        topics.put(address, activeMQTopic);
        this.recoverregistryBindings(topicName, PersistedType.Topic);
        return true;
    }
}
Also used : ActiveMQTopic(org.apache.activemq.artemis.jms.client.ActiveMQTopic) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo)

Aggregations

AddressInfo (org.apache.activemq.artemis.core.server.impl.AddressInfo)116 Test (org.junit.Test)89 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)73 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)32 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)24 AmqpConnection (org.apache.activemq.transport.amqp.client.AmqpConnection)23 AmqpSession (org.apache.activemq.transport.amqp.client.AmqpSession)23 AmqpClient (org.apache.activemq.transport.amqp.client.AmqpClient)22 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)21 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)19 AmqpMessage (org.apache.activemq.transport.amqp.client.AmqpMessage)18 AmqpReceiver (org.apache.activemq.transport.amqp.client.AmqpReceiver)17 JsonObject (javax.json.JsonObject)16 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)16 ActiveMQServerControl (org.apache.activemq.artemis.api.core.management.ActiveMQServerControl)16 JsonArray (javax.json.JsonArray)15 Queue (org.apache.activemq.artemis.core.server.Queue)15 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)13 Configuration (org.apache.activemq.artemis.core.config.Configuration)12 Session (javax.jms.Session)11