You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2020/12/04 12:54:06 UTC

[GitHub] [camel] omarsmak opened a new pull request #4723: CAMEL-15510: Add camel-vertx-kafka component

omarsmak opened a new pull request #4723:
URL: https://github.com/apache/camel/pull/4723


   This is pretty huge PR but I can summarise here , this PR will the component of camel-vertx-kafka to Camel, it includes the following:
   
   - A maven plugin that will generate the component configuration from Kafka Consumer/Producer similar to camel-debezium. Later, I am planning as well to use this plugin to generate the configurations for camel-kafka. This plugin is highly customizable with many options that allows to be adopted it easily to camel-kafka. 
   - The component itself, based on Vert.x client for Kafka that supports full async operations, coupled with Mono and Flux just to minimize the callbacks (not necessary) though. In vertx 4.0 (still beta) it uses Future instead of callback, however the work should't be large, with Flux/Mono could easily wrapped. 
   - The component doesnt yet support manual commit offsets, storing offsets to be committed but it can be added later.
   - The ensure the consistently functionality, I have used the integration tests and unit tests of camel-kafka to test this component (with small modifications specific to the component).


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel] davsclaus commented on a change in pull request #4723: CAMEL-15510: Add camel-vertx-kafka component

Posted by GitBox <gi...@apache.org>.
davsclaus commented on a change in pull request #4723:
URL: https://github.com/apache/camel/pull/4723#discussion_r536659607



##########
File path: components/camel-vertx-kafka/camel-vertx-kafka-component/src/main/java/org/apache/camel/component/vertx/kafka/VertxKafkaConsumer.java
##########
@@ -0,0 +1,118 @@
+/*
+ * 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.vertx.kafka;
+
+import io.vertx.kafka.client.consumer.KafkaConsumer;
+import io.vertx.kafka.client.consumer.KafkaConsumerRecord;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExtendedExchange;
+import org.apache.camel.Processor;
+import org.apache.camel.component.vertx.kafka.configuration.VertxKafkaConfiguration;
+import org.apache.camel.component.vertx.kafka.operations.VertxKafkaConsumerOperations;
+import org.apache.camel.spi.Synchronization;
+import org.apache.camel.support.DefaultConsumer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class VertxKafkaConsumer extends DefaultConsumer {
+
+    private static final Logger LOG = LoggerFactory.getLogger(VertxKafkaConsumer.class);
+
+    private KafkaConsumer<Object, Object> kafkaConsumer;
+
+    public VertxKafkaConsumer(final VertxKafkaEndpoint endpoint, final Processor processor) {
+        super(endpoint, processor);
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+
+        // create the consumer client
+        kafkaConsumer = KafkaConsumer.create(getEndpoint().getVertx(), getConfiguration().createConsumerConfiguration());
+
+        // create the consumer operation
+        final VertxKafkaConsumerOperations consumerOperations
+                = new VertxKafkaConsumerOperations(kafkaConsumer, getConfiguration());
+
+        // process our records
+        consumerOperations.receiveEvents(this::onEventListener, this::onErrorListener);
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        if (kafkaConsumer != null) {
+            kafkaConsumer.close();
+        }
+
+        super.doStop();
+    }
+
+    public VertxKafkaConfiguration getConfiguration() {
+        return getEndpoint().getConfiguration();
+    }
+
+    @Override
+    public VertxKafkaEndpoint getEndpoint() {
+        return (VertxKafkaEndpoint) super.getEndpoint();
+    }
+
+    private void onEventListener(final KafkaConsumerRecord<Object, Object> record) {
+        final Exchange exchange = getEndpoint().createVertxKafkaExchange(record);
+        // set propagated headers
+        VertxKafkaHeadersPropagation.getPropagatedHeaders(record.headers(), exchange.getIn())
+                .forEach((key, value) -> exchange.getIn().setHeader(key, value));
+
+        // add exchange callback
+        exchange.adapt(ExtendedExchange.class).addOnCompletion(new Synchronization() {
+            @Override
+            public void onComplete(Exchange exchange) {
+                // at the moment we don't commit the offsets manually, we can add it in the future
+            }
+
+            @Override
+            public void onFailure(Exchange exchange) {
+                // we do nothing here
+                processRollback(exchange);
+            }
+        });
+        // send message to next processor in the route
+        getAsyncProcessor().process(exchange, doneSync -> LOG.trace("Processing exchange [{}] done.", exchange));
+    }
+
+    private void onErrorListener(final Throwable error) {
+        final Exchange exchange = getEndpoint().createExchange();

Review comment:
       Looks like here you need to set the exception on the created exchange as otherwise its null

##########
File path: components/camel-vertx-kafka/camel-vertx-kafka-component/src/main/java/org/apache/camel/component/vertx/kafka/VertxKafkaConsumer.java
##########
@@ -0,0 +1,118 @@
+/*
+ * 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.vertx.kafka;
+
+import io.vertx.kafka.client.consumer.KafkaConsumer;
+import io.vertx.kafka.client.consumer.KafkaConsumerRecord;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExtendedExchange;
+import org.apache.camel.Processor;
+import org.apache.camel.component.vertx.kafka.configuration.VertxKafkaConfiguration;
+import org.apache.camel.component.vertx.kafka.operations.VertxKafkaConsumerOperations;
+import org.apache.camel.spi.Synchronization;
+import org.apache.camel.support.DefaultConsumer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class VertxKafkaConsumer extends DefaultConsumer {
+
+    private static final Logger LOG = LoggerFactory.getLogger(VertxKafkaConsumer.class);
+
+    private KafkaConsumer<Object, Object> kafkaConsumer;
+
+    public VertxKafkaConsumer(final VertxKafkaEndpoint endpoint, final Processor processor) {
+        super(endpoint, processor);
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+
+        // create the consumer client
+        kafkaConsumer = KafkaConsumer.create(getEndpoint().getVertx(), getConfiguration().createConsumerConfiguration());
+
+        // create the consumer operation
+        final VertxKafkaConsumerOperations consumerOperations
+                = new VertxKafkaConsumerOperations(kafkaConsumer, getConfiguration());
+
+        // process our records
+        consumerOperations.receiveEvents(this::onEventListener, this::onErrorListener);
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        if (kafkaConsumer != null) {
+            kafkaConsumer.close();
+        }
+
+        super.doStop();
+    }
+
+    public VertxKafkaConfiguration getConfiguration() {
+        return getEndpoint().getConfiguration();
+    }
+
+    @Override
+    public VertxKafkaEndpoint getEndpoint() {
+        return (VertxKafkaEndpoint) super.getEndpoint();
+    }
+
+    private void onEventListener(final KafkaConsumerRecord<Object, Object> record) {
+        final Exchange exchange = getEndpoint().createVertxKafkaExchange(record);

Review comment:
       the name are createExchange on endpoints in the other components

##########
File path: components/camel-vertx-kafka/camel-vertx-kafka-component/src/main/java/org/apache/camel/component/vertx/kafka/VertxKafkaHeaderFilterStrategy.java
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.vertx.kafka;
+
+import java.util.regex.Pattern;
+
+import org.apache.camel.support.DefaultHeaderFilterStrategy;
+
+public class VertxKafkaHeaderFilterStrategy extends DefaultHeaderFilterStrategy {

Review comment:
       If you use the header filter strategy, then the endpoint implementation should implement an interface or extend another base class that has support for header filtering, where you can plugin a custom strategy. 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel] jamesnetherton commented on a change in pull request #4723: CAMEL-15510: Add camel-vertx-kafka component

Posted by GitBox <gi...@apache.org>.
jamesnetherton commented on a change in pull request #4723:
URL: https://github.com/apache/camel/pull/4723#discussion_r536161246



##########
File path: components/camel-vertx-kafka/camel-vertx-kafka-component/src/main/java/org/apache/camel/component/vertx/kafka/VertxKafkaComponent.java
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.vertx.kafka;
+
+import java.util.Map;
+
+import io.vertx.core.Vertx;
+import io.vertx.core.VertxOptions;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
+import org.apache.camel.component.vertx.kafka.configuration.VertxKafkaConfiguration;
+import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.annotations.Component;
+import org.apache.camel.support.DefaultComponent;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.PropertiesHelper;
+
+@Component("vertx-kafka")
+public class VertxKafkaComponent extends DefaultComponent {
+
+    @Metadata
+    private VertxKafkaConfiguration configuration = new VertxKafkaConfiguration();
+
+    @Metadata(label = "advanced", autowired = true)
+    private Vertx vertx;
+    @Metadata(label = "advanced")
+    private VertxOptions vertxOptions;
+
+    public VertxKafkaComponent() {
+    }
+
+    public VertxKafkaComponent(CamelContext context) {
+        super(context);
+    }
+
+    @Override
+    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
+
+        if (ObjectHelper.isEmpty(remaining)) {
+            throw new IllegalArgumentException("Topic must be configured on endpoint using syntax kafka:topic");
+        }
+
+        final VertxKafkaConfiguration configuration
+                = this.configuration != null ? this.configuration.copy() : new VertxKafkaConfiguration();
+
+        configuration.setTopic(remaining);
+
+        final VertxKafkaEndpoint endpoint = new VertxKafkaEndpoint(uri, this, configuration);
+
+        // extract the additional properties map
+        if (PropertiesHelper.hasProperties(parameters, "additionalProperties.")) {
+            final Map<String, Object> additionalProperties = endpoint.getConfiguration().getAdditionalProperties();
+
+            // add and overwrite additional properties from endpoint to
+            // pre-configured properties
+            additionalProperties.putAll(PropertiesHelper.extractProperties(parameters, "additionalProperties."));
+        }
+
+        setProperties(endpoint, parameters);
+
+        validateConfigurations(configuration);
+
+        return endpoint;
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+
+        if (vertx == null) {
+            if (vertxOptions != null) {
+                vertx = Vertx.vertx(vertxOptions);
+            } else {
+                vertx = Vertx.vertx();
+            }
+        }
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        if (vertx != null) {
+            vertx.close();
+        }
+        vertx = null;

Review comment:
       Might be better to do like [`VertxComponent.doStop()`](https://github.com/apache/camel/blob/master/components/camel-vertx/src/main/java/org/apache/camel/component/vertx/VertxComponent.java#L201-L209) and handle the case where the Vert.x instance was provided by the user. In that scenario you probably don't want to clean it up as there may be other things that depend on it.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel] omarsmak commented on a change in pull request #4723: CAMEL-15510: Add camel-vertx-kafka component

Posted by GitBox <gi...@apache.org>.
omarsmak commented on a change in pull request #4723:
URL: https://github.com/apache/camel/pull/4723#discussion_r536706816



##########
File path: components/camel-vertx-kafka/camel-vertx-kafka-component/src/main/java/org/apache/camel/component/vertx/kafka/VertxKafkaConsumer.java
##########
@@ -0,0 +1,118 @@
+/*
+ * 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.vertx.kafka;
+
+import io.vertx.kafka.client.consumer.KafkaConsumer;
+import io.vertx.kafka.client.consumer.KafkaConsumerRecord;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExtendedExchange;
+import org.apache.camel.Processor;
+import org.apache.camel.component.vertx.kafka.configuration.VertxKafkaConfiguration;
+import org.apache.camel.component.vertx.kafka.operations.VertxKafkaConsumerOperations;
+import org.apache.camel.spi.Synchronization;
+import org.apache.camel.support.DefaultConsumer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class VertxKafkaConsumer extends DefaultConsumer {
+
+    private static final Logger LOG = LoggerFactory.getLogger(VertxKafkaConsumer.class);
+
+    private KafkaConsumer<Object, Object> kafkaConsumer;
+
+    public VertxKafkaConsumer(final VertxKafkaEndpoint endpoint, final Processor processor) {
+        super(endpoint, processor);
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+
+        // create the consumer client
+        kafkaConsumer = KafkaConsumer.create(getEndpoint().getVertx(), getConfiguration().createConsumerConfiguration());
+
+        // create the consumer operation
+        final VertxKafkaConsumerOperations consumerOperations
+                = new VertxKafkaConsumerOperations(kafkaConsumer, getConfiguration());
+
+        // process our records
+        consumerOperations.receiveEvents(this::onEventListener, this::onErrorListener);
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        if (kafkaConsumer != null) {
+            kafkaConsumer.close();
+        }
+
+        super.doStop();
+    }
+
+    public VertxKafkaConfiguration getConfiguration() {
+        return getEndpoint().getConfiguration();
+    }
+
+    @Override
+    public VertxKafkaEndpoint getEndpoint() {
+        return (VertxKafkaEndpoint) super.getEndpoint();
+    }
+
+    private void onEventListener(final KafkaConsumerRecord<Object, Object> record) {
+        final Exchange exchange = getEndpoint().createVertxKafkaExchange(record);
+        // set propagated headers
+        VertxKafkaHeadersPropagation.getPropagatedHeaders(record.headers(), exchange.getIn())
+                .forEach((key, value) -> exchange.getIn().setHeader(key, value));
+
+        // add exchange callback
+        exchange.adapt(ExtendedExchange.class).addOnCompletion(new Synchronization() {
+            @Override
+            public void onComplete(Exchange exchange) {
+                // at the moment we don't commit the offsets manually, we can add it in the future
+            }
+
+            @Override
+            public void onFailure(Exchange exchange) {
+                // we do nothing here
+                processRollback(exchange);
+            }
+        });
+        // send message to next processor in the route
+        getAsyncProcessor().process(exchange, doneSync -> LOG.trace("Processing exchange [{}] done.", exchange));
+    }
+
+    private void onErrorListener(final Throwable error) {
+        final Exchange exchange = getEndpoint().createExchange();

Review comment:
       Ah good note. Didnt realize that it will be always null




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel] omarsmak commented on pull request #4723: CAMEL-15510: Add camel-vertx-kafka component

Posted by GitBox <gi...@apache.org>.
omarsmak commented on pull request #4723:
URL: https://github.com/apache/camel/pull/4723#issuecomment-739831065


   @jamesnetherton @davsclaus I have addressed your comments, please take a look. In regards to the filter strategy, I have added the support for the custom header filtering in the configuration where you can plug-in the custom header filter in the configurations.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel] omarsmak merged pull request #4723: CAMEL-15510: Add camel-vertx-kafka component

Posted by GitBox <gi...@apache.org>.
omarsmak merged pull request #4723:
URL: https://github.com/apache/camel/pull/4723


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org