Search in sources :

Example 21 with Exchange

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

the class HeadersExchangeTest method testRouteToQueueViaTwoExchangesWithReplacementRoutingKeyAndFiltering.

@Test
public void testRouteToQueueViaTwoExchangesWithReplacementRoutingKeyAndFiltering() {
    String bindingKey = "key1";
    String replacementKey = "key2";
    Map<String, Object> viaExchangeArguments = new HashMap<>();
    viaExchangeArguments.put(Exchange.NAME, getTestName() + "_via_exch");
    viaExchangeArguments.put(Exchange.TYPE, ExchangeDefaults.TOPIC_EXCHANGE_CLASS);
    Exchange via = _virtualHost.createChild(Exchange.class, viaExchangeArguments);
    Queue<?> queue = _virtualHost.createChild(Queue.class, Collections.singletonMap(Queue.NAME, getTestName() + "_queue"));
    Map<String, Object> exchToViaBindArguments = new HashMap<>();
    exchToViaBindArguments.put(Binding.BINDING_ARGUMENT_REPLACEMENT_ROUTING_KEY, replacementKey);
    exchToViaBindArguments.put(JMS_SELECTOR.toString(), "prop = True");
    boolean exchToViaBind = _exchange.bind(via.getName(), bindingKey, exchToViaBindArguments, false);
    assertTrue("Exchange to exchange bind operation should be successful", exchToViaBind);
    boolean viaToQueueBind = via.bind(queue.getName(), replacementKey, Collections.emptyMap(), false);
    assertTrue("Exchange to queue bind operation should be successful", viaToQueueBind);
    RoutingResult<ServerMessage<?>> result = _exchange.route(createTestMessage(Collections.singletonMap("prop", true)), bindingKey, _instanceProperties);
    assertTrue("Message unexpectedly not routed to queue", result.hasRoutes());
    result = _exchange.route(createTestMessage(Collections.singletonMap("prop", false)), bindingKey, _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)

Example 22 with Exchange

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

the class AbstractDurableConfigurationStoreTestCase method createTestExchange.

private Exchange<?> createTestExchange() {
    Exchange exchange = mock(Exchange.class);
    Map<String, Object> actualAttributes = new HashMap<>();
    actualAttributes.put("name", getTestName());
    actualAttributes.put("type", getTestName() + "Type");
    actualAttributes.put("lifetimePolicy", LifetimePolicy.DELETE_ON_NO_OUTBOUND_LINKS);
    when(exchange.getName()).thenReturn(getTestName());
    when(exchange.getType()).thenReturn(getTestName() + "Type");
    when(exchange.isAutoDelete()).thenReturn(true);
    when(exchange.getId()).thenReturn(_exchangeId);
    when(exchange.getCategoryClass()).thenReturn(Exchange.class);
    when(exchange.isDurable()).thenReturn(true);
    when(exchange.getObjectFactory()).thenReturn(_factory);
    when(exchange.getModel()).thenReturn(_factory.getModel());
    TaskExecutor taskExecutor = CurrentThreadTaskExecutor.newStartedInstance();
    when(exchange.getTaskExecutor()).thenReturn(taskExecutor);
    when(exchange.getChildExecutor()).thenReturn(taskExecutor);
    ConfiguredObjectRecord exchangeRecord = mock(ConfiguredObjectRecord.class);
    when(exchangeRecord.getId()).thenReturn(_exchangeId);
    when(exchangeRecord.getType()).thenReturn(Exchange.class.getSimpleName());
    when(exchangeRecord.getAttributes()).thenReturn(actualAttributes);
    when(exchangeRecord.getParents()).thenReturn(Collections.singletonMap(_rootRecord.getType(), _rootRecord.getId()));
    when(exchange.asObjectRecord()).thenReturn(exchangeRecord);
    when(exchange.getEventLogger()).thenReturn(new EventLogger());
    return exchange;
}
Also used : Exchange(org.apache.qpid.server.model.Exchange) CurrentThreadTaskExecutor(org.apache.qpid.server.configuration.updater.CurrentThreadTaskExecutor) TaskExecutor(org.apache.qpid.server.configuration.updater.TaskExecutor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) EventLogger(org.apache.qpid.server.logging.EventLogger) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject)

Example 23 with Exchange

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

the class PropertyConverter_Internal_to_v0_8Test method testReplyToConversionWhenExchangeIsSpecified.

@Test
public void testReplyToConversionWhenExchangeIsSpecified() {
    final String replyTo = "testExchange";
    final AMQMessageHeader header = mock(AMQMessageHeader.class);
    when(header.getReplyTo()).thenReturn(replyTo);
    Exchange exchange = mock(Exchange.class);
    when(exchange.getName()).thenReturn(replyTo);
    when(exchange.getType()).thenReturn(ExchangeDefaults.FANOUT_EXCHANGE_CLASS);
    doReturn(exchange).when(_addressSpace).getAttainedMessageDestination(eq(replyTo), anyBoolean());
    InternalMessage originalMessage = createTestMessage(header);
    AMQMessage convertedMessage = _messageConverter.convert(originalMessage, _addressSpace);
    assertEquals("Unexpected reply-to", "fanout://" + replyTo + "//", convertedMessage.getContentHeaderBody().getProperties().getReplyToAsString());
}
Also used : Exchange(org.apache.qpid.server.model.Exchange) InternalMessage(org.apache.qpid.server.message.internal.InternalMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AMQMessageHeader(org.apache.qpid.server.message.AMQMessageHeader) Test(org.junit.Test)

Example 24 with Exchange

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

the class QmfManagementAgent method childRemoved.

/**
 * ConfigurationChangeListener method called when a child ConfiguredObject is removed.
 * <p>
 * This method checks the type of the child ConfiguredObject that has been removed and raises the appropriate
 * QMF2 Events, it then destroys the QMF2 Management Object and removes the mapping between child and the QMF Object.
 *
 * @param object the parent object that the child is being removed from.
 * @param child the child object being removed.
 */
@Override
public void childRemoved(final ConfiguredObject object, final ConfiguredObject child) {
    if (_log.isDebugEnabled()) {
        _log.debug("childRemoved: " + child.getClass().getSimpleName() + "." + child.getName());
    }
    child.removeChangeListener(this);
    // Look up the associated QmfAgentData and mark it for deletion by the Agent.
    QmfAgentData data = _objects.get(child);
    if (data != null) {
        if (child instanceof Connection) {
            // Raise a Client Disconnect Event.
            _agent.raiseEvent(((org.apache.qpid.server.qmf2.agentdata.Connection) data).createClientDisconnectEvent());
        } else if (child instanceof Session) {
        // no-op, don't need to do anything specific when Session is removed.
        } else if (child instanceof Exchange) {
            // Raise an Exchange Delete Event.
            _agent.raiseEvent(((org.apache.qpid.server.qmf2.agentdata.Exchange) data).createExchangeDeleteEvent());
        } else if (child instanceof Queue) {
            // Raise a Queue Delete Event.
            _agent.raiseEvent(((org.apache.qpid.server.qmf2.agentdata.Queue) data).createQueueDeleteEvent());
        } else if (child instanceof Binding) {
            String eName = ((Binding) child).getExchange().getName();
            if (// Don't send Event for Unbinding from default direct.
            !eName.equals("<<default>>")) {
                // Raise an Unbind Event.
                _agent.raiseEvent(((org.apache.qpid.server.qmf2.agentdata.Binding) data).createUnbindEvent());
            }
        } else if (child instanceof Consumer) {
            // Raise an Unsubscribe Event.
            _agent.raiseEvent(((org.apache.qpid.server.qmf2.agentdata.Subscription) data).createUnsubscribeEvent());
        }
        data.destroy();
    }
    // Remove the mapping from the internal ConfiguredObject->QmfAgentData Map.
    _objects.remove(child);
}
Also used : Binding(org.apache.qpid.server.model.Binding) 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) Queue(org.apache.qpid.server.model.Queue) Session(org.apache.qpid.server.model.Session)

Example 25 with Exchange

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

the class Broker method invokeMethod.

/**
 * This method acts as a single entry point for QMF methods invoked on the Broker Object.
 *
 * @param agent the org.apache.qpid.qmf2.agent.Agent instance that we call methodResponse() and raiseException() on.
 * @param handle the reply handle used by methodResponse() and raiseException().
 * @param methodName the name of the QMF method being invoked.
 * @param inArgs a Map of input arguments wrapped in a QmfData Object.
 */
@SuppressWarnings("unchecked")
public void invokeMethod(Agent agent, Handle handle, String methodName, QmfData inArgs) {
    if (methodName.equals("create") || methodName.equals("delete")) {
        QmfData outArgs = new QmfData();
        String name = inArgs.getStringValue("name");
        String type = inArgs.getStringValue("type");
        NameParser nameParser = new NameParser(name, type);
        String vhostName = nameParser.getVirtualHostName();
        VirtualHost vhost = nameParser.getVirtualHost();
        if (vhost == null) {
            if (vhostName == null) {
                agent.raiseException(handle, "VirtualHost names for exchange and queue must match.");
            } else {
                agent.raiseException(handle, "VirtualHost " + vhostName + " not found.");
            }
        } else {
            if (// method = create
            methodName.equals("create")) {
                try {
                    // boolean strict = inArgs.getBooleanValue("strict");
                    Map<String, Object> properties = inArgs.getValue("properties");
                    boolean durable = false;
                    Object property = properties.get("durable");
                    if (property != null && property instanceof Boolean) {
                        Boolean durableProperty = (Boolean) property;
                        durable = durableProperty.booleanValue();
                        properties.remove("durable");
                    }
                    if (// create exchange.
                    type.equals("exchange")) {
                        /*
System.out.println("Create Exchange");
System.out.println("vhostName = " + vhostName);
System.out.println("exchange name = " + nameParser.getExchangeName());
System.out.println("properties = " + properties);
*/
                        String exchangeType = "";
                        property = properties.get("exchange-type");
                        if (property != null && property instanceof String) {
                            exchangeType = property.toString();
                            properties.remove("exchange-type");
                        }
                        String alternateExchange = parseAlternateExchange(vhostName, properties);
                        if (alternateExchange != null && alternateExchange.equals("invalid")) {
                            agent.raiseException(handle, "Alternate Exchange must belong to the same Virtual Host as the Exchange being added.");
                            return;
                        }
                        // Note that for Qpid 0.20 the "qpid.msg_sequence=1" and "qpid.ive=1" properties are
                        // not suppored, indeed no exchange properties seem to be supported yet.
                        Map<String, Object> attributes = new HashMap<>();
                        attributes.put(Exchange.NAME, nameParser.getExchangeName());
                        attributes.put(Exchange.STATE, State.ACTIVE);
                        attributes.put(Exchange.DURABLE, durable);
                        attributes.put(Exchange.LIFETIME_POLICY, LifetimePolicy.PERMANENT);
                        attributes.put(Exchange.TYPE, exchangeType);
                        attributes.put(Exchange.ALTERNATE_EXCHANGE, alternateExchange);
                        vhost.createExchange(attributes);
                    } else // End of create exchange.
                    if (// create queue.
                    type.equals("queue")) {
                        /*
System.out.println("Create Queue");
System.out.println("vhostName = " + vhostName);
System.out.println("queue name = " + nameParser.getQueueName());
System.out.println("properties = " + properties);
*/
                        // TODO Try to map from the QMF create queue properties to the closest equivalents on
                        // the Java Broker. Unfortunately there are a *lot* of frustrating little differences.
                        String alternateExchange = parseAlternateExchange(vhostName, properties);
                        if (alternateExchange != null && alternateExchange.equals("invalid")) {
                            agent.raiseException(handle, "Alternate Exchange must belong to the same Virtual Host as the Queue being added.");
                            return;
                        }
                        // I don't *think* that it make sense to allow setting exclusive or autoDelete to
                        // a queue created from config.
                        Map<String, Object> attributes = new HashMap<String, Object>(properties);
                        attributes.put(Queue.NAME, nameParser.getQueueName());
                        attributes.put(Queue.DURABLE, durable);
                        attributes.put(Queue.LIFETIME_POLICY, LifetimePolicy.PERMANENT);
                        // to work, so we may as well make use of this convenience here too.
                        if (alternateExchange != null) {
                            ObjectId objectId = new ObjectId("", "org.apache.qpid.broker:exchange:" + alternateExchange, 0);
                            // Look up Exchange QmfAgentData by ObjectId from the Agent's internal Object store.
                            QmfAgentData object = agent.getObject(objectId);
                            if (object != null) {
                                org.apache.qpid.server.qmf2.agentdata.Exchange ex = (org.apache.qpid.server.qmf2.agentdata.Exchange) object;
                                Exchange altEx = ex.getExchange();
                                attributes.put(Queue.ALTERNATE_EXCHANGE, altEx.getId());
                            }
                        }
                        Queue queue = vhost.createQueue(attributes);
                    } else if (// create binding.
                    type.equals("binding")) {
                        Exchange exchange = nameParser.getExchange();
                        if (exchange == null) {
                            agent.raiseException(handle, "Cannot create binding on Exchange " + nameParser.getExchangeName());
                            return;
                        } else {
                            Map<String, Object> attributes = Collections.emptyMap();
                            exchange.createBinding(nameParser.getBindingKey(), nameParser.getQueue(), properties, attributes);
                        }
                    }
                    agent.methodResponse(methodName, handle, outArgs, null);
                } catch (Exception e) {
                    agent.raiseException(handle, e.getMessage());
                }
            } else // method = delete
            {
                try {
                    if (// delete exchange.
                    type.equals("exchange")) {
                        Exchange exchange = nameParser.getExchange();
                        if (exchange != null) {
                            exchange.delete();
                        }
                    } else if (// delete queue.
                    type.equals("queue")) {
                        Queue queue = nameParser.getQueue();
                        if (queue != null) {
                            queue.deleteAndReturnCount();
                        }
                    } else if (// delete binding.
                    type.equals("binding")) {
                        Binding binding = nameParser.getBinding();
                        if (binding != null) {
                            binding.delete();
                        }
                    }
                    agent.methodResponse(methodName, handle, outArgs, null);
                } catch (Exception e) {
                    agent.raiseException(handle, e.getMessage());
                }
            }
        }
    } else // If methodName is not create or delete.
    {
        agent.raiseException(handle, methodName + " not yet implemented on Broker.");
    }
}
Also used : Binding(org.apache.qpid.server.model.Binding) QmfData(org.apache.qpid.qmf2.common.QmfData) HashMap(java.util.HashMap) ObjectId(org.apache.qpid.qmf2.common.ObjectId) Exchange(org.apache.qpid.server.model.Exchange) QmfAgentData(org.apache.qpid.qmf2.agent.QmfAgentData) VirtualHost(org.apache.qpid.server.model.VirtualHost) HashMap(java.util.HashMap) Map(java.util.Map) Queue(org.apache.qpid.server.model.Queue)

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