使用 Camel 缓存时,Camel 路由已启动但未 运行

Camel Routes started but not run when using Camel Cache

我是第一次尝试使用 Camel Cache。所以我创建了一个基于 camel-java maven 原型的小应用程序。
我的代码基于 here 中的示例。这是片段

public class AddingToCache extends RouteBuilder {
   public void configure() {
            from("direct:start")
            .log("START")
            .setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_ADD))
            .setHeader(CacheConstants.CACHE_KEY, constant("Custom_key"))
            .process(new Processor() {
                @Override
                public void process(Exchange exchange) throws Exception {
                    exchange.getOut().setBody("My custom out");
                }
            })
            .log("starting ...")
            .to("cache://cache1")
            .to("direct:next");
    }
}


public class ReadingFromCache extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("direct:next")
            .setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_GET))
            .setHeader(CacheConstants.CACHE_KEY, constant("Custom_key"))
            .to("cache://cache1")
            .choice()
           .when(header(CacheConstants.CACHE_ELEMENT_WAS_FOUND).isNotNull())
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        Object body = exchange.getIn().getBody();
                        System.out.println("Cache body - " + body);
                    }
                })
            .otherwise()
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        Object body = exchange.getIn().getBody();
                        System.out.println("Cache body when not found - " + body);
                    }
                })
            .end()
            .to("direct:finish");
    }
}

您的路线可能 运行,您只是还没有调用它们(无论如何从您上面发布的代码)。您需要使用 ProducerTemplate 向 direct:startdirect:next 路由发送消息以执行路由...

ProducerTemplate template = camelContext.createProducerTemplate();
template.sendBody("direct:start", "message");