Polish "Add Kafka sample"
Closes gh-11597
This commit is contained in:
@@ -19,10 +19,11 @@ import org.springframework.kafka.annotation.KafkaListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Consumer {
|
||||
class Consumer {
|
||||
|
||||
@KafkaListener(topics = "myTopic")
|
||||
@KafkaListener(topics = "testTopic")
|
||||
public void processMessage(SampleMessage message) {
|
||||
System.out.println("consumer has received message : [" + message + "]");
|
||||
System.out.println("Received sample message [" + message + "]");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,18 +15,21 @@
|
||||
*/
|
||||
package sample.kafka;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.kafka.core.KafkaTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Producer {
|
||||
|
||||
@Autowired
|
||||
private KafkaTemplate kafkaTemplate;
|
||||
private final KafkaTemplate<Object, SampleMessage> kafkaTemplate;
|
||||
|
||||
Producer(KafkaTemplate<Object, SampleMessage> kafkaTemplate) {
|
||||
this.kafkaTemplate = kafkaTemplate;
|
||||
}
|
||||
|
||||
public void send(SampleMessage message) {
|
||||
kafkaTemplate.send("myTopic", message);
|
||||
System.out.println("producer has sent message.");
|
||||
this.kafkaTemplate.send("testTopic", message);
|
||||
System.out.println("Sent sample message [" + message + "]");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,8 +15,11 @@
|
||||
*/
|
||||
package sample.kafka;
|
||||
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SampleKafkaApplication {
|
||||
@@ -24,4 +27,10 @@ public class SampleKafkaApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SampleKafkaApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ApplicationRunner runner(Producer producer) {
|
||||
return args -> producer.send(new SampleMessage(1, "A simple test message"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,39 +15,37 @@
|
||||
*/
|
||||
package sample.kafka;
|
||||
|
||||
public class SampleMessage {
|
||||
private Integer id;
|
||||
private String message;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
public class SampleMessage {
|
||||
|
||||
private final Integer id;
|
||||
|
||||
private final String message;
|
||||
|
||||
@JsonCreator
|
||||
public SampleMessage(@JsonProperty("id") Integer id,
|
||||
@JsonProperty("message") String message) {
|
||||
this.id = id;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
public Integer getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public SampleMessage() {
|
||||
}
|
||||
|
||||
public SampleMessage(Integer id, String message) {
|
||||
this.id = id;
|
||||
this.message = message;
|
||||
return this.message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SampleMessage{" +
|
||||
"id=" + id +
|
||||
", message='" + message + '\'' +
|
||||
'}';
|
||||
final StringBuilder sb = new StringBuilder("SampleMessage{");
|
||||
sb.append("id=").append(this.id);
|
||||
sb.append(", message='").append(this.message).append('\'');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user