
com.zaradai.gluon.client.KafkaClient Maven / Gradle / Ivy
Show all versions of gluon-core Show documentation
/**
* Copyright 2016 Zaradai
*
* Licensed 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 com.zaradai.gluon.client;
import com.google.common.eventbus.EventBus;
import com.zaradai.gluon.codecs.FstCodec;
import com.zaradai.gluon.kafka.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.google.common.base.Preconditions.checkNotNull;
public class KafkaClient extends BaseClient {
private static final Logger LOG = LoggerFactory.getLogger(KafkaClient.class);
static final String CLIENT_BUS_ID = "client";
static final String CLIENT_GROUP = "Client-Group";
private final KafkaClientEventAdaptor eventAdaptor;
public KafkaClient(KafkaConfig config) {
this(config, new DefaultProducerFactory(), new DefaultConsumerFactory());
}
public KafkaClient(KafkaConfig config, ProducerFactory producerFactory, ConsumerFactory consumerFactory) {
super(new EventBus(CLIENT_BUS_ID));
checkNotNull(config, "Invalid config.");
// assign a client group
config.getConsumerConfig().setGroupId(CLIENT_GROUP);
// adapt the client event bus using kafka messages
eventAdaptor = new KafkaClientEventAdaptor(getEventBus(), new FstCodec(), config, producerFactory, consumerFactory);
}
@Override
protected void startUp() throws Exception {
super.startUp();
eventAdaptor.startAsync().awaitRunning();
LOG.debug("Kafka client started.");
}
@Override
protected void shutDown() throws Exception {
super.shutDown();
eventAdaptor.stopAsync().awaitTerminated();
LOG.debug("Kafka client shutdown.");
}
}