使用Camel Cache时,Camel Routes已启动但未运行

时间:2015-06-24 10:28:40

标签: java maven caching apache-camel

我第一次尝试使用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");
    }
}

1 个答案:

答案 0 :(得分:2)

你的路线可能正在运行,你还没有调用它们(从你上面发布的代码来看)。您需要使用ProducerTemplate向public class FileBytesAbstraction : TagLib.File.IFileAbstraction { public FileBytesAbstraction(string name, byte[] bytes) { Name = name; var stream = new MemoryStream(bytes); ReadStream = stream; WriteStream = stream; } public void CloseStream(Stream stream) { stream.Dispose(); } public string Name { get; private set; } public Stream ReadStream { get; private set; } public Stream WriteStream { get; private set; } } direct:start路由发送消息来行使路线......

  

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