2016-12-01 84 views
0

我們基於存儲在數據庫中的配置參數動態構建駱駝路由。我們有建立所有駱駝路線的泛型類。一些參數僅作爲原始Camel Spring XML提供。該構建駱駝路由類擴展和RouteBuilder這裏是代碼構建駱駝路線的一部分:Apache Camel java.lang.ClassCastException當從Spring XML配置組件動態構建路由時

@Override 
    public void configure() throws Exception { 
     RouteDefinition route = from(inputFile); 
     configureSpringXMLActivity(0, route, convertBodyXml); 
     configureSpringXMLActivity(5, route, setHeaderXml); 
    } 

    void configureSpringXMLActivity(final Integer seq, final RouteDefinition route, final String xmlConfig) 
      throws Exception { 
     ActivityIdentifier identifier = new CamelActivityIdentifier(seq); 
     route.process(new ActivityHandoverProcessor(identifier)); 
     final ChoiceDefinition choice = route.choice().when(new ActivityPredicate(identifier)); 

     RouteContext routeContext = new DefaultRouteContext(camelContext, route, route.getInputs().get(0), 
       camelContext.getRoutes()); 

     final StringReader reader = new StringReader(xmlConfig); 
     Object result = esb.getConfigUnmarshaller().unmarshal(reader); 
     if (result != null) { 
      ProcessorDefinition<?> processorDefinition = (ProcessorDefinition<?>) result; 
      Processor processor = processorDefinition.createProcessor(routeContext); 
      choice.process(processor); 
     } 

其中

/** setHeader xml. */ 
    private final String setHeaderXml = "<setHeader headerName=\"extractFileName\" xmlns=\"http://camel.apache.org/schema/spring\"><simple>${body}</simple></setHeader>"; 

    /** convertBody xml. */ 
    private final String convertBodyXml = "<convertBodyTo xmlns=\"http://camel.apache.org/schema/spring\" type=\"java.lang.String\"/>"; 

當我們開始駱駝上下文它產生於創建處理器的setHeader例外。

java.lang.ClassCastException: org.apache.camel.model.ProcessDefinition cannot be cast to org.apache.camel.model.SetHeaderDefinition 
    at org.apache.camel.management.DefaultManagementObjectStrategy.getManagedObjectForProcessor(DefaultManagementObjectStrategy.java:355) 
    at org.apache.camel.management.DefaultManagementLifecycleStrategy.getManagedObjectForProcessor(DefaultManagementLifecycleStrategy.java:515) 
    at org.apache.camel.management.DefaultManagementLifecycleStrategy.getManagedObjectForService(DefaultManagementLifecycleStrategy.java:467) 
    at org.apache.camel.management.DefaultManagementLifecycleStrategy.onServiceAdd(DefaultManagementLifecycleStrategy.java:378) 
    at org.apache.camel.impl.RouteService.startChildService(RouteService.java:338) 
    at org.apache.camel.impl.RouteService.warmUp(RouteService.java:182) 
    at org.apache.camel.impl.DefaultCamelContext.doWarmUpRoutes(DefaultCamelContext.java:3496) 
    at org.apache.camel.impl.DefaultCamelContext.safelyStartRouteServices(DefaultCamelContext.java:3426) 
    at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRoutes(DefaultCamelContext.java:3203) 
    at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3059) 
    at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:175) 
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2854) 
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2850) 
    at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:2873) 
    at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:2850) 
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) 
    at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:2819) 

在駱駝(版本2.17.0)的源代碼 - 類DefaultManagementObjectStrategy:

.......... 
    } else if(target1 instanceof SetHeaderProcessor) { 
        answer = new ManagedSetHeader(context, (SetHeaderProcessor)target1, (SetHeaderDefinition)definition); 
......... 

鑄造失敗時(SetHeaderDefinition)定義

但是,我的第一個活動沒有任何問題 - convertBodyXml。在同一駱駝類:

if(target1 instanceof ConvertBodyProcessor) { 
        answer = new ManagedConvertBody(context, (ConvertBodyProcessor)target1, definition); 

在這種情況下,駱駝代碼並不需要轉換定義來創建管理對象:......,定義)

DefaultManagementObjectStrategy該類創建一些管理鑄造時的具體定義客體,而不是其他人。

請問您能否推薦如何解決ClassCastException,但仍然從通用ProcessorDefinition對象中構建路由。

在此先感謝。

+0

似乎是你正在嘗試做一個非常複雜的方法。 –

回答

0

而是從ProcessorDefinitionChoiceDefinition構建Processor和呼叫處理方法,有可能只是ChoiceDefinition直接調用addOutputProcessorDefinition參數。

基本上,choice.addOutput(processorDefinition);

這裏被更新的代碼片段:

@Override 
    public void configure() throws Exception { 
     RouteDefinition route = from(inputFile); 
     configureSpringXMLActivity(0, route, convertBodyXml); 
     configureSpringXMLActivity(5, route, setHeaderXml); 
    } 

    void configureSpringXMLActivity(final Integer seq, final RouteDefinition route, final String xmlConfig) 
      throws Exception { 
     ActivityIdentifier identifier = new CamelActivityIdentifier(seq); 
     route.process(new ActivityHandoverProcessor(identifier)); 
     final ChoiceDefinition choice = route.choice().when(new ActivityPredicate(identifier)); 


     final StringReader reader = new StringReader(xmlConfig); 
     Object result = esb.getConfigUnmarshaller().unmarshal(reader); 
     if (result != null) { 
      ProcessorDefinition<?> processorDefinition = (ProcessorDefinition<?>) result; 
      choice.addOutput(result); 
     } 

我希望這會幫助別人