接收批量消息

使用 RabbitMQ 绑定器,消费者绑定处理两种类型的批次

生产者创建的批次

通常,如果生产者绑定具有batch-enabled=true(参见Rabbit 生产者属性),或者消息是由BatchingRabbitTemplate创建的,批次的元素将作为对监听器方法的单个调用返回。从 3.0 版本开始,如果将spring.cloud.stream.bindings.<name>.consumer.batch-mode设置为true,则任何此类批次都可以作为List<?>呈现给监听器方法。

消费者端批处理

从 3.1 版本开始,可以配置消费者将多个入站消息组装成一个批次,该批次作为转换后的有效负载的List<?>呈现给应用程序。以下简单应用程序演示了如何使用此技术

spring.cloud.stream.bindings.input-in-0.group=someGroup

spring.cloud.stream.bindings.input-in-0.consumer.batch-mode=true

spring.cloud.stream.rabbit.bindings.input-in-0.consumer.enable-batching=true
spring.cloud.stream.rabbit.bindings.input-in-0.consumer.batch-size=10
spring.cloud.stream.rabbit.bindings.input-in-0.consumer.receive-timeout=200
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Bean
	Consumer<List<Thing>> input() {
		return list -> {
			System.out.println("Received " + list.size());
			list.forEach(thing -> {
				System.out.println(thing);

				// ...

			});
		};
	}

	@Bean
	public ApplicationRunner runner(RabbitTemplate template) {
		return args -> {
			template.convertAndSend("input-in-0.someGroup", "{\"field\":\"value1\"}");
			template.convertAndSend("input-in-0.someGroup", "{\"field\":\"value2\"}");
		};
	}

	public static class Thing {

		private String field;

		public Thing() {
		}

		public Thing(String field) {
			this.field = field;
		}

		public String getField() {
			return this.field;
		}

		public void setField(String field) {
			this.field = field;
		}

		@Override
		public String toString() {
			return "Thing [field=" + this.field + "]";
		}

	}

}
Received 2
Thing [field=value1]
Thing [field=value2]

批次中的消息数量由batch-sizereceive-timeout属性指定;如果receive-timeout到期且没有新消息,则会传递一个“短”批次。

消费者端批处理仅在container-type=simple(默认值)下受支持。

如果您希望检查消费者端批处理消息的标头,则应使用Message<List<?>>进行消费;标头是标头AmqpInboundChannelAdapter.CONSOLIDATED_HEADERS中的List<Map<String, Object>>,每个有效负载元素的标头位于相应的索引中。同样,这里是一个简单的示例

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Bean
	Consumer<Message<List<Thing>>> input() {
		return msg -> {
			List<Thing> things = msg.getPayload();
			System.out.println("Received " + things.size());
			List<Map<String, Object>> headers =
					(List<Map<String, Object>>) msg.getHeaders().get(AmqpInboundChannelAdapter.CONSOLIDATED_HEADERS);
			for (int i = 0; i < things.size(); i++) {
				System.out.println(things.get(i) + " myHeader=" + headers.get(i).get("myHeader"));

				// ...

			}
		};
	}

	@Bean
	public ApplicationRunner runner(RabbitTemplate template) {
		return args -> {
			template.convertAndSend("input-in-0.someGroup", "{\"field\":\"value1\"}", msg -> {
				msg.getMessageProperties().setHeader("myHeader", "headerValue1");
				return msg;
			});
			template.convertAndSend("input-in-0.someGroup", "{\"field\":\"value2\"}", msg -> {
				msg.getMessageProperties().setHeader("myHeader", "headerValue2");
				return msg;
			});
		};
	}

	public static class Thing {

		private String field;

		public Thing() {
		}

		public Thing(String field) {
			this.field = field;
		}

		public String getfield() {
			return this.field;
		}

		public void setfield(String field) {
			this.field = field;
		}

		@Override
		public String toString() {
			return "Thing [field=" + this.field + "]";
		}

	}

}
Received 2
Thing [field=value1] myHeader=headerValue1
Thing [field=value2] myHeader=headerValue2