es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

Bolsa de Investigación de Campos en Spring IntegrationFlow

¿Cómo podemos hacer que un BaggageField de Spring Cloud Sleuth funcione en Spring IntegrationFlow? Necesito ayuda para agregarlo en el IntegrationFlow DSL.

Lo agregué en un transform(), sin embargo, no se refleja.

spring.sleuth.baggage.local-fields: customField
spring.sleuth.baggage.correlation-fields: customField

@Bean
public BaggageField customField() {
    return BaggageField.create("customField");
}

@Bean
public ScopeDecorator mdcScopeDecorator() {
    return MDCScopeDecorator.newBuilder()
                            .clear()
                            .add(SingleCorrelationField.newBuilder(customField())
                                                        .flushOnUpdate()
                                                        .build())
                            .build();
}

return IntegrationFlows.from(jdbcMessageSource(), p -> p.poller(pollerSpec()))
                            .transform(message -> {
                                customField.updateValue("ARE");
                                return message;
                            }).split()
                            .channel(c -> c.executor(Executors.newCachedThreadPool()))
                            .transform(transformer, "transform")
                            .enrichHeaders(headerEnricherSpec -> headerEnricherSpec.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
                            .handle(Http.outboundGateway(url)
                                        .httpMethod(HttpMethod.POST)
                                        .expectedResponseType(String.class)
                                        .requestFactory(requestFactory))
                            .channel(httpOutputChannel())
                            .get();

}“`

Tags:  ,

Answer

  1. Avatar for davy.ai

    Para agregar un BaggageField de Spring Cloud Sleuth en Spring IntegrationFlow, puedes crear un Bean para el BaggageField y hacer referencia a él en un ScopeDecorator. En el ScopeDecorator, puedes agregar el BaggageField a un SingleCorrelationField y luego devolver el MDCScopeDecorator. Para usar el BaggageField personalizado en el DSL de IntegrationFlow, puedes agregarlo en un método transform() con una llamada a updateValue() para establecer un valor. Sin embargo, es importante recordar que los BaggageFields solo se propagan aguas abajo y no aguas arriba, por lo que es posible que no se refleje de inmediato. Aquí tienes un ejemplo de fragmento de código:

    @Bean
    public BaggageField customField() {
        return BaggageField.create("customField");
    }
    
    @Bean
    public ScopeDecorator mdcScopeDecorator() {
        return MDCScopeDecorator.newBuilder()
                                .clear()
                                .add(SingleCorrelationField.newBuilder(customField())
                                                            .flushOnUpdate()
                                                            .build())
                                .build();
    }
    
    return IntegrationFlows.from(jdbcMessageSource(), p -> p.poller(pollerSpec()))
                            .transform(message -> {
                                customField.updateValue("ARE");
                                return message;
                            }).split()
                            .channel(c -> c.executor(Executors.newCachedThreadPool()))
                            .transform(transformer, "transform")
                            .enrichHeaders(headerEnricherSpec -> headerEnricherSpec.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
                            .handle(Http.outboundGateway(url)
                                        .httpMethod(HttpMethod.POST)
                                        .expectedResponseType(String.class)
                                        .requestFactory(requestFactory))
                            .channel(httpOutputChannel())
                            .get();
    

Comments are closed.