nstream.adapter.kafka.KafkaPublishingAgent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nstream-adapter-kafka Show documentation
Show all versions of nstream-adapter-kafka Show documentation
Templates for consuming from and producing to Kafka topics with Swim
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://redis.com/legal/rsalv2-agreement/
//
// 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 nstream.adapter.kafka;
import java.util.Properties;
import nstream.adapter.common.AdapterSettings;
import nstream.adapter.common.egress.PublisherAgent;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import swim.structure.Value;
public abstract class KafkaPublishingAgent
extends PublisherAgent> {
public KafkaPublishingAgent() {
}
protected Producer kafkaProducer;
protected Producer kafkaProducer() {
return this.kafkaProducer;
}
protected void assignProducer(Producer producer) {
if (this.kafkaProducer != null) {
throw new RuntimeException(nodeUri() + ": producer already assigned");
}
this.kafkaProducer = producer;
}
protected void assignProducer(Properties loadConf, Runnable onSuccess) {
if (this.kafkaProducer != null) {
throw new RuntimeException(nodeUri() + ": producer already assigned");
}
execute(() -> {
try {
this.kafkaProducer = new KafkaProducer<>(loadConf);
} catch (Exception e) {
throw new RuntimeException(nodeUri() + ": failed to assign producer", e);
}
onSuccess.run();
});
}
@Override
protected AdapterSettings parseEgressSettings(Value prop) {
return null; // FIXME
}
@Override
protected void publish(ProducerRecord publishable) {
if (this.kafkaProducer == null) {
throw new RuntimeException(nodeUri() + ": can't publish before assigning producer");
}
this.kafkaProducer.send(publishable, (m, e) -> {
if (e != null) {
didFail(new RuntimeException(nodeUri()
+ ": exception thrown while producing message with offset " + m.offset(), e));
}
});
}
@Override
public void didStart() {
info(nodeUri() + ": didStart");
stagePublication();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy