Spring integration Spring集成DSL:使用头文件的转换器

Spring integration Spring集成DSL:使用头文件的转换器,spring-integration,Spring Integration,我想将JavaDSL用于Spring集成,但我不知道如何在转换期间使用消息头 我以前的实现有这样一个转换器: @Transformer(inputChannel = "inputChannel", outputChannel = "outputChannel") public EventB transform( EventA eventA, @Header("a_header") String aHeader, @Header("other_hea

我想将JavaDSL用于Spring集成,但我不知道如何在转换期间使用消息头

我以前的实现有这样一个转换器:

@Transformer(inputChannel = "inputChannel", outputChannel = "outputChannel")
public EventB transform(
        EventA eventA,
        @Header("a_header") String aHeader,
        @Header("other_header") String otherHeader){
    return new EventB(eventA.getSomeField(), aHeader, otherHeader);
}
  .transform(Message.class, m -> m.getHeaders())
现在我有以下DSL:

@Bean
public IntegrationFlow aFlow(){
    return IntegrationFlows.from(EventASink.INPUT)
        .filter("headers['operation'] == 'OPERATION_A'")
        .transform() //<-- What should I do here?
        .handle(Http.outboundGateway(uri).httpMethod(HttpMethod.POST))
        .get();
}
@Bean
公共集成流aFlow(){
返回IntegrationFlows.from(EventASink.INPUT)
.filter(“标题['operation']='operation_A'”)

.transform()//因为DSL是框架的一部分,并且在您开始使用它之前就已经编译好了,所以我们无法推断出任何自定义POJO方法,因此没有像您的示例中那样干净的方法使用任何自定义头进行计数

重新使用
transform()
和这些参数注释的最简单方法是使用以下
.transform()
契约:

/**
 * Populate the {@code MessageTransformingHandler} for the {@link MethodInvokingTransformer}
 * to invoke the service method at runtime.
 * @param service the service to use.
 * @param methodName the method to invoke.
 * @return the current {@link IntegrationFlowDefinition}.
 * @see MethodInvokingTransformer
 */
public B transform(Object service, String methodName)
因此,您需要使用该方法声明一个bean,并在
服务
参数中使用它,同时在
方法名
参数中提到该方法

访问头的另一种方法是为lambda请求整个
消息
类型:

/**
 * Populate the {@link MessageTransformingHandler} instance for the provided
 * {@link GenericTransformer} for the specific {@code payloadType} to convert at
 * runtime.
 * Use {@link #transform(Class, GenericTransformer)} if you need access to the
 * entire message.
 * @param payloadType the {@link Class} for expected payload type. It can also be
 * {@code Message.class} if you wish to access the entire message in the transformer.
 * Conversion to this type will be attempted, if necessary.
 * @param genericTransformer the {@link GenericTransformer} to populate.
 * @param <P> the payload type - 'transform from' or {@code Message.class}.
 * @param <T> the target type - 'transform to'.
 * @return the current {@link IntegrationFlowDefinition}.
 * @see MethodInvokingTransformer
 * @see LambdaMessageProcessor
 */
public <P, T> B transform(Class<P> payloadType, GenericTransformer<P, T> genericTransformer) {

由于DSL是框架的一部分,并且在您开始使用它之前对其进行了编译,因此我们无法推断任何自定义POJO方法,因此没有像您的示例中那样使用任何自定义头进行计数的干净方法

重新使用
transform()
和这些参数注释的最简单方法是使用以下
.transform()
契约:

/**
 * Populate the {@code MessageTransformingHandler} for the {@link MethodInvokingTransformer}
 * to invoke the service method at runtime.
 * @param service the service to use.
 * @param methodName the method to invoke.
 * @return the current {@link IntegrationFlowDefinition}.
 * @see MethodInvokingTransformer
 */
public B transform(Object service, String methodName)
因此,您需要使用该方法声明一个bean,并在
服务
参数中使用它,同时在
方法名
参数中提到该方法

访问头的另一种方法是为lambda请求整个
消息
类型:

/**
 * Populate the {@link MessageTransformingHandler} instance for the provided
 * {@link GenericTransformer} for the specific {@code payloadType} to convert at
 * runtime.
 * Use {@link #transform(Class, GenericTransformer)} if you need access to the
 * entire message.
 * @param payloadType the {@link Class} for expected payload type. It can also be
 * {@code Message.class} if you wish to access the entire message in the transformer.
 * Conversion to this type will be attempted, if necessary.
 * @param genericTransformer the {@link GenericTransformer} to populate.
 * @param <P> the payload type - 'transform from' or {@code Message.class}.
 * @param <T> the target type - 'transform to'.
 * @return the current {@link IntegrationFlowDefinition}.
 * @see MethodInvokingTransformer
 * @see LambdaMessageProcessor
 */
public <P, T> B transform(Class<P> payloadType, GenericTransformer<P, T> genericTransformer) {

我也遇到了同样的问题。我需要标题和负载。经过大量的修补,我找到了一个解决方案。我使用了
.handle
而不是
。transform
的handle方法提供了负载和标题。 在您的情况下,它看起来像:

@Bean
public IntegrationFlow aFlow(){
return IntegrationFlows.from(EventASink.INPUT)
    .filter("headers['operation'] == 'OPERATION_A'")
    .<EventA>handle((eventA, h) -> new EventB(
        eventA.getSomeField(),
        h.get("a_header", String.class),
        h.get("other_header", String.class)))
    .handle(Http.outboundGateway(uri).httpMethod(HttpMethod.POST))
    .get();
}
@Bean
公共集成流aFlow(){
返回IntegrationFlows.from(EventASink.INPUT)
.filter(“标题['operation']='operation_A'”)
.handle((eventA,h)->新EventB(
eventA.getSomeField(),
h、 get(“a_头”,String.class),
h、 get(“其他_头”,String.class)))
.handle(Http.outboundGateway(uri).httpMethod(httpMethod.POST))
.get();
}

我也遇到了同样的问题。我需要标题和负载。经过大量的修补,我找到了一个解决方案。我使用了
.handle
而不是
。transform
的handle方法提供了负载和标题。 在您的情况下,它看起来像:

@Bean
public IntegrationFlow aFlow(){
return IntegrationFlows.from(EventASink.INPUT)
    .filter("headers['operation'] == 'OPERATION_A'")
    .<EventA>handle((eventA, h) -> new EventB(
        eventA.getSomeField(),
        h.get("a_header", String.class),
        h.get("other_header", String.class)))
    .handle(Http.outboundGateway(uri).httpMethod(HttpMethod.POST))
    .get();
}
@Bean
公共集成流aFlow(){
返回IntegrationFlows.from(EventASink.INPUT)
.filter(“标题['operation']='operation_A'”)
.handle((eventA,h)->新EventB(
eventA.getSomeField(),
h、 get(“a_头”,String.class),
h、 get(“其他_头”,String.class)))
.handle(Http.outboundGateway(uri).httpMethod(httpMethod.POST))
.get();
}

有了这个:
.transform(Message.class,m->m.getHeaders())
可以将消息负载参数化吗?不,使用
Message.class
必须强制转换
getPayload()
。如果只处理
负载
,只需使用
.transform(foo->for.toBar())
。我需要访问有效负载和标头。我通过内联anonymous
GenericTransformer
类而不是Lamba实现了这一点:
.transform(Message.class,m->m.getHeaders())
可以将消息有效负载参数化吗?不,使用
Message.class
您必须强制转换
getPayload()
。如果只处理
有效负载
,只需使用
.transform(foo->for.toBar())
。我需要访问有效负载和标题。我通过内联匿名
GenericTransformer
类而不是Lamba来实现这一点。