kafka 的pom文件_kafka与Spring的集成

packagecom.git.kafka.producer;importjava.util.HashMap;importjava.util.Map;importjava.util.Random;importjava.util.concurrent.ExecutionException;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.kafka.core.KafkaTemplate;importorg.springframework.kafka.support.SendResult;importorg.springframework.stereotype.Component;importorg.springframework.util.concurrent.ListenableFuture;importcom.alibaba.fastjson.JSON;importcom.git.kafka.constant.KafkaMesConstant;/*** kafkaProducer模板

* 使用此模板发送消息

*@authorwangb

**/@Componentpublic classKafkaProducerServer{

@Autowiredprivate KafkaTemplatekafkaTemplate;/*** kafka发送消息模板

*@paramtopic 主题

*@paramvalue messageValue

*@paramifPartition 是否使用分区 0是\1不是

*@parampartitionNum 分区数 如果是否使用分区为0,分区数必须大于0

*@paramrole 角色:bbc app erp...*/

public MapsndMesForTemplate(String topic, Object value, String ifPartition,

Integer partitionNum, String role){

String key= role+"-"+value.hashCode();

String valueString=JSON.toJSONString(value);if(ifPartition.equals("0")){//表示使用分区

int partitionIndex =getPartitionIndex(key, partitionNum);

ListenableFuture> result =kafkaTemplate.send(topic, partitionIndex, key, valueString);

Map res =checkProRecord(result);returnres;

}else{

ListenableFuture> result =kafkaTemplate.send(topic, key, valueString);

Map res =checkProRecord(result);returnres;

}

}/*** 根据key值获取分区索引

*@paramkey

*@parampartitionNum

*@return

*/

private int getPartitionIndex(String key, intpartitionNum){if (key == null) {

Random random= newRandom();returnrandom.nextInt(partitionNum);

}else{int result = Math.abs(key.hashCode())%partitionNum;returnresult;

}

}/*** 检查发送返回结果record

*@paramres

*@return

*/@SuppressWarnings("rawtypes")private Map checkProRecord(ListenableFuture>res){

Map m = new HashMap();if(res!=null){try{

SendResult r= res.get();//检查result结果集

/*检查recordMetadata的offset数据,不检查producerRecord*/Long offsetIndex=r.getRecordMetadata().offset();if(offsetIndex!=null && offsetIndex>=0){

m.put("code", KafkaMesConstant.SUCCESS_CODE);

m.put("message", KafkaMesConstant.SUCCESS_MES);returnm;

}else{

m.put("code", KafkaMesConstant.KAFKA_NO_OFFSET_CODE);

m.put("message", KafkaMesConstant.KAFKA_NO_OFFSET_MES);returnm;

}

}catch(InterruptedException e) {

e.printStackTrace();

m.put("code", KafkaMesConstant.KAFKA_SEND_ERROR_CODE);

m.put("message", KafkaMesConstant.KAFKA_SEND_ERROR_MES);returnm;

}catch(ExecutionException e) {

e.printStackTrace();

m.put("code", KafkaMesConstant.KAFKA_SEND_ERROR_CODE);

m.put("message", KafkaMesConstant.KAFKA_SEND_ERROR_MES);returnm;

}

}else{

m.put("code", KafkaMesConstant.KAFKA_NO_RESULT_CODE);

m.put("message", KafkaMesConstant.KAFKA_NO_RESULT_MES);returnm;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值