访问骆驼正文内容以动态加载SQL语句-org.apache.camel.RuntimeCamelException

时间:2018-10-24 07:37:56

标签: apache-camel camel-sql

camel-sql 2.22.1

在我的路线上,我有:

from("direct:targetDatabase").startupOrder(1)
        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                System.out.println("BODY:"+exchange.getIn().getBody());
                System.out.println(
                     exchange.getIn().
                       getBody(MessageContentsList.class).get(1));
           }
        })

在控制台上,我看到:

BODY:[[{"id":2314,"code":"3498HK","value":30.88}], testInsert1]
testInsert1

我想使用此值来选择要使用SQLComponent执行的sql语句。

我有sql.properties文件:

sql.insertdata=insert into gsdata values(:#id, :#code, :#value)
sql.testInsert1=insert into gsdata2 values(:#id, :#code, :#value)

在我的路线上,我知道这可行:

.to("sqlComponent:{{sql.insertdata}}")

但是我想这样做(或以某种方式实现):

.toD("sqlComponent:{{sql.${bodyAs(java.util.List).get(1)}}}")

当部署到Tomcat时,这给了我一个骆驼的运行时异常。

24-Oct-2018 19:52:36.433 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
 org.apache.camel.RuntimeCamelException: org.apache.camel.FailedToCreateRouteException: Failed to create route route4 at: >>> Split[simple{${body}} -> [process[Processor@0x7784550a], DynamicTo[]]] <<< in route: Route(route4)[[From[direct:targetDatabase]] -> [process[Proc... because of Property with key [sql.${bodyAs(java.util.List).get(1)] not found in properties from text: sqlComponent:{{sql.${bodyAs(java.util.List).get(1)}}}
    at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1830)
    at org.apache.camel.spring.SpringCamelContext.start(SpringCamelContext.java:136)
    at org.apache.camel.spring.CamelContextFactoryBean.start(CamelContextFactoryBean.java:369)
    at org.apache.camel.spring.CamelContextFactoryBean.onApplicationEvent(CamelContextFactoryBean.java:416)
    at org.apache.camel.spring.CamelContextFactoryBean.onApplicationEvent(CamelContextFactoryBean.java:94)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:400)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:354)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:888)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:409)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4792)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5256)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:985)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.camel.FailedToCreateRouteException: Failed to create route route4 at: >>> Split[simple{${body}} -> [process[Processor@0x7784550a], DynamicTo[]]] <<< in route: Route(route4)[[From[direct:targetDatabase]] -> [process[Proc... because of Property with key [sql.${bodyAs(java.util.List).get(1)] not found in properties from text: sqlComponent:{{sql.${bodyAs(java.util.List).get(1)}}}
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1346)
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:206)
    at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:1145)
    at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:3731)
    at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3445)
    at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:209)
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3253)
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3249)
    at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:3272)
    at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:3249)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
    at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:3165)
    at org.apache.camel.spring.SpringCamelContext.start(SpringCamelContext.java:133)
    ... 26 more
Caused by: java.lang.IllegalArgumentException: Property with key [sql.${bodyAs(java.util.List).get(1)] not found in properties from text: sqlComponent:{{sql.${bodyAs(java.util.List).get(1)}}}
    at org.apache.camel.component.properties.DefaultPropertiesParser$ParsingContext.getPropertyValue(DefaultPropertiesParser.java:270)
    at org.apache.camel.component.properties.DefaultPropertiesParser$ParsingContext.readProperty(DefaultPropertiesParser.java:156)
    at org.apache.camel.component.properties.DefaultPropertiesParser$ParsingContext.doParse(DefaultPropertiesParser.java:115)
    at org.apache.camel.component.properties.DefaultPropertiesParser$ParsingContext.parse(DefaultPropertiesParser.java:99)
    at org.apache.camel.component.properties.DefaultPropertiesParser.parseUri(DefaultPropertiesParser.java:62)
    at org.apache.camel.component.properties.PropertiesComponent.parseUri(PropertiesComponent.java:235)
    at org.apache.camel.component.properties.PropertiesComponent.parseUri(PropertiesComponent.java:178)
    at org.apache.camel.impl.DefaultCamelContext.resolvePropertyPlaceholders(DefaultCamelContext.java:2552)
    at org.apache.camel.model.ProcessorDefinitionHelper.resolvePropertyPlaceholders(ProcessorDefinitionHelper.java:735)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessorImpl(ProcessorDefinition.java:455)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:441)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:185)
    at org.apache.camel.model.ProcessorDefinition.createChildProcessor(ProcessorDefinition.java:204)
    at org.apache.camel.model.SplitDefinition.createProcessor(SplitDefinition.java:101)
    at org.apache.camel.model.ProcessorDefinition.makeProcessorImpl(ProcessorDefinition.java:562)
    at org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:523)
    at org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:239)
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1343)
    ... 38 more

我尝试过此语句的许多变体,但均未成功。 有三个问题:

  1. 如何动态设置sqlcomponent将用于获取适当的sql的值?
  2. 如何正确从正文中检索值?
  3. 有更好的方法吗?

非常感谢,这是有史以来最好的网站!

2 个答案:

答案 0 :(得分:0)

您可以尝试:

toD(simple("sqlComponent:{{sql.${bodyAs(java.util.List).get(1)}}}"))

来自documentation

我会在动态路由之前将所需的值存储在exchange属性中。

答案 1 :(得分:0)

得到一个有效的答案:

.setProperty("sqlQuery", simple("${properties:sql.${bodyAs(java.util.List).get(1)}}"))
.log(LoggingLevel.INFO,"SQLQuery: ${exchangeProperty[sqlQuery]}")
.unmarshal(csv)
.split(body())
.toD("sql:${property.sqlQuery}?dataSource=testDataSource&batch=true")
.end()

我现在可以有一个专门针对各种期望的CSV格式文件的SQL插入查询。无需重新编译即可支持其他CSV。

props文件中的SQL示例(用于PostreSQL)

10=insert into source_upload_data(st_id,st_code,st_avg_cost,uploadid)values(:#id::integer,:#name,:#amount::numeric,10);

示例CSV:

id,name,amount
1,Box,230.21
2,Bucket,7865.43
3,Tyre,89