43 lines
1.2 KiB
Java
43 lines
1.2 KiB
Java
package com.hyc.kafka.demo.controller;
|
|
|
|
import com.hyc.kafka.demo.config.KafkaSendResultHandler;
|
|
import org.springframework.kafka.core.KafkaTemplate;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import java.util.UUID;
|
|
|
|
/**
|
|
* 测试kafka控制层
|
|
*
|
|
* @author YouChe·He
|
|
* @ClassName: KafkaController
|
|
* @Description: 测试kafka控制层
|
|
* @CreateTime: 2024/6/6 14:42
|
|
*/
|
|
@RestController
|
|
public class KafkaController {
|
|
|
|
|
|
private KafkaTemplate<Object, Object> kafkaTemplate;
|
|
|
|
public KafkaController(KafkaTemplate<Object, Object> kafkaTemplate, KafkaSendResultHandler kafkaSendResultHandler) {
|
|
this.kafkaTemplate = kafkaTemplate;
|
|
this.kafkaTemplate.setProducerListener(kafkaSendResultHandler);
|
|
}
|
|
|
|
@GetMapping("/send")
|
|
@Transactional
|
|
public void sendMessage(String message) {
|
|
System.out.println("呼呼呼");
|
|
|
|
kafkaTemplate.send("topic1", UUID.randomUUID().toString(), message);
|
|
}
|
|
|
|
}
|
|
|
|
|