kafka消费模型(图片引用自胡夕大神的博客:http://www.cnblogs.com/huxi2b/p/6124937.html):
下面直接上代码:
package cn.com.duiba.test;
public class Cache {
private static Cache instance = null;
/**
* kafka 拉取数据线程的状态, false:停止获取数据
*/
private boolean kafkaThreadStatus = true;
public static Cache getInstance(){
if(instance == null){
init();
}
return instance;
}
public synchronized static void init(){
if(instance == null){
instance = new Cache();
}
}
public boolean isKafkaThreadStatus() {
return kafkaThreadStatus;
}
public void setKafkaThreadStatus(boolean kafkaThreadStatus) {
this.kafkaThreadStatus = kafkaThreadStatus;
}
}
package cn.com.duiba.test;
import java.util.concurrent.Callable;
import java.util.concurrent.Semaphore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class WorkThread implements Callable<String>{
private static Logger logger = LoggerFactory.getLogger(WorkThread.class);
/**
* 记录解析失败的日志或者发送到kafka集群失败的日志
*/
private String topic;
private String message;
private Semaphore semaphore;
public WorkThread(String topic, String message, Semaphore semaphore){
this.topic = topic;
this.message = message;
this.sem