问题
Now I have following flow:
flow -> flow.channel(some_channel())
.....
.gateway(anotherFlow, idempotentByHeader(OBJECT_ID_HEADER));
Consumer<GatewayEndpointSpec> idempotentByHeader(String objectIdHeader) {
return endpointSpec -> endpointSpec.advice(idempotentByHeaderInterceptor(objectIdHeader)).errorChannel(errorChannel());
}
default IdempotentReceiverInterceptor idempotentByHeaderInterceptor(String header) {
MessageProcessor<String> headerSelector = message -> headerExpression(header).apply(message);
var interceptor = new IdempotentReceiverInterceptor(new MetadataStoreSelector(headerSelector, idempotencyStore()));
interceptor.setDiscardChannel(idempotentDiscardChannel());
return interceptor;
}
The problem here that:
anotherFlow
is finished with MessageHandler
which is void
so anotherFlow doesn't return anything.
I tried to use following approach:
flow -> flow.channel(some_channel())
.....
.wireTap(anotherFlow, idempotentByHeader(OBJECT_ID_HEADER));
but compiler complains because of idempotentByHeader
return type so I tried to do following:
default Consumer<WireTapSpec> idempotentByHeader(String objectIdHeader) {
return endpointSpec -> endpointSpec.advice(idempotentByHeaderInterceptor(objectIdHeader)).errorChannel(errorChannel());
}
but WireTapSpec doesn't have advice method.
how to fix it ?
P.S.
I was able to write with changing return type of idempotentByHeader
.wireTap(anotherFlow)
.enrich(idempotentByHeader(OBJECT_ID_HEADER));
But now app can't start because of:
Caused by: java.lang.IllegalStateException: If the errorChannel is set, then the requestChannel must not be null
at org.springframework.util.Assert.state(Assert.java:73)
at org.springframework.integration.transformer.ContentEnricher.doInit(ContentEnricher.java:277)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.onInit(AbstractReplyProducingMessageHandler.java:98)
at org.springframework.integration.context.IntegrationObjectSupport.afterPropertiesSet(IntegrationObjectSupport.java:214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1862)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1799)
... 42 common frames omitted
回答1:
OK. You are missing the fact that WireTap
is a Channel Interceptor. It is not an endpoint like gateway to be able to accept an idempotent receiver interceptor.
I'm not sure what is your goal with that idempotentByHeaderInterceptor
, but headers are really passed in the message which is going to be sent to that WireTap. Therefore you get access to the headers in the sub-flow subscribed to this WireTap.
Also your latest sample with enrich()
confuses me a little bit. Before with a gateway you tried to avoid sending the same message to that sub-flow via idempotentByHeaderInterceptor
, but now you send to wireTap
unconditionally and only after that your apply such that idempotentByHeaderInterceptor
.
So, what is the goal of your idempotentByHeaderInterceptor
and where you would like to apply it?
来源:https://stackoverflow.com/questions/58630616/how-to-pass-header-using-wiretap