|
| 1 | +package demo.uniquemessage; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import java.util.Random; |
| 5 | +import java.util.UUID; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | +import javax.annotation.PostConstruct; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.context.annotation.Profile; |
| 12 | +import org.springframework.kafka.core.KafkaTemplate; |
| 13 | +import org.springframework.kafka.support.KafkaHeaders; |
| 14 | +import org.springframework.kafka.support.SendResult; |
| 15 | +import org.springframework.stereotype.Component; |
| 16 | +import org.springframework.util.concurrent.ListenableFuture; |
| 17 | +import org.springframework.util.concurrent.ListenableFutureCallback; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author zacconding |
| 21 | + * @Date 2018-12-19 |
| 22 | + * @GitHub : https://github.com/zacscoding |
| 23 | + */ |
| 24 | +@Profile("uniquemessage") |
| 25 | +@Slf4j(topic = "[PRODUCER]") |
| 26 | +@Component |
| 27 | +public class UniqueMessageProducer { |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private KafkaTemplate<String, UniqueMessage> kafkaTemplate; |
| 31 | + private final String topic = "unique-message"; |
| 32 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 33 | + private Random random = new Random(); |
| 34 | + |
| 35 | + @PostConstruct |
| 36 | + public void setUp() { |
| 37 | + Thread produceTask = new Thread(() -> { |
| 38 | + log.info("## Start producer task"); |
| 39 | + try { |
| 40 | + while (!Thread.currentThread().isInterrupted()) { |
| 41 | + UniqueMessage message = new UniqueMessage(generateRandomId(), geneateMessage()); |
| 42 | + |
| 43 | + ProducerRecord<String, UniqueMessage> record = new ProducerRecord<>( |
| 44 | + topic, message); |
| 45 | + record.headers().add(KafkaHeaders.MESSAGE_KEY, message.getId().getBytes()); |
| 46 | + ListenableFuture<SendResult<String, UniqueMessage>> result = kafkaTemplate |
| 47 | + .send(record); |
| 48 | + |
| 49 | + result.addCallback(new ListenableFutureCallback<SendResult<String, UniqueMessage>>() { |
| 50 | + @Override |
| 51 | + public void onFailure(Throwable error) { |
| 52 | + log.info("## Failed to send {}. > {}", message, error.getMessage()); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void onSuccess(SendResult<String, UniqueMessage> result) { |
| 57 | + log.info("## Success to send {}", result.toString()); |
| 58 | + } |
| 59 | + }); |
| 60 | + TimeUnit.SECONDS.sleep(3L); |
| 61 | + } |
| 62 | + } catch (Exception e) { |
| 63 | + log.error("## Exception occur while producing message", e); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + produceTask.setDaemon(true); |
| 68 | + produceTask.start(); |
| 69 | + } |
| 70 | + |
| 71 | + private String generateRandomId() { |
| 72 | + return String.valueOf( |
| 73 | + random.nextInt(5) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + private String geneateMessage() { |
| 78 | + return UUID.randomUUID().toString(); |
| 79 | + } |
| 80 | +} |
0 commit comments