forked from apache/eventmesh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Motivation fix checkstyle Modifications Documentation
- Loading branch information
fengyongshe
committed
Sep 15, 2022
1 parent
20da206
commit f34be3a
Showing
4 changed files
with
185 additions
and
127 deletions.
There are no files selected for viewing
162 changes: 91 additions & 71 deletions
162
...lsar/src/main/java/org/apache/eventmesh/connector/pulsar/consumer/PulsarConsumerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,117 @@ | ||
/* | ||
* 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.pulsar.consumer; | ||
|
||
import io.cloudevents.CloudEvent; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.eventmesh.api.AbstractContext; | ||
import org.apache.eventmesh.api.EventListener; | ||
import org.apache.eventmesh.api.consumer.Consumer; | ||
import org.apache.eventmesh.connector.pulsar.config.ClientConfiguration; | ||
|
||
import org.apache.pulsar.client.api.PulsarClient; | ||
|
||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import io.cloudevents.CloudEvent; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class PulsarConsumerImpl implements Consumer { | ||
|
||
private final AtomicBoolean started = new AtomicBoolean(false); | ||
private Properties properties; | ||
private PulsarClient pulsarClient; | ||
private EventListener eventListener; | ||
private final AtomicBoolean started = new AtomicBoolean(false); | ||
private Properties properties; | ||
private PulsarClient pulsarClient; | ||
private EventListener eventListener; | ||
|
||
private SubscribeTask task; | ||
|
||
@Override | ||
public void init(Properties properties) throws Exception { | ||
this.properties = properties; | ||
|
||
final ClientConfiguration clientConfiguration = new ClientConfiguration(); | ||
clientConfiguration.init(); | ||
|
||
private SubscribeTask task; | ||
try { | ||
this.pulsarClient = PulsarClient.builder() | ||
.serviceUrl(clientConfiguration.serviceAddr) | ||
.build(); | ||
} catch (Exception ex) { | ||
log.error("Failed to start the pulsar client: {}", ex.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public void start() { | ||
this.started.compareAndSet(false, true); | ||
} | ||
|
||
@Override | ||
public void init(Properties properties) throws Exception { | ||
this.properties = properties; | ||
@Override | ||
public void subscribe(String topic) throws Exception { | ||
if (pulsarClient == null) { | ||
log.error("Cann't find the pulsar client"); | ||
} | ||
org.apache.pulsar.client.api.Consumer<byte[]> consumer = pulsarClient.newConsumer() | ||
.topic(topic) | ||
.subscriptionName(properties.getProperty("consumerGroup")) | ||
.subscribe(); | ||
task = new SubscribeTask(topic, consumer, eventListener); | ||
task.start(); | ||
} | ||
|
||
@Override | ||
public void unsubscribe(String topic) { | ||
|
||
final ClientConfiguration clientConfiguration = new ClientConfiguration(); | ||
clientConfiguration.init(); | ||
} | ||
|
||
try { | ||
this.pulsarClient = PulsarClient.builder() | ||
.serviceUrl(clientConfiguration.serviceAddr) | ||
.build(); | ||
} catch (Exception ex) { | ||
log.error("Failed to start the pulsar client: {}", ex.getMessage()); | ||
@Override | ||
public void registerEventListener(EventListener listener) { | ||
this.eventListener = listener; | ||
} | ||
} | ||
|
||
@Override | ||
public void start() { | ||
this.started.compareAndSet(false, true); | ||
} | ||
@Override | ||
public void updateOffset(List<CloudEvent> cloudEvents, AbstractContext context) { | ||
|
||
@Override | ||
public void subscribe(String topic) throws Exception { | ||
if(pulsarClient == null) { | ||
log.error("Cann't find the pulsar client"); | ||
} | ||
org.apache.pulsar.client.api.Consumer<byte[]> consumer = pulsarClient.newConsumer() | ||
.topic(topic) | ||
.subscriptionName(properties.getProperty("consumerGroup")) | ||
.subscribe(); | ||
task = new SubscribeTask(topic, consumer, eventListener); | ||
task.start(); | ||
} | ||
|
||
@Override | ||
public void unsubscribe(String topic) { | ||
|
||
} | ||
|
||
@Override | ||
public void registerEventListener(EventListener listener) { | ||
this.eventListener = listener; | ||
} | ||
|
||
@Override | ||
public void updateOffset(List<CloudEvent> cloudEvents, AbstractContext context) { | ||
|
||
} | ||
|
||
@Override | ||
public boolean isStarted() { | ||
return this.started.get(); | ||
} | ||
|
||
@Override | ||
public boolean isClosed() { | ||
return !this.isStarted(); | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
try { | ||
this.started.compareAndSet(true, false); | ||
this.pulsarClient.close(); | ||
if(task != null) { | ||
task.stopRead(); | ||
} | ||
} catch (Exception ignored) { | ||
// ignored | ||
|
||
@Override | ||
public boolean isStarted() { | ||
return this.started.get(); | ||
} | ||
|
||
@Override | ||
public boolean isClosed() { | ||
return !this.isStarted(); | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
try { | ||
this.started.compareAndSet(true, false); | ||
this.pulsarClient.close(); | ||
if (task != null) { | ||
task.stopRead(); | ||
} | ||
} catch (Exception ignored) { | ||
// ignored | ||
} | ||
} | ||
} | ||
|
||
} |
108 changes: 64 additions & 44 deletions
108
...or-pulsar/src/main/java/org/apache/eventmesh/connector/pulsar/consumer/SubscribeTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,82 @@ | ||
/* | ||
* 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.pulsar.consumer; | ||
|
||
import io.cloudevents.CloudEvent; | ||
import io.cloudevents.core.provider.EventFormatProvider; | ||
import io.cloudevents.jackson.JsonFormat; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.eventmesh.api.EventListener; | ||
import org.apache.eventmesh.api.EventMeshAction; | ||
import org.apache.eventmesh.api.EventMeshAsyncConsumeContext; | ||
|
||
import org.apache.pulsar.client.api.Consumer; | ||
import org.apache.pulsar.client.api.Message; | ||
import org.apache.pulsar.client.api.PulsarClientException; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import io.cloudevents.CloudEvent; | ||
import io.cloudevents.core.provider.EventFormatProvider; | ||
import io.cloudevents.jackson.JsonFormat; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class SubscribeTask extends Thread { | ||
|
||
private final Consumer<byte[]> consumer; | ||
private final EventListener listener; | ||
private final AtomicBoolean running = new AtomicBoolean(true); | ||
|
||
public SubscribeTask(String name, Consumer<byte[]> consumer, EventListener listener) { | ||
super(name); | ||
this.consumer = consumer; | ||
this.listener = listener; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
log.debug("New Consumer: {} for pulsar connector started", consumer.getSubscription()); | ||
while(running.get()) { | ||
try { | ||
Message<byte[]> msg = consumer.receive(); | ||
CloudEvent cloudEvent = EventFormatProvider | ||
.getInstance() | ||
.resolveFormat(JsonFormat.CONTENT_TYPE) | ||
.deserialize(msg.getData()); | ||
EventMeshAsyncConsumeContext consumeContext = new EventMeshAsyncConsumeContext() { | ||
@Override | ||
public void commit(EventMeshAction action) { | ||
|
||
} | ||
}; | ||
listener.consume(cloudEvent, consumeContext); | ||
consumer.acknowledge(msg); | ||
} catch (PulsarClientException ex) { | ||
log.warn("Access the pulsar eventStore with exeption: {}",ex.getMessage()); | ||
} | ||
private final Consumer<byte[]> consumer; | ||
private final EventListener listener; | ||
private final AtomicBoolean running = new AtomicBoolean(true); | ||
|
||
public SubscribeTask(String name, Consumer<byte[]> consumer, EventListener listener) { | ||
super(name); | ||
this.consumer = consumer; | ||
this.listener = listener; | ||
} | ||
} | ||
|
||
public void stopRead() { | ||
running.compareAndSet(true, false); | ||
try { | ||
this.consumer.close(); | ||
} catch (PulsarClientException ex) { | ||
log.warn("Pulsar Consumer closed with exeption: {}", ex.getMessage()); | ||
|
||
@Override | ||
public void run() { | ||
log.debug("New Consumer: {} for pulsar connector started", consumer.getSubscription()); | ||
while (running.get()) { | ||
try { | ||
Message<byte[]> msg = consumer.receive(); | ||
CloudEvent cloudEvent = EventFormatProvider | ||
.getInstance() | ||
.resolveFormat(JsonFormat.CONTENT_TYPE) | ||
.deserialize(msg.getData()); | ||
EventMeshAsyncConsumeContext consumeContext = new EventMeshAsyncConsumeContext() { | ||
@Override | ||
public void commit(EventMeshAction action) { | ||
|
||
} | ||
}; | ||
listener.consume(cloudEvent, consumeContext); | ||
consumer.acknowledge(msg); | ||
} catch (PulsarClientException ex) { | ||
log.warn("Access the pulsar eventStore with exeption: {}", ex.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
public void stopRead() { | ||
running.compareAndSet(true, false); | ||
try { | ||
this.consumer.close(); | ||
} catch (PulsarClientException ex) { | ||
log.warn("Pulsar Consumer closed with exeption: {}", ex.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters