camel - 如何通过producerTemplates

时间:2017-02-17 09:41:54

标签: java apache-camel

这篇文章与此get exchange from within pojo without changing it's interface

有关

此问题的解决方案是使用MDC。

这是执行此操作的代码:(取自camel测试代码)

给出了这个接口,例如

    public interface IEcho {
        String echo(String param);
    }

和配置如下:

    import org.apache.camel.CamelContext;
    import org.apache.camel.ProducerTemplate;
    import org.apache.camel.builder.RouteBuilder;
    import org.apache.camel.impl.DefaultCamelContext;
    import org.junit.Test;
    import org.slf4j.MDC;

    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;

    import static org.junit.Assert.assertEquals;

    public class RetrieveCorrelationIdTest {

        @Test
        public void testCamel() throws Exception {
            DefaultCamelContext ctx = new DefaultCamelContext();
            try {
                ctx.setUseMDCLogging(true);
                ctx.addRoutes(createRouteBuilder());
                ctx.start();
                Object body = ctx.createProducerTemplate().requestBody("direct:a", "text in body");
                assertEquals("TEXT IN BODY", body);
            } finally {
                ctx.stop();
            }
        }

        protected RouteBuilder createRouteBuilder() throws Exception {
            return new RouteBuilder() {
                @Override
                public void configure() throws Exception {
                    IEcho proxy = (IEcho) ReflectHelper.simpleProxy(IEcho.class, new Proxybean(getContext()));

                    from("direct:a").routeId("route-a")
                            .setHeader("CUSTOM-CORRELATION-ID", constant("correlationIdsetInHeader"))
                            .process(exchange -> MDC.put("CUSTOM-HEADER-MDC", "correlationIdsetWithMDC"))
                            .bean(proxy, "echo(${body})");

                    from("direct:b").routeId("route-b")
                            .process(exchange -> {
                                String customHeader = (String) exchange.getIn().getHeader("CUSTOM-CORRELATION-ID");
                                String mdcHeader = MDC.get("CUSTOM-HEADER-MDC");
                                assertEquals(customHeader, mdcHeader);
                                exchange.getIn().setBody(((String)exchange.getIn().getBody()).toUpperCase());
                            })
                            .to("mock:result");
                }
            };
        }

        class Proxybean implements InvocationHandler {
            CamelContext ctx;
            Proxybean(CamelContext ctx) {
                this.ctx = ctx;
            }
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if (method.getName().equals("echo")) {
                    // how to get the CUSTOM-CORRELATION-ID here ????µ
                    // only possible with MDC ?
                    String mdcHeader = MDC.get("CUSTOME-HEADER-MDC");
                    String result =
                            (String) ctx.createProducerTemplate()
                                    .requestBodyAndHeader("direct:b", args[0], "CUSTOME-HEADER", mdcHeader);
                    return result;
                }
                return null;
            }
        }
    }

参见代码中的评论:

// how to get the CUSTOM-CORRELATION-ID here ????µ
// only possible with MDC ?

即使没有使用MDC进行交换,我也可以访问一些数据

所以我的问题:

如果我想从一个端点调用beans方法获取一个correlationId,该方法调用通过使用producerTemplate进行调用的代理等公开的其他方法等等......

  • 如果没有MDC可以吗,驼峰会提供另一种方法来检索这些信息吗?

0 个答案:

没有答案