SpringCloud 使用@StreamListener注釋

2023-11-25 09:15 更新

作為對(duì)Spring Integration支持的補(bǔ)充,Spring Cloud Stream提供了自己的@StreamListener注釋,其模仿其他Spring消息注釋(@MessageMapping@JmsListener,@RabbitListener等)并提供便利,例如基于內(nèi)容的路由等。

@EnableBinding(Sink.class)
public class VoteHandler {

  @Autowired
  VotingService votingService;

  @StreamListener(Sink.INPUT)
  public void handle(Vote vote) {
    votingService.record(vote);
  }
}

與其他Spring消息傳遞方法一樣,方法參數(shù)可以用@Payload,@Headers@Header進(jìn)行注釋。

對(duì)于返回?cái)?shù)據(jù)的方法,必須使用@SendTo批注為該方法返回的數(shù)據(jù)指定輸出綁定目標(biāo),如以下示例所示:

@EnableBinding(Processor.class)
public class TransformProcessor {

  @Autowired
  VotingService votingService;

  @StreamListener(Processor.INPUT)
  @SendTo(Processor.OUTPUT)
  public VoteResult handle(Vote vote) {
    return votingService.record(vote);
  }
}
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)