Search in sources :

Example 1 with MessageDestination

use of org.apache.qpid.server.message.MessageDestination in project qpid-broker-j by apache.

the class MessageConverter_1_0_to_v0_10 method setExchangeAndRoutingKeyOnDeliveryProperties.

private void setExchangeAndRoutingKeyOnDeliveryProperties(final DeliveryProperties deliveryProps, final MessageMetaData_1_0.MessageHeader_1_0 origHeader, final NamedAddressSpace addressSpace) {
    final String to = origHeader.getTo();
    final String subject = origHeader.getSubject() == null ? "" : origHeader.getSubject();
    final String exchangeName;
    final String routingKey;
    if (to != null && !"".equals(to)) {
        DestinationAddress destinationAddress = new DestinationAddress(addressSpace, to);
        MessageDestination messageDestination = destinationAddress.getMessageDestination();
        if (messageDestination instanceof Queue) {
            exchangeName = "";
            routingKey = messageDestination.getName();
        } else if (messageDestination instanceof Exchange) {
            exchangeName = messageDestination.getName();
            routingKey = "".equals(destinationAddress.getRoutingKey()) ? subject : destinationAddress.getRoutingKey();
        } else {
            exchangeName = "";
            routingKey = to;
        }
    } else {
        exchangeName = "";
        routingKey = subject;
    }
    deliveryProps.setRoutingKey(ensureStr8("to' or 'subject", routingKey));
    deliveryProps.setExchange(ensureStr8("to", exchangeName));
}
Also used : Exchange(org.apache.qpid.server.model.Exchange) MessageDestination(org.apache.qpid.server.message.MessageDestination) Queue(org.apache.qpid.server.model.Queue) DestinationAddress(org.apache.qpid.server.model.DestinationAddress)

Example 2 with MessageDestination

use of org.apache.qpid.server.message.MessageDestination in project qpid-broker-j by apache.

the class AbstractVirtualHost method publishMessage.

@Override
public int publishMessage(@Param(name = "message") final ManageableMessage message) {
    final String address = message.getAddress();
    MessageDestination destination = address == null ? getDefaultDestination() : getAttainedMessageDestination(address);
    if (destination == null) {
        destination = getDefaultDestination();
    }
    final AMQMessageHeader header = new MessageHeaderImpl(message);
    Serializable body = null;
    Object messageContent = message.getContent();
    if (messageContent != null) {
        if (messageContent instanceof Map || messageContent instanceof List) {
            if (message.getMimeType() != null || message.getEncoding() != null) {
                throw new IllegalArgumentException("If the message content is provided as map or list, the mime type and encoding must be left unset");
            }
            body = (Serializable) messageContent;
        } else if (messageContent instanceof String) {
            String contentTransferEncoding = message.getContentTransferEncoding();
            if ("base64".equalsIgnoreCase(contentTransferEncoding)) {
                body = Strings.decodeBase64((String) messageContent);
            } else if (contentTransferEncoding == null || contentTransferEncoding.trim().equals("") || contentTransferEncoding.trim().equalsIgnoreCase("identity")) {
                String mimeType = message.getMimeType();
                if (mimeType != null && !(mimeType = mimeType.trim().toLowerCase()).equals("")) {
                    if (!(mimeType.startsWith("text/") || Arrays.asList("application/json", "application/xml").contains(mimeType))) {
                        throw new IllegalArgumentException(message.getMimeType() + " is invalid as a MIME type for this message. " + "Only MIME types of the text type can be used if a string is supplied as the content");
                    } else if (mimeType.matches(".*;\\s*charset\\s*=.*")) {
                        throw new IllegalArgumentException(message.getMimeType() + " is invalid as a MIME type for this message. " + "If a string is supplied as the content, the MIME type must not include a charset parameter");
                    }
                }
                body = (String) messageContent;
            } else {
                throw new IllegalArgumentException("contentTransferEncoding value '" + contentTransferEncoding + "' is invalid.  The only valid values are base64 and identity");
            }
        } else {
            throw new IllegalArgumentException("The message content (if present) can only be a string, map or list");
        }
    }
    InternalMessage internalMessage = InternalMessage.createMessage(getMessageStore(), header, body, message.isPersistent(), address);
    AutoCommitTransaction txn = new AutoCommitTransaction(getMessageStore());
    final InstanceProperties instanceProperties = new InstanceProperties() {

        @Override
        public Object getProperty(final Property prop) {
            switch(prop) {
                case EXPIRATION:
                    Date expiration = message.getExpiration();
                    return expiration == null ? 0 : expiration.getTime();
                case IMMEDIATE:
                    return false;
                case PERSISTENT:
                    return message.isPersistent();
                case MANDATORY:
                    return false;
                case REDELIVERED:
                    return false;
                default:
                    return null;
            }
        }
    };
    final RoutingResult<InternalMessage> result = destination.route(internalMessage, address, instanceProperties);
    return result.send(txn, null);
}
Also used : AutoCommitTransaction(org.apache.qpid.server.txn.AutoCommitTransaction) Serializable(java.io.Serializable) MessageDestination(org.apache.qpid.server.message.MessageDestination) InternalMessage(org.apache.qpid.server.message.internal.InternalMessage) InstanceProperties(org.apache.qpid.server.message.InstanceProperties) AMQMessageHeader(org.apache.qpid.server.message.AMQMessageHeader) Date(java.util.Date) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Collections.newSetFromMap(java.util.Collections.newSetFromMap)

Example 3 with MessageDestination

use of org.apache.qpid.server.message.MessageDestination in project qpid-broker-j by apache.

the class AbstractExchange method performDelete.

private void performDelete() {
    if (_closed.compareAndSet(false, true)) {
        performDeleteTasks();
        for (Binding b : _bindings) {
            final MessageDestination messageDestination = getAttainedMessageDestination(b.getDestination());
            if (messageDestination != null) {
                messageDestination.linkRemoved(this, b);
            }
        }
        for (MessageSender sender : _linkedSenders.keySet()) {
            sender.destinationRemoved(this);
        }
        if (_alternateBindingDestination != null) {
            _alternateBindingDestination.removeReference(AbstractExchange.this);
        }
        getEventLogger().message(_logSubject, ExchangeMessages.DELETED());
    }
}
Also used : Binding(org.apache.qpid.server.model.Binding) AlternateBinding(org.apache.qpid.server.model.AlternateBinding) MessageDestination(org.apache.qpid.server.message.MessageDestination) MessageSender(org.apache.qpid.server.message.MessageSender)

Example 4 with MessageDestination

use of org.apache.qpid.server.message.MessageDestination in project qpid-broker-j by apache.

the class AbstractExchange method route.

@Override
public <M extends ServerMessage<? extends StorableMessageMetaData>> RoutingResult<M> route(final M message, final String routingAddress, final InstanceProperties instanceProperties) {
    if (_virtualHost.getState() != State.ACTIVE) {
        throw new VirtualHostUnavailableException(this._virtualHost);
    }
    final RoutingResult<M> routingResult = new RoutingResult<>(message);
    Map<AbstractExchange<?>, Set<String>> currentThreadMap = CURRENT_ROUTING.get();
    boolean topLevel = currentThreadMap == null;
    try {
        if (topLevel) {
            currentThreadMap = new HashMap<>();
            CURRENT_ROUTING.set(currentThreadMap);
        }
        Set<String> existingRoutes = currentThreadMap.get(this);
        if (existingRoutes == null) {
            currentThreadMap.put(this, Collections.singleton(routingAddress));
        } else if (existingRoutes.contains(routingAddress)) {
            return routingResult;
        } else {
            existingRoutes = new HashSet<>(existingRoutes);
            existingRoutes.add(routingAddress);
            currentThreadMap.put(this, existingRoutes);
        }
        _receivedMessageCount.incrementAndGet();
        _receivedMessageSize.addAndGet(message.getSizeIncludingHeader());
        doRoute(message, routingAddress, instanceProperties, routingResult);
        if (!routingResult.hasRoutes()) {
            MessageDestination alternateBindingDestination = getAlternateBindingDestination();
            if (alternateBindingDestination != null) {
                routingResult.add(alternateBindingDestination.route(message, routingAddress, instanceProperties));
            }
        }
        if (routingResult.hasRoutes()) {
            _routedMessageCount.incrementAndGet();
            _routedMessageSize.addAndGet(message.getSizeIncludingHeader());
        } else {
            _droppedMessageCount.incrementAndGet();
            _droppedMessageSize.addAndGet(message.getSizeIncludingHeader());
        }
        return routingResult;
    } finally {
        if (topLevel) {
            CURRENT_ROUTING.set(null);
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) MessageDestination(org.apache.qpid.server.message.MessageDestination) RoutingResult(org.apache.qpid.server.message.RoutingResult) VirtualHostUnavailableException(org.apache.qpid.server.virtualhost.VirtualHostUnavailableException) HashSet(java.util.HashSet)

Example 5 with MessageDestination

use of org.apache.qpid.server.message.MessageDestination in project qpid-broker-j by apache.

the class AbstractExchange method onOpen.

@Override
protected void onOpen() {
    super.onOpen();
    final ConfiguredDerivedMethodAttribute<Exchange<?>, Collection<Binding>> durableBindingsAttribute = (ConfiguredDerivedMethodAttribute<Exchange<?>, Collection<Binding>>) getModel().getTypeRegistry().getAttributeTypes(getTypeClass()).get(DURABLE_BINDINGS);
    final Collection<Binding> bindings = durableBindingsAttribute.convertValue(getActualAttributes().get(DURABLE_BINDINGS), this);
    if (bindings != null) {
        _bindings.addAll(bindings);
        for (Binding b : _bindings) {
            final MessageDestination messageDestination = getOpenedMessageDestination(b.getDestination());
            if (messageDestination != null) {
                Map<String, Object> arguments = b.getArguments() == null ? Collections.emptyMap() : b.getArguments();
                onBind(new BindingIdentifier(b.getBindingKey(), messageDestination), arguments);
                messageDestination.linkAdded(this, b);
            }
        }
    }
    if (getLifetimePolicy() == LifetimePolicy.DELETE_ON_CREATING_LINK_CLOSE) {
        if (_creatingLinkInfo != null) {
            final LinkModel link;
            if (_creatingLinkInfo.isSendingLink()) {
                link = _virtualHost.getSendingLink(_creatingLinkInfo.getRemoteContainerId(), _creatingLinkInfo.getLinkName());
            } else {
                link = _virtualHost.getReceivingLink(_creatingLinkInfo.getRemoteContainerId(), _creatingLinkInfo.getLinkName());
            }
            addLifetimeConstraint(link);
        } else {
            throw new IllegalArgumentException("Exchanges created with a lifetime policy of " + getLifetimePolicy() + " must be created from a AMQP 1.0 link.");
        }
    }
    if (getAlternateBinding() != null) {
        String alternateDestination = getAlternateBinding().getDestination();
        _alternateBindingDestination = getOpenedMessageDestination(alternateDestination);
        if (_alternateBindingDestination != null) {
            _alternateBindingDestination.addReference(this);
        } else {
            LOGGER.warn("Cannot find alternate binding destination '{}' for exchange '{}'", alternateDestination, toString());
        }
    }
    getEventLogger().message(ExchangeMessages.CREATED(getType(), getName(), isDurable()));
}
Also used : Binding(org.apache.qpid.server.model.Binding) AlternateBinding(org.apache.qpid.server.model.AlternateBinding) MessageDestination(org.apache.qpid.server.message.MessageDestination) LinkModel(org.apache.qpid.server.protocol.LinkModel) Exchange(org.apache.qpid.server.model.Exchange) ConfiguredDerivedMethodAttribute(org.apache.qpid.server.model.ConfiguredDerivedMethodAttribute) Collection(java.util.Collection) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject)

Aggregations

MessageDestination (org.apache.qpid.server.message.MessageDestination)40 DestinationAddress (org.apache.qpid.server.model.DestinationAddress)11 Queue (org.apache.qpid.server.model.Queue)11 Exchange (org.apache.qpid.server.model.Exchange)10 HashMap (java.util.HashMap)6 AbstractConfiguredObject (org.apache.qpid.server.model.AbstractConfiguredObject)6 Map (java.util.Map)5 AlternateBinding (org.apache.qpid.server.model.AlternateBinding)5 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)5 AccessControlException (java.security.AccessControlException)4 HashSet (java.util.HashSet)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 RoutingResult (org.apache.qpid.server.message.RoutingResult)4 Binding (org.apache.qpid.server.model.Binding)4 NamedAddressSpace (org.apache.qpid.server.model.NamedAddressSpace)4 Set (java.util.Set)3 TopicExchangeResult (org.apache.qpid.server.exchange.topic.TopicExchangeResult)3 InstanceProperties (org.apache.qpid.server.message.InstanceProperties)3 ReplyTo (org.apache.qpid.server.protocol.v0_10.transport.ReplyTo)3 AMQShortString (org.apache.qpid.server.protocol.v0_8.AMQShortString)3