Search in sources :

Example 36 with RoutingType

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

the class RoutingTest method testAnycastMessageRoutingExclusivity.

@Test
public void testAnycastMessageRoutingExclusivity() throws Exception {
    ClientSession sendSession = cf.createSession(false, true, true);
    EnumSet<RoutingType> routingTypes = EnumSet.of(RoutingType.ANYCAST, RoutingType.MULTICAST);
    sendSession.createAddress(addressA, routingTypes, false);
    sendSession.createQueue(addressA, RoutingType.ANYCAST, queueA);
    sendSession.createQueue(addressA, RoutingType.ANYCAST, queueB);
    sendSession.createQueue(addressA, RoutingType.MULTICAST, queueC);
    ClientProducer p = sendSession.createProducer(addressA);
    ClientMessage message = sendSession.createMessage(false);
    message.setRoutingType(RoutingType.ANYCAST);
    p.send(message);
    sendSession.close();
    assertTrue(Wait.waitFor(() -> server.locateQueue(queueA).getMessageCount() + server.locateQueue(queueB).getMessageCount() == 1));
    assertTrue(Wait.waitFor(() -> server.locateQueue(queueC).getMessageCount() == 0));
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) RoutingType(org.apache.activemq.artemis.api.core.RoutingType) Test(org.junit.Test)

Example 37 with RoutingType

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

Example 38 with RoutingType

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

the class AMQSession method checkAutoCreateQueue.

private boolean checkAutoCreateQueue(SimpleString queueName, boolean isTemporary) throws Exception {
    boolean hasQueue = true;
    if (!connection.containsKnownDestination(queueName)) {
        BindingQueryResult bindingQuery = server.bindingQuery(queueName);
        QueueQueryResult queueBinding = server.queueQuery(queueName);
        try {
            if (!queueBinding.isExists()) {
                if (bindingQuery.isAutoCreateQueues()) {
                    SimpleString queueNameToUse = queueName;
                    SimpleString addressToUse = queueName;
                    RoutingType routingTypeToUse = RoutingType.ANYCAST;
                    if (CompositeAddress.isFullyQualified(queueName.toString())) {
                        CompositeAddress compositeAddress = CompositeAddress.getQueueName(queueName.toString());
                        addressToUse = new SimpleString(compositeAddress.getAddress());
                        queueNameToUse = new SimpleString(compositeAddress.getQueueName());
                        if (bindingQuery.getAddressInfo() != null) {
                            routingTypeToUse = bindingQuery.getAddressInfo().getRoutingType();
                        } else {
                            AddressSettings as = server.getAddressSettingsRepository().getMatch(addressToUse.toString());
                            routingTypeToUse = as.getDefaultAddressRoutingType();
                        }
                    }
                    coreSession.createQueue(addressToUse, queueNameToUse, routingTypeToUse, null, isTemporary, true);
                    connection.addKnownDestination(queueName);
                } else {
                    hasQueue = false;
                }
            }
        } catch (ActiveMQQueueExistsException e) {
            // In case another thread created the queue before us but after we did the binding query
            hasQueue = true;
        }
    }
    return hasQueue;
}
Also used : BindingQueryResult(org.apache.activemq.artemis.core.server.BindingQueryResult) CompositeAddress(org.apache.activemq.artemis.utils.CompositeAddress) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) ActiveMQQueueExistsException(org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) QueueQueryResult(org.apache.activemq.artemis.core.server.QueueQueryResult) RoutingType(org.apache.activemq.artemis.api.core.RoutingType)

Example 39 with RoutingType

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

the class VersionedStompFrameHandler method onSend.

public StompFrame onSend(StompFrame frame) {
    StompFrame response = null;
    try {
        connection.validate();
        String destination = getDestination(frame);
        RoutingType routingType = getRoutingType(frame.getHeader(Headers.Send.DESTINATION_TYPE), frame.getHeader(Headers.Send.DESTINATION));
        connection.autoCreateDestinationIfPossible(destination, routingType);
        connection.checkDestination(destination);
        connection.checkRoutingSemantics(destination, routingType);
        String txID = frame.getHeader(Stomp.Headers.TRANSACTION);
        long timestamp = System.currentTimeMillis();
        CoreMessage message = connection.createServerMessage();
        if (routingType != null) {
            message.setRoutingType(routingType);
        }
        message.setTimestamp(timestamp);
        message.setAddress(SimpleString.toSimpleString(destination));
        StompUtils.copyStandardHeadersFromFrameToMessage(frame, message);
        if (frame.hasHeader(Stomp.Headers.CONTENT_LENGTH)) {
            message.setType(Message.BYTES_TYPE);
            message.getBodyBuffer().writeBytes(frame.getBodyAsBytes());
        } else {
            message.setType(Message.TEXT_TYPE);
            String text = frame.getBody();
            message.getBodyBuffer().writeNullableSimpleString(SimpleString.toSimpleString(text));
        }
        connection.sendServerMessage(message, txID);
    } catch (ActiveMQStompException e) {
        response = e.getFrame();
    } catch (Exception e) {
        ActiveMQStompException error = BUNDLE.errorHandleSend(e).setHandler(this);
        response = error.getFrame();
    }
    return response;
}
Also used : SimpleString(org.apache.activemq.artemis.api.core.SimpleString) CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) ICoreMessage(org.apache.activemq.artemis.api.core.ICoreMessage) RoutingType(org.apache.activemq.artemis.api.core.RoutingType)

Example 40 with RoutingType

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

the class StompConnection method autoCreateDestinationIfPossible.

public void autoCreateDestinationIfPossible(String queue, RoutingType routingType) throws ActiveMQStompException {
    ServerSession session = getSession().getCoreSession();
    try {
        SimpleString simpleQueue = SimpleString.toSimpleString(queue);
        if (manager.getServer().getAddressInfo(simpleQueue) == null) {
            AddressSettings addressSettings = manager.getServer().getAddressSettingsRepository().getMatch(queue);
            RoutingType effectiveAddressRoutingType = routingType == null ? addressSettings.getDefaultAddressRoutingType() : routingType;
            if (addressSettings.isAutoCreateAddresses()) {
                session.createAddress(simpleQueue, effectiveAddressRoutingType, true);
            }
            // only auto create the queue if the address is ANYCAST
            if (effectiveAddressRoutingType == RoutingType.ANYCAST && addressSettings.isAutoCreateQueues()) {
                session.createQueue(simpleQueue, simpleQueue, routingType == null ? addressSettings.getDefaultQueueRoutingType() : routingType, null, false, true, true);
            }
        }
    } catch (ActiveMQQueueExistsException e) {
    // ignore
    } catch (Exception e) {
        throw new ActiveMQStompException(e.getMessage(), e).setHandler(frameHandler);
    }
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) ActiveMQQueueExistsException(org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQQueueExistsException(org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException) RoutingType(org.apache.activemq.artemis.api.core.RoutingType)

Aggregations

RoutingType (org.apache.activemq.artemis.api.core.RoutingType)45 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)31 Test (org.junit.Test)15 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)11 AddressInfo (org.apache.activemq.artemis.core.server.impl.AddressInfo)11 HashSet (java.util.HashSet)9 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)9 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)8 QueueQueryResult (org.apache.activemq.artemis.core.server.QueueQueryResult)7 OperationContext (org.apache.activemq.artemis.core.persistence.OperationContext)6 ServerSession (org.apache.activemq.artemis.core.server.ServerSession)6 QueueBinding (org.apache.activemq.artemis.core.postoffice.QueueBinding)5 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)5 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 Semaphore (java.util.concurrent.Semaphore)4 Pair (org.apache.activemq.artemis.api.core.Pair)4 CoreAddressConfiguration (org.apache.activemq.artemis.core.config.CoreAddressConfiguration)4 CoreQueueConfiguration (org.apache.activemq.artemis.core.config.CoreQueueConfiguration)4 Filter (org.apache.activemq.artemis.core.filter.Filter)4 PageSubscription (org.apache.activemq.artemis.core.paging.cursor.PageSubscription)4