如果要對JMS BROKER生產和消費MESSAGE,一種方式是用JmsTemplate發送和消費消息,另一種方式是SPRING INTEGRATION。
SPRING INTEGRATION是實現了EIP模式的一種框架,即使用CHANNEL和JMS-INBOUND-ADAPTER、JMS-OUTBOUND-ADAPTER,完全脫離了JmsTemplate的API。
如果需要實現這種場景:從BROKER取一條消息,處理消息,且處理途中不要再從BROKER再取消息,處理完后再取消息,再處理。
這樣要求手動開始和停止JMS LISTENER,即手動開始和停止JMS-INBOUND-ADAPTER、JMS-OUTBOUND-ADAPTER。
@Bean
@InboundChannelAdapter(value = "loaderResponseChannel")
public MessageSource loaderResponseSource() throws Exception {
return Jms
.inboundAdapter(oracleConnectionFactory())
.configureJmsTemplate(
t -> t.deliveryPersistent(true)
.jmsMessageConverter(jacksonJmsMessageConverter())
).destination(jmsInbound).get();
}
當使用@InboundChannelAdapter時,會自動注冊一個SourcePollingChannelAdapter ,但這個名字比較長:configrationName.loaderResponseSource.inboundChannelAdapter。
呼叫這個實例的start()和stop()方法即可。
@Bean
public IntegrationFlow controlBusFlow() {
return IntegrationFlows.from("controlBus")
.controlBus()
.get();
}
Message operation = MessageBuilder.withPayload("@configrationName.loaderResponseSource.inboundChannelAdapter.start()").build();
operationChannel.send(operation)
https://stackoverflow.com/questions/45632469/shutdown-spring-integration-with-jms-inboundadapterhttps://docs.spring.io/spring-integration/docs/5.0.7.RELEASE/reference/html/system-management-chapter.html#control-bushttps://github.com/spring-projects/spring-integration-java-dsl/blob/master/src/test/java/org/springframework/integration/dsl/test/jms/JmsTests.javahttps://stackoverflow.com/questions/50428552/how-to-stop-or-suspend-polling-after-batch-job-fail