You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eventmesh.apache.org by ch...@apache.org on 2022/10/23 06:58:09 UTC
[incubator-eventmesh] branch rabbitmq-connector updated: add rabbitmq consumer
This is an automated email from the ASF dual-hosted git repository.
chenguangsheng pushed a commit to branch rabbitmq-connector
in repository https://gitbox.apache.org/repos/asf/incubator-eventmesh.git
The following commit(s) were added to refs/heads/rabbitmq-connector by this push:
new 25a30205 add rabbitmq consumer
new 68985f61 Merge pull request #1764 from mroccyen/rabbitmq-consumer-operation
25a30205 is described below
commit 25a3020529c85ad021a1f7942ef50c9efeea94e8
Author: mroccyen <qi...@126.com>
AuthorDate: Sun Oct 23 14:39:17 2022 +0800
add rabbitmq consumer
---
.../rabbitmq/consumer/RabbitmqConsumer.java | 134 +++++++++++++++++++++
.../rabbitmq/consumer/RabbitmqConsumerHandler.java | 84 +++++++++++++
2 files changed, 218 insertions(+)
diff --git a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/consumer/RabbitmqConsumer.java b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/consumer/RabbitmqConsumer.java
new file mode 100644
index 00000000..56124ab8
--- /dev/null
+++ b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/consumer/RabbitmqConsumer.java
@@ -0,0 +1,134 @@
+/*
+ * 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.eventmesh.connector.rabbitmq.consumer;
+
+import org.apache.eventmesh.api.AbstractContext;
+import org.apache.eventmesh.api.EventListener;
+import org.apache.eventmesh.api.consumer.Consumer;
+import org.apache.eventmesh.common.ThreadPoolFactory;
+import org.apache.eventmesh.connector.rabbitmq.client.RabbitmqClient;
+import org.apache.eventmesh.connector.rabbitmq.client.RabbitmqConnectionFactory;
+import org.apache.eventmesh.connector.rabbitmq.config.ConfigurationHolder;
+
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.ThreadPoolExecutor;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.cloudevents.CloudEvent;
+
+import com.rabbitmq.client.Channel;
+import com.rabbitmq.client.Connection;
+
+public class RabbitmqConsumer implements Consumer {
+
+ private static final Logger logger = LoggerFactory.getLogger(RabbitmqConsumer.class);
+
+ private RabbitmqConnectionFactory rabbitmqConnectionFactory = new RabbitmqConnectionFactory();
+
+ private RabbitmqClient rabbitmqClient;
+
+ private Connection connection;
+
+ private Channel channel;
+
+ private volatile boolean started = false;
+
+ private final ConfigurationHolder configurationHolder = new ConfigurationHolder();
+
+ private final ThreadPoolExecutor executor = ThreadPoolFactory.createThreadPoolExecutor(
+ Runtime.getRuntime().availableProcessors() * 2,
+ Runtime.getRuntime().availableProcessors() * 2,
+ "EventMesh-Rabbitmq-Consumer-");
+
+ private RabbitmqConsumerHandler rabbitmqConsumerHandler;
+
+ @Override
+ public boolean isStarted() {
+ return started;
+ }
+
+ @Override
+ public boolean isClosed() {
+ return !isStarted();
+ }
+
+ @Override
+ public void start() {
+ if (!started) {
+ started = true;
+ }
+ }
+
+ @Override
+ public void shutdown() {
+ if (started) {
+ try {
+ rabbitmqClient.closeConnection(connection);
+ rabbitmqClient.closeChannel(channel);
+ rabbitmqConsumerHandler.stop();
+ } finally {
+ started = false;
+ }
+ }
+ }
+
+ @Override
+ public void init(Properties keyValue) throws Exception {
+ this.configurationHolder.init();
+ this.rabbitmqClient = new RabbitmqClient(rabbitmqConnectionFactory);
+ this.connection = rabbitmqClient.getConnection(configurationHolder.getHost(), configurationHolder.getUsername(),
+ configurationHolder.getPasswd(), configurationHolder.getPort(), configurationHolder.getVirtualHost());
+ this.channel = rabbitmqConnectionFactory.createChannel(connection);
+ this.rabbitmqConsumerHandler = new RabbitmqConsumerHandler(channel, configurationHolder);
+ }
+
+ @Override
+ public void updateOffset(List<CloudEvent> cloudEvents, AbstractContext context) {
+
+ }
+
+ @Override
+ public void subscribe(String topic) {
+ rabbitmqClient.binding(channel, configurationHolder.getExchangeType(), configurationHolder.getExchangeName(),
+ configurationHolder.getRoutingKey(), configurationHolder.getQueueName());
+ executor.execute(rabbitmqConsumerHandler);
+ }
+
+ @Override
+ public void unsubscribe(String topic) {
+ try {
+ rabbitmqClient.unbinding(channel, configurationHolder.getExchangeName(),
+ configurationHolder.getRoutingKey(), configurationHolder.getQueueName());
+ rabbitmqConsumerHandler.stop();
+ } catch (Exception ex) {
+ logger.error("[RabbitmqConsumer] unsubscribe happen exception.", ex);
+ }
+ }
+
+ @Override
+ public void registerEventListener(EventListener listener) {
+ rabbitmqConsumerHandler.setEventListener(listener);
+ }
+
+ public void setRabbitmqConnectionFactory(RabbitmqConnectionFactory rabbitmqConnectionFactory) {
+ this.rabbitmqConnectionFactory = rabbitmqConnectionFactory;
+ }
+}
diff --git a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/consumer/RabbitmqConsumerHandler.java b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/consumer/RabbitmqConsumerHandler.java
new file mode 100644
index 00000000..0d53bb1b
--- /dev/null
+++ b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/consumer/RabbitmqConsumerHandler.java
@@ -0,0 +1,84 @@
+/*
+ * 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.eventmesh.connector.rabbitmq.consumer;
+
+import org.apache.eventmesh.api.EventListener;
+import org.apache.eventmesh.api.EventMeshAction;
+import org.apache.eventmesh.api.EventMeshAsyncConsumeContext;
+import org.apache.eventmesh.connector.rabbitmq.cloudevent.RabbitmqCloudEvent;
+import org.apache.eventmesh.connector.rabbitmq.config.ConfigurationHolder;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.cloudevents.CloudEvent;
+
+import com.rabbitmq.client.Channel;
+import com.rabbitmq.client.GetResponse;
+
+public class RabbitmqConsumerHandler implements Runnable {
+
+ private static final Logger logger = LoggerFactory.getLogger(RabbitmqConsumerHandler.class);
+
+ private final Channel channel;
+ private final ConfigurationHolder configurationHolder;
+ private final AtomicBoolean stop = new AtomicBoolean(false);
+ private EventListener eventListener;
+
+ public RabbitmqConsumerHandler(Channel channel, ConfigurationHolder configurationHolder) {
+ this.channel = channel;
+ this.configurationHolder = configurationHolder;
+ }
+
+ @Override
+ public void run() {
+ while (!stop.get()) {
+ try {
+ GetResponse response = channel.basicGet(configurationHolder.getQueueName(), configurationHolder.isAutoAck());
+ if (response != null) {
+ RabbitmqCloudEvent rabbitmqCloudEvent = RabbitmqCloudEvent.getFromByteArray(response.getBody());
+ CloudEvent cloudEvent = rabbitmqCloudEvent.convertToCloudEvent();
+ final EventMeshAsyncConsumeContext consumeContext = new EventMeshAsyncConsumeContext() {
+ @Override
+ public void commit(EventMeshAction action) {
+ logger.info("[RabbitmqConsumerHandler] Rabbitmq consumer context commit.");
+ }
+ };
+ if (eventListener != null) {
+ eventListener.consume(cloudEvent, consumeContext);
+ }
+ if (!configurationHolder.isAutoAck()) {
+ channel.basicAck(response.getEnvelope().getDeliveryTag(), false);
+ }
+ }
+ } catch (Exception ex) {
+ logger.error("[RabbitmqConsumerHandler] thread run happen exception.", ex);
+ }
+ }
+ }
+
+ public void setEventListener(EventListener eventListener) {
+ this.eventListener = eventListener;
+ }
+
+ public void stop() {
+ stop.set(true);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: commits-help@eventmesh.apache.org