消息中间件——RabbitMQRabbitMQ整合Spring Cloud Stream实战!(全)
@EnableBinding(Sink.class)
public class LoggingConsumer {
private static Logger logger = LoggerFactory.getLogger(LoggingConsumer.class);
@StreamListener(Sink.INPUT)
public void handle(Person person) {
logger.info("Received: " + person);
}
}
@EnableBinding(Source.class)
public class MainApplication {
private static Logger logger = LoggerFactory.getLogger(MainApplication.class);
@Autowired
private Source source;
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
@Bean
public ApplicationRunner runner() {
return args -> {
source.output().send(MessageBuilder.withPayload(new Person("John", "Doe")).build());
logger.info("Message sent");
};
}
}
在这个例子中,我们定义了一个消息接收者LoggingConsumer
,它使用@StreamListener
注解来监听输入端点,并将接收到的消息打印到日志中。MainApplication
类则通过Source
绑定,并在程序启动时发送一条消息到RabbitMQ。这个例子展示了如何使用Spring Cloud Stream与RabbitMQ进行消息的发送和接收。
评论已关闭