All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.wicp.tams.common.kafka.KafkaAssitInst Maven / Gradle / Ivy

The newest version!
/*
 * **********************************************************************
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称:common
 * 项目描述:公共的工具集
 * 版权说明:本软件属andy.zhou([email protected])所有。
 * ***********************************************************************
 */
package net.wicp.tams.common.kafka;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.kafka.clients.producer.KafkaProducer;

import lombok.extern.slf4j.Slf4j;
import net.wicp.tams.common.Conf;

/***
 * common.others.kafka.key.serializer=org.apache.kafka.common.serialization.StringSerializer
 * common.others.kafka.value.serializer=org.apache.kafka.common.serialization.ByteArraySerializer
 * 
 * @author Administrator
 *
 */
@Slf4j
public class KafkaAssitInst {
	static {
		// 防止自定义classload的时错过默认的加载配置,再次加载配置文件,不覆盖已存在的项
		Conf.overConf("/common-kafka.properties", KafkaAssitInst.class, false);
	}

	private static KafkaAssitInst inst = new KafkaAssitInst();

	private Map> map = new HashMap<>();

	private KafkaAssitInst() {
	}

	public static KafkaAssitInst getInst() {
		return inst;
	}

	public  KafkaProducer getKafkaProducer(Class classz) {
		String key = classz.getName();
		if (map.isEmpty() || !map.containsKey(key)) {
			Properties prop = KafkaAssist.getProps(true);
			String valueprop = KafkaAssist.getValueProp(classz, true);
			prop.put("value.serializer", valueprop);
			prop.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
			log.info("kafka producer 参数:");
			for (Object propele : prop.keySet()) {
				log.info("{}:{}", propele, prop.get(propele));
			}
			// link和batch不用加
			KafkaProducer kafkaProducer = new KafkaProducer<>(prop);
			map.put(key, kafkaProducer);
			return kafkaProducer;
		} else {
			return (KafkaProducer) map.get(key);
		}
	}

	public void startkafkaCousumerS(boolean async,String groupId, String topic, IConsumer doConsumer, int hostNum) {
		KafkaConsumerGroup group = new KafkaConsumerGroupS(async,groupId, topic, doConsumer, hostNum);
		group.start();
	}

	public void startkafkaCousumerS(boolean async,String topic, IConsumer doConsumer, int hostNum) {
		KafkaConsumerGroup group = new KafkaConsumerGroupS(async,topic, doConsumer, hostNum);
		group.start();
	}

	public void startkafkaCousumerB(boolean async,String groupId, String topic, IConsumer doConsumer, int hostNum) {
		KafkaConsumerGroup group = new KafkaConsumerGroupB(async,groupId, topic, doConsumer, hostNum);
		group.start();
	}

	public void startkafkaCousumerB(boolean async,String topic, IConsumer doConsumer, int hostNum) {
		KafkaConsumerGroup group = new KafkaConsumerGroupB(async,topic, doConsumer, hostNum);
		group.start();
	}

	public void startKafkaConsumerThreadB(String groupId, String topic, IConsumer doConsumer, int threadNum) {
		KafkaConsumerThreadB group = new KafkaConsumerThreadB(groupId, topic, doConsumer);
		group.start(threadNum);
	}

	public void startKafkaConsumerThreadB(String topic, IConsumer doConsumer, int threadNum) {
		KafkaConsumerThreadB group = new KafkaConsumerThreadB(topic, doConsumer);
		group.start(threadNum);
	}

	public void startKafkaConsumerThreadS(String groupId, String topic, IConsumer doConsumer, int threadNum) {
		KafkaConsumerThreadS group = new KafkaConsumerThreadS(groupId, topic, doConsumer);
		group.start(threadNum);
	}

	public void startKafkaConsumerThreadS(String topic, IConsumer doConsumer, int threadNum) {
		KafkaConsumerThreadS group = new KafkaConsumerThreadS(topic, doConsumer);
		group.start(threadNum);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy