복붙노트

[SPRING] Java 런타임에서 낙타 경로 추가

SPRING

Java 런타임에서 낙타 경로 추가

Java 런타임에서 낙타 경로를 추가하려면 어떻게해야합니까? Grails 예제를 발견했지만 Java로 구현했습니다.

내 applicationContext.xml에는 이미 미리 정의 된 정적 경로가 있으며 런타임에 일부 동적 경로를 추가하려고합니다. 가능한가? 동적 경로를 포함하는 유일한 방법은 route.xml을 작성한 다음 라우트 정의를 컨텍스트에로드하는 것입니다. 기존 정적 경로에서 어떻게 작동합니까? 런타임시 라우팅

해결법

  1. ==============================

    1.CamelContext에서 몇 가지 API를 호출하여 경로를 추가 할 수 있습니다.

    CamelContext에서 몇 가지 API를 호출하여 경로를 추가 할 수 있습니다.

    context.addRoutes(new MyDynamcRouteBuilder(context, "direct:foo", "mock:foo"));
    ....
    private static final class MyDynamcRouteBuilder extends RouteBuilder {
        private final String from;
        private final String to;
    
        private MyDynamcRouteBuilder(CamelContext context, String from, String to) {
            super(context);
            this.from = from;
            this.to = to;
        }
    
        @Override
        public void configure() throws Exception {
            from(from).to(to);
        }
    }
    

    전체 예제를 보려면이 단위 테스트를 참조하십시오 ...

    https://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/AddRoutesAtRuntimeTest.java

  2. ==============================

    2.@Himanshu, 특정 조건에 따라 다른 '목적지'로 동적 경로를 지정하는 데 도움이되는 다이내믹 옵션 (즉, 라우팅 슬립)을 살펴보십시오.

    @Himanshu, 특정 조건에 따라 다른 '목적지'로 동적 경로를 지정하는 데 도움이되는 다이내믹 옵션 (즉, 라우팅 슬립)을 살펴보십시오.

    낙타 사이트의 동적 라우터 도움말 링크를 확인하십시오.

    http://camel.apache.org/dynamic-router.html

    from("direct:start")
        // use a bean as the dynamic router
        .dynamicRouter(method(DynamicRouterTest.class, "slip"));
    

    그리고 슬립 방법;

    /**
     * Use this method to compute dynamic where we should route next.
     *
     * @param body the message body
     * @return endpoints to go, or <tt>null</tt> to indicate the end
     */
    public String slip(String body) {
        bodies.add(body);
        invoked++;
    
        if (invoked == 1) {
            return "mock:a";
        } else if (invoked == 2) {
            return "mock:b,mock:c";
        } else if (invoked == 3) {
            return "direct:foo";
        } else if (invoked == 4) {
            return "mock:result";
        }
    
        // no more so return null
        return null;
    }
    

    희망이 도움이 ...

    감사.

  3. from https://stackoverflow.com/questions/10451444/add-camel-route-at-runtime-in-java by cc-by-sa and MIT license