BasicDataSource到DataSource

时间:2015-12-17 14:13:42

标签: scala jdbc apache-camel datasource

我正在尝试使用camels sql:并将自己搜索到这个:

import org.apache.commons.dbcp2.BasicDataSource
import org.apache.camel.impl.{SimpleRegistry, DefaultCamelContext}

object CamelApplication {
  val jdbcUrl = "jdbc:mysql://host:3306"
  val user = "test"
  val password = "secret"
  val driverClass = "com.mysql.jdbc.Driver"

  // code to create data source here
  val ds = new BasicDataSource

  ds.setUrl(jdbcUrl)
  ds.setUsername(user)
  ds.setPassword(password)
  ds.setDriverClassName(driverClass)

  val registry = new SimpleRegistry
  registry.put("dataSource", ds)

  def main(args: Array[String]) = {
    val context = new DefaultCamelContext(registry)

    context.setUseMDCLogging(true)
    context.addRoutes(new DlrToDb)
    context.start()
    Thread.currentThread.join()
  }
}

和我的DlrToDb路线是这样的:

import org.apache.camel.scala.dsl.builder.RouteBuilder

class DlrToDb extends RouteBuilder{

  """netty:tcp://localhost:12000?textline=true""" ==> {
    id("DlrToDb")
    log("sql insert coming up")
    to("sql:insert into camel_test (msgid, dlr_body) VALUES ('some_id','test')")
  }
}

即。当我telnet到localhost并按回车时我想在我的数据库中添加一些数据。但它是BasicDataSource而不是DataSource,因此我收到错误:

Failed to create route DlrToDb .....
....  due to: Property 'dataSource' is required 

我是否需要从BasicDatasource进行更改/转换,或者我是否需要对注册表执行某些操作才能使其正常工作?

1 个答案:

答案 0 :(得分:1)

您需要附加查询选项" dataSource"到URI:

....
to("sql:insert into camel_test (msgid, dlr_body) VALUES ('some_id','test')?dataSource=dataSource")
....