You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/12/27 14:40:52 UTC

[camel] branch master updated: CAMEL-15991: camel-rabbitmq - Add support for DynamicAware endpoints for toD

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 8b43334  CAMEL-15991: camel-rabbitmq - Add support for DynamicAware endpoints for toD
8b43334 is described below

commit 8b4333411a465a046d0e037dbab7647825ba90b3
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Dec 27 15:40:18 2020 +0100

    CAMEL-15991: camel-rabbitmq - Add support for DynamicAware endpoints for toD
---
 .../org/apache/camel/send-dynamic/rabbitmq         |   2 +
 .../rabbitmq/RabbitMQSendDynamicAware.java         | 125 +++++++++++++++++++++
 .../rabbitmq/integration/RabbitMQToDIntTest.java   |  56 +++++++++
 .../integration/RabbitMQToDSendDynamicTest.java    |  57 ++++++++++
 4 files changed, 240 insertions(+)

diff --git a/components/camel-rabbitmq/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/rabbitmq b/components/camel-rabbitmq/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/rabbitmq
new file mode 100644
index 0000000..64373d7
--- /dev/null
+++ b/components/camel-rabbitmq/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/rabbitmq
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.rabbitmq.RabbitMQSendDynamicAware
diff --git a/components/camel-rabbitmq/src/main/java/org/apache/camel/component/rabbitmq/RabbitMQSendDynamicAware.java b/components/camel-rabbitmq/src/main/java/org/apache/camel/component/rabbitmq/RabbitMQSendDynamicAware.java
new file mode 100644
index 0000000..7b54881
--- /dev/null
+++ b/components/camel-rabbitmq/src/main/java/org/apache/camel/component/rabbitmq/RabbitMQSendDynamicAware.java
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.rabbitmq;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.spi.SendDynamicAware;
+import org.apache.camel.spi.annotations.SendDynamic;
+import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.StringHelper;
+
+/**
+ * RabbitMQ based {@link org.apache.camel.spi.SendDynamicAware} which allows to optimise RabbitMQ components with the
+ * toD (dynamic to) DSL in Camel. This implementation optimises by allowing to provide dynamic parameters via
+ * {@link RabbitMQConstants#EXCHANGE_OVERRIDE_NAME} header instead of the endpoint uri. That allows to use a static
+ * endpoint and its producer to service dynamic requests.
+ */
+@SendDynamic("rabbitmq")
+public class RabbitMQSendDynamicAware extends ServiceSupport implements SendDynamicAware {
+
+    private CamelContext camelContext;
+    private String scheme;
+
+    @Override
+    public String getScheme() {
+        return scheme;
+    }
+
+    @Override
+    public void setScheme(String scheme) {
+        this.scheme = scheme;
+    }
+
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public boolean isOnlyDynamicQueryParameters() {
+        return true;
+    }
+
+    @Override
+    public boolean isLenientProperties() {
+        return false;
+    }
+
+    @Override
+    public DynamicAwareEntry prepare(Exchange exchange, String uri, String originalUri) throws Exception {
+        return new DynamicAwareEntry(uri, originalUri, null, null);
+    }
+
+    @Override
+    public String resolveStaticUri(Exchange exchange, DynamicAwareEntry entry) throws Exception {
+        String exchangeName = parseExchangeName(entry.getUri());
+        if (exchangeName != null) {
+            String originalExchangeName = parseExchangeName(entry.getOriginalUri());
+            if (!exchangeName.equals(originalExchangeName)) {
+                // okay the exchange name was dynamic, so use the original as endpoint name
+                String answer = entry.getUri();
+                answer = StringHelper.replaceFirst(answer, exchangeName, originalExchangeName);
+                return answer;
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public Processor createPreProcessor(Exchange exchange, DynamicAwareEntry entry) throws Exception {
+        if (exchange.getMessage().getHeader(RabbitMQConstants.EXCHANGE_OVERRIDE_NAME) != null) {
+            return null;
+        }
+
+        final String destinationName = parseExchangeName(entry.getUri());
+        return new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getMessage().setHeader(RabbitMQConstants.EXCHANGE_OVERRIDE_NAME, destinationName);
+            }
+        };
+    }
+
+    @Override
+    public Processor createPostProcessor(Exchange exchange, DynamicAwareEntry entry) throws Exception {
+        // no post processor is needed
+        return null;
+    }
+
+    private String parseExchangeName(String uri) {
+        // strip query
+        int pos = uri.indexOf('?');
+        if (pos != -1) {
+            uri = uri.substring(0, pos);
+        }
+        // exchange name is after first colon
+        pos = uri.indexOf(':');
+        if (pos != -1) {
+            return uri.substring(pos + 1);
+        } else {
+            return null;
+        }
+    }
+
+}
diff --git a/components/camel-rabbitmq/src/test/java/org/apache/camel/component/rabbitmq/integration/RabbitMQToDIntTest.java b/components/camel-rabbitmq/src/test/java/org/apache/camel/component/rabbitmq/integration/RabbitMQToDIntTest.java
new file mode 100644
index 0000000..c59ac38
--- /dev/null
+++ b/components/camel-rabbitmq/src/test/java/org/apache/camel/component/rabbitmq/integration/RabbitMQToDIntTest.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.rabbitmq.integration;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.rabbitmq.RabbitMQComponent;
+import org.junit.jupiter.api.Test;
+
+public class RabbitMQToDIntTest extends AbstractRabbitMQIntTest {
+
+    @Test
+    public void testToD() throws Exception {
+        getMockEndpoint("mock:bar").expectedBodiesReceived("Hello bar");
+        getMockEndpoint("mock:beer").expectedBodiesReceived("Hello beer");
+
+        template.sendBodyAndHeader("direct:start", "Hello bar", "where", "bar");
+        template.sendBodyAndHeader("direct:start", "Hello beer", "where", "beer");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                RabbitMQComponent mq = context.getComponent("rabbitmq", RabbitMQComponent.class);
+                mq.setHostname(service.connectionProperties().hostname());
+                mq.setPortNumber(service.connectionProperties().port());
+                mq.setUsername(service.connectionProperties().username());
+                mq.setPassword(service.connectionProperties().password());
+
+                // route message dynamic using toD
+                from("direct:start").toD("rabbitmq:${header.where}");
+
+                from("rabbitmq:bar").to("mock:bar");
+                from("rabbitmq:beer").to("mock:beer");
+            }
+        };
+    }
+
+}
diff --git a/components/camel-rabbitmq/src/test/java/org/apache/camel/component/rabbitmq/integration/RabbitMQToDSendDynamicTest.java b/components/camel-rabbitmq/src/test/java/org/apache/camel/component/rabbitmq/integration/RabbitMQToDSendDynamicTest.java
new file mode 100644
index 0000000..d399bdf
--- /dev/null
+++ b/components/camel-rabbitmq/src/test/java/org/apache/camel/component/rabbitmq/integration/RabbitMQToDSendDynamicTest.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.rabbitmq.integration;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.rabbitmq.RabbitMQComponent;
+import org.apache.camel.component.rabbitmq.RabbitMQConstants;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class RabbitMQToDSendDynamicTest extends AbstractRabbitMQIntTest {
+
+    @Test
+    public void testToD() throws Exception {
+        template.sendBodyAndHeader("direct:start", "Hello bar", "where", "bar");
+        template.sendBodyAndHeader("direct:start", "Hello beer", "where", "beer");
+
+        // there should only be one rabbitmq endpoint
+        long count = context.getEndpoints().stream().filter(e -> e.getEndpointUri().startsWith("rabbitmq:")).count();
+        assertEquals(1, count, "There should only be 1 rabbitmq endpoint");
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                RabbitMQComponent mq = context.getComponent("rabbitmq", RabbitMQComponent.class);
+                mq.setHostname(service.connectionProperties().hostname());
+                mq.setPortNumber(service.connectionProperties().port());
+                mq.setUsername(service.connectionProperties().username());
+                mq.setPassword(service.connectionProperties().password());
+
+                // route message dynamic using toD
+                from("direct:start")
+                        .setHeader(RabbitMQConstants.DELIVERY_MODE, constant("2"))
+                        .toD("rabbitmq:${header.where}");
+            }
+        };
+    }
+
+}