發布者確認
有兩種機制可以取得發布訊息的結果;在每種情況下,連線工廠都必須將 publisherConfirmType
設定為 ConfirmType.CORRELATED
。「舊版」機制是將 confirmAckChannel
設定為訊息通道的 bean 名稱,您可以從該通道非同步檢索確認;否定確認會傳送到錯誤通道(如果已啟用)- 請參閱 錯誤通道。
版本 3.1 中新增的建議機制是使用關聯資料標頭,並透過其 Future<Confirm>
屬性等待結果。這對於批次監聽器特別有用,因為您可以在等待結果之前傳送多個訊息。若要使用此技術,請將 useConfirmHeader
屬性設定為 true。以下簡單應用程式是使用此技術的範例
spring.cloud.stream.bindings.input-in-0.group=someGroup
spring.cloud.stream.bindings.input-in-0.consumer.batch-mode=true
spring.cloud.stream.source=output
spring.cloud.stream.bindings.output-out-0.producer.error-channel-enabled=true
spring.cloud.stream.rabbit.bindings.output-out-0.producer.useConfirmHeader=true
spring.cloud.stream.rabbit.bindings.input-in-0.consumer.auto-bind-dlq=true
spring.cloud.stream.rabbit.bindings.input-in-0.consumer.batch-size=10
spring.rabbitmq.publisher-confirm-type=correlated
spring.rabbitmq.publisher-returns=true
@SpringBootApplication
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Autowired
private StreamBridge bridge;
@Bean
Consumer<List<String>> input() {
return list -> {
List<MyCorrelationData> results = new ArrayList<>();
list.forEach(str -> {
log.info("Received: " + str);
MyCorrelationData corr = new MyCorrelationData(UUID.randomUUID().toString(), str);
results.add(corr);
this.bridge.send("output-out-0", MessageBuilder.withPayload(str.toUpperCase())
.setHeader(AmqpHeaders.PUBLISH_CONFIRM_CORRELATION, corr)
.build());
});
results.forEach(correlation -> {
try {
Confirm confirm = correlation.getFuture().get(10, TimeUnit.SECONDS);
log.info(confirm + " for " + correlation.getPayload());
if (correlation.getReturnedMessage() != null) {
log.error("Message for " + correlation.getPayload() + " was returned ");
// throw some exception to invoke binder retry/error handling
}
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
}
catch (ExecutionException | TimeoutException e) {
throw new IllegalStateException(e);
}
});
};
}
@Bean
public ApplicationRunner runner(BatchingRabbitTemplate template) {
return args -> IntStream.range(0, 10).forEach(i ->
template.convertAndSend("input-in-0", "input-in-0.rbgh303", "foo" + i));
}
@Bean
public BatchingRabbitTemplate template(CachingConnectionFactory cf, TaskScheduler taskScheduler) {
BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(10, 1000000, 1000);
return new BatchingRabbitTemplate(cf, batchingStrategy, taskScheduler);
}
}
class MyCorrelationData extends CorrelationData {
private final String payload;
MyCorrelationData(String id, String payload) {
super(id);
this.payload = payload;
}
public String getPayload() {
return this.payload;
}
}
如您所見,我們傳送每則訊息,然後等待發布結果。如果訊息無法路由,則關聯資料會在 future 完成之前填入傳回的訊息。
關聯資料必須提供唯一的 id ,以便框架可以執行關聯。 |
您不能同時設定 useConfirmHeader
和 confirmAckChannel
,但是當 useConfirmHeader
為 true 時,您仍然可以在錯誤通道中接收傳回的訊息,但是使用關聯標頭更方便。