Search in sources :

Example 16 with Exchange

use of org.apache.qpid.server.model.Exchange in project qpid by apache.

the class QmfManagementAgent method childAdded.

/**
 * ConfigurationChangeListener method called when a child ConfiguredObject is added.
 * <p>
 * This method checks the type of the child ConfiguredObject that has been added and creates the equivalent
 * QMF2 Management Object if one doesn't already exist. In most cases it's a one-to-one mapping, but for
 * Binding for example the Binding child is added to both Queue and Exchange so we only create the Binding
 * QMF2 Management Object once and add the queueRef and exchangeRef reference properties referencing the Queue
 * and Exchange parent Objects respectively, Similarly for Consumer (AKA Subscription).
 * <p>
 * This method is also responsible for raising the appropriate QMF2 Events when Management Objects are created.
 * @param object the parent object that the child is being added to.
 * @param child the child object being added.
 */
@Override
public void childAdded(final ConfiguredObject object, final ConfiguredObject child) {
    if (_log.isDebugEnabled()) {
        _log.debug("childAdded: " + child.getClass().getSimpleName() + "." + child.getName());
    }
    QmfAgentData data = null;
    if (child instanceof Broker) {
        data = new org.apache.qpid.server.qmf2.agentdata.Broker((Broker) child);
    } else if (child instanceof Connection) {
        if (!agentConnection && !_objects.containsKey(child)) {
            // If the parent object is the default vhost set it to null so that the Connection ignores it.
            VirtualHost vhost = (object.getName().equals(_defaultVirtualHost)) ? null : (VirtualHost) object;
            data = new org.apache.qpid.server.qmf2.agentdata.Connection(vhost, (Connection) child);
            _objects.put(child, data);
            // Raise a Client Connect Event.
            _agent.raiseEvent(((org.apache.qpid.server.qmf2.agentdata.Connection) data).createClientConnectEvent());
        }
        // Only ignore the first Connection, which is the one from the Agent.
        agentConnection = false;
    } else if (child instanceof Session) {
        if (!_objects.containsKey(child)) {
            // Get the Connection QmfAgentData so we can get connectionRef.
            QmfAgentData ref = _objects.get(object);
            if (ref != null) {
                data = new org.apache.qpid.server.qmf2.agentdata.Session((Session) child, ref.getObjectId());
                _objects.put(child, data);
            }
        }
    } else if (child instanceof Exchange) {
        if (!_objects.containsKey(child)) {
            // If the parent object is the default vhost set it to null so that the Connection ignores it.
            VirtualHost vhost = (object.getName().equals(_defaultVirtualHost)) ? null : (VirtualHost) object;
            data = new org.apache.qpid.server.qmf2.agentdata.Exchange(vhost, (Exchange) child);
            _objects.put(child, data);
            // Raise an Exchange Declare Event.
            _agent.raiseEvent(((org.apache.qpid.server.qmf2.agentdata.Exchange) data).createExchangeDeclareEvent());
        }
    } else if (child instanceof Queue) {
        if (!_objects.containsKey(child)) {
            // If the parent object is the default vhost set it to null so that the Connection ignores it.
            VirtualHost vhost = (object.getName().equals(_defaultVirtualHost)) ? null : (VirtualHost) object;
            data = new org.apache.qpid.server.qmf2.agentdata.Queue(vhost, (Queue) child);
            _objects.put(child, data);
            // Raise a Queue Declare Event.
            _agent.raiseEvent(((org.apache.qpid.server.qmf2.agentdata.Queue) data).createQueueDeclareEvent());
        }
    } else if (child instanceof Binding) {
        // depending on whether Queue or Exchange was the parent of this addChild() call.
        if (!_objects.containsKey(child)) {
            data = new org.apache.qpid.server.qmf2.agentdata.Binding((Binding) child);
            _objects.put(child, data);
            String eName = ((Binding) child).getExchange().getName();
            if (// Don't send Event for Binding to default direct.
            !eName.equals("<<default>>")) {
                // Raise a Bind Event.
                _agent.raiseEvent(((org.apache.qpid.server.qmf2.agentdata.Binding) data).createBindEvent());
            }
        }
        org.apache.qpid.server.qmf2.agentdata.Binding binding = (org.apache.qpid.server.qmf2.agentdata.Binding) _objects.get(child);
        QmfAgentData ref = _objects.get(object);
        if (ref != null) {
            if (object instanceof Queue) {
                binding.setQueueRef(ref.getObjectId());
            } else if (object instanceof Exchange) {
                binding.setExchangeRef(ref.getObjectId());
            }
        }
    } else if (// AKA Subscription
    child instanceof Consumer) {
        // Session reference depending on whether Queue or Session was the parent of this addChild() call.
        if (!_objects.containsKey(child)) {
            data = new org.apache.qpid.server.qmf2.agentdata.Subscription((Consumer) child);
            _objects.put(child, data);
        }
        org.apache.qpid.server.qmf2.agentdata.Subscription subscription = (org.apache.qpid.server.qmf2.agentdata.Subscription) _objects.get(child);
        QmfAgentData ref = _objects.get(object);
        if (ref != null) {
            if (object instanceof Queue) {
                subscription.setQueueRef(ref.getObjectId(), (Queue) object);
                // Raise a Subscribe Event - N.B. Need to do it *after* we've set the queueRef.
                _agent.raiseEvent(subscription.createSubscribeEvent());
            } else if (object instanceof Session) {
                subscription.setSessionRef(ref.getObjectId());
            }
        }
    }
    try {
        // If we've created new QmfAgentData we register it with the Agent.
        if (data != null) {
            _agent.addObject(data);
        }
    } catch (QmfException qmfe) {
        _log.error("QmfException caught in QmfManagementAgent.addObject()", qmfe);
    }
    child.addChangeListener(this);
}
Also used : Binding(org.apache.qpid.server.model.Binding) Broker(org.apache.qpid.server.model.Broker) Connection(org.apache.qpid.server.model.Connection) Exchange(org.apache.qpid.server.model.Exchange) Consumer(org.apache.qpid.server.model.Consumer) QmfAgentData(org.apache.qpid.qmf2.agent.QmfAgentData) VirtualHost(org.apache.qpid.server.model.VirtualHost) Queue(org.apache.qpid.server.model.Queue) Session(org.apache.qpid.server.model.Session) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 17 with Exchange

use of org.apache.qpid.server.model.Exchange 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();
                try {
                    onBind(new BindingIdentifier(b.getBindingKey(), messageDestination), arguments);
                } catch (AMQInvalidArgumentException e) {
                    throw new IllegalConfigurationException("Unexpected bind argument : " + e.getMessage(), e);
                }
                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());
        }
    }
}
Also used : Binding(org.apache.qpid.server.model.Binding) AlternateBinding(org.apache.qpid.server.model.AlternateBinding) MessageDestination(org.apache.qpid.server.message.MessageDestination) AMQInvalidArgumentException(org.apache.qpid.server.filter.AMQInvalidArgumentException) IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) 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)

Example 18 with Exchange

use of org.apache.qpid.server.model.Exchange in project qpid-broker-j by apache.

the class DirectExchangeTest method testAlternateBinding.

@Test
public void testAlternateBinding() {
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(Exchange.NAME, getTestName());
    attributes.put(Exchange.TYPE, ExchangeDefaults.DIRECT_EXCHANGE_CLASS);
    attributes.put(Exchange.ALTERNATE_BINDING, Collections.singletonMap(AlternateBinding.DESTINATION, _exchange.getName()));
    attributes.put(Exchange.DURABLE, false);
    Exchange newExchange = _vhost.createChild(Exchange.class, attributes);
    assertEquals("Unexpected alternate binding", _exchange.getName(), newExchange.getAlternateBinding().getDestination());
}
Also used : Exchange(org.apache.qpid.server.model.Exchange) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 19 with Exchange

use of org.apache.qpid.server.model.Exchange in project qpid-broker-j by apache.

the class FanoutExchangeTest method testRouteToQueueViaTwoExchanges.

@Test
public void testRouteToQueueViaTwoExchanges() {
    String bindingKey = "key";
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(Exchange.NAME, getTestName());
    attributes.put(Exchange.TYPE, ExchangeDefaults.FANOUT_EXCHANGE_CLASS);
    Exchange via = _vhost.createChild(Exchange.class, attributes);
    Queue<?> queue = _vhost.createChild(Queue.class, Collections.singletonMap(Queue.NAME, getTestName() + "_queue"));
    boolean exchToViaBind = _exchange.bind(via.getName(), bindingKey, Collections.emptyMap(), false);
    assertTrue("Exchange to exchange bind operation should be successful", exchToViaBind);
    boolean viaToQueueBind = via.bind(queue.getName(), bindingKey, Collections.emptyMap(), false);
    assertTrue("Exchange to queue bind operation should be successful", viaToQueueBind);
    RoutingResult<ServerMessage<?>> result = _exchange.route(_messageWithNoHeaders, null, _instanceProperties);
    assertTrue("Message unexpectedly not routed to queue", result.hasRoutes());
}
Also used : Exchange(org.apache.qpid.server.model.Exchange) HashMap(java.util.HashMap) ServerMessage(org.apache.qpid.server.message.ServerMessage) Test(org.junit.Test)

Example 20 with Exchange

use of org.apache.qpid.server.model.Exchange in project qpid-broker-j by apache.

the class FanoutExchangeTest method testRouteToQueueViaTwoExchangesWithReplacementRoutingKeyAndFiltering.

@Test
public void testRouteToQueueViaTwoExchangesWithReplacementRoutingKeyAndFiltering() {
    Map<String, Object> viaExchangeArguments = new HashMap<>();
    viaExchangeArguments.put(Exchange.NAME, getTestName() + "_via_exch");
    viaExchangeArguments.put(Exchange.TYPE, ExchangeDefaults.DIRECT_EXCHANGE_CLASS);
    Exchange via = _vhost.createChild(Exchange.class, viaExchangeArguments);
    Queue<?> queue = _vhost.createChild(Queue.class, Collections.singletonMap(Queue.NAME, getTestName() + "_queue"));
    Map<String, Object> exchToViaBindArguments = new HashMap<>();
    exchToViaBindArguments.put(Binding.BINDING_ARGUMENT_REPLACEMENT_ROUTING_KEY, "key2");
    exchToViaBindArguments.put(JMS_SELECTOR.toString(), "prop = True");
    boolean exchToViaBind = _exchange.bind(via.getName(), "key1", exchToViaBindArguments, false);
    assertTrue("Exchange to exchange bind operation should be successful", exchToViaBind);
    boolean viaToQueueBind = via.bind(queue.getName(), "key2", Collections.emptyMap(), false);
    assertTrue("Exchange to queue bind operation should be successful", viaToQueueBind);
    RoutingResult<ServerMessage<?>> result = _exchange.route(createTestMessage(Collections.singletonMap("prop", true)), "key1", _instanceProperties);
    assertTrue("Message unexpectedly not routed to queue", result.hasRoutes());
    result = _exchange.route(createTestMessage(Collections.singletonMap("prop", false)), "key1", _instanceProperties);
    assertFalse("Message unexpectedly routed to queue", result.hasRoutes());
}
Also used : Exchange(org.apache.qpid.server.model.Exchange) HashMap(java.util.HashMap) ServerMessage(org.apache.qpid.server.message.ServerMessage) Test(org.junit.Test)

Aggregations

Exchange (org.apache.qpid.server.model.Exchange)53 Test (org.junit.Test)29 HashMap (java.util.HashMap)24 ServerMessage (org.apache.qpid.server.message.ServerMessage)15 Queue (org.apache.qpid.server.model.Queue)15 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)14 MessageDestination (org.apache.qpid.server.message.MessageDestination)13 DestinationAddress (org.apache.qpid.server.model.DestinationAddress)8 ReplyTo (org.apache.qpid.server.protocol.v0_10.transport.ReplyTo)8 AMQShortString (org.apache.qpid.server.protocol.v0_8.AMQShortString)8 Message_1_0 (org.apache.qpid.server.protocol.v1_0.Message_1_0)8 ApplicationProperties (org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationProperties)8 Properties (org.apache.qpid.server.protocol.v1_0.type.messaging.Properties)8 NamedAddressSpace (org.apache.qpid.server.model.NamedAddressSpace)7 AccessControlException (java.security.AccessControlException)6 LinkedHashMap (java.util.LinkedHashMap)6 InternalMessage (org.apache.qpid.server.message.internal.InternalMessage)6 BasicContentHeaderProperties (org.apache.qpid.server.protocol.v0_8.transport.BasicContentHeaderProperties)6 MessagePublishInfo (org.apache.qpid.server.protocol.v0_8.transport.MessagePublishInfo)6 IllegalConfigurationException (org.apache.qpid.server.configuration.IllegalConfigurationException)5