2015-06-24 28 views
2

我試圖首次使用駝峯緩存。所以我創建了一個基於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"); 
    } 
} 

回答

2

您的路線可能運行的,你只是還沒有被調用它們,但(從你上面貼的反正代碼)。你需要使用ProducerTemplate行使路由發送消息到direct:startdirect:next路線...

ProducerTemplate模板= camelContext.createProducerTemplate();
template.sendBody(「direct:start」,「message」);