How to pass header using wireTap?

主宰稳场 提交于 2019-12-11 17:18:54

问题


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:

anotherFlowis 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!