我正在尝试第一次使用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模板= camelContext.createProducerTemplate();
  template.sendBody(“ direct:start”,“ message”);

09-18 01:53