Skip to content

Commit

Permalink
Fixes ISSUE apache#1238
Browse files Browse the repository at this point in the history
Motivation

fix checkstyle

Modifications

Documentation
  • Loading branch information
fengyongshe committed Sep 15, 2022
1 parent 20da206 commit f34be3a
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 127 deletions.
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
}
}
}

}
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());
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

package org.apache.eventmesh.connector.pulsar.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.eventmesh.api.SendCallback;
import org.apache.eventmesh.api.exception.ConnectorRuntimeException;
import org.apache.eventmesh.api.exception.OnExceptionContext;
import org.apache.eventmesh.connector.pulsar.utils.CloudEventUtils;

import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;

import java.util.Properties;
import java.util.concurrent.TimeUnit;
Expand All @@ -33,7 +33,8 @@
import io.cloudevents.CloudEvent;
import io.cloudevents.core.provider.EventFormatProvider;
import io.cloudevents.jackson.JsonFormat;
import org.apache.pulsar.client.api.PulsarClientException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ProducerImpl extends AbstractProducer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* 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.config;

import org.junit.BeforeClass;
Expand All @@ -7,17 +24,17 @@

public class ClientConfigurationTest {

private static ClientConfiguration config;
private static ClientConfiguration config;

@BeforeClass
public static void init() {
config = new ClientConfiguration();
config.init();
}
@BeforeClass
public static void init() {
config = new ClientConfiguration();
config.init();
}

@Test
public void getServiceUrl() {
assertEquals(config.serviceAddr, "pulsar://127.0.0.1:6650");
}
@Test
public void getServiceUrl() {
assertEquals(config.serviceAddr, "pulsar://127.0.0.1:6650");
}

}

0 comments on commit f34be3a

Please sign in to comment.