1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| package com.jhj.kafka.service;
import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.kafka.core.KafkaOperations; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.support.KafkaHeaders; import org.springframework.kafka.support.SendResult; import org.springframework.messaging.Message; import org.springframework.messaging.support.MessageBuilder; import org.springframework.stereotype.Service; import org.springframework.util.concurrent.ListenableFuture; import org.springframework.util.concurrent.ListenableFutureCallback;
import javax.annotation.Resource; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException;
@Slf4j @Service public class KafkaProducerService { @Resource private KafkaTemplate<String, String> kafkaTemplate;
@Resource private KafkaTemplate<String, String> kafkaTemplateWithTransaction;
public void sendMessageSync(String topic, String key, String message) throws InterruptedException, ExecutionException, TimeoutException { kafkaTemplate.send(topic, message).get(10, TimeUnit.SECONDS); log.info("sendMessageSync => topic: {}, key: {}, message: {}", topic, key, message);
}
public void sendMessageGetResult(String topic, String key, String message) throws ExecutionException, InterruptedException { SendResult<String, String> result = kafkaTemplate.send(topic, message).get(); log.info("sendMessageSync => topic: {}, key: {}, message: {}", topic, key, message); log.info("The partition the message was sent to: " + result.getRecordMetadata().partition()); }
public void sendMessageAsync(String topic, String message) { ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(topic, message); future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() { @Override public void onFailure(Throwable throwable) { log.error("sendMessageAsync failure! topic : {}, message: {}", topic, message); }
@Override public void onSuccess(SendResult<String, String> stringStringSendResult) { log.info("sendMessageAsync success! topic: {}, message: {}", topic, message); } }); }
public void testMessageBuilder(String topic, String key, String message) throws InterruptedException, ExecutionException, TimeoutException { Message msg = MessageBuilder.withPayload(message) .setHeader(KafkaHeaders.MESSAGE_KEY, key) .setHeader(KafkaHeaders.TOPIC, topic) .setHeader(KafkaHeaders.PREFIX,"kafka_") .build(); kafkaTemplate.send(msg).get();
}
public void sendMessageInTransaction(String topic, String key, String message) { kafkaTemplateWithTransaction.executeInTransaction(new KafkaOperations.OperationsCallback<String, String, Object>() { @Override public Object doInOperations(KafkaOperations<String, String> kafkaOperations) { kafkaOperations.send(topic, key, message); throw new RuntimeException("12"); } }); }
}
|