基本数据源到数据源

BasicDataSource to DataSource

我正在尝试使用骆驼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')")
  }
}

即当我远程登录到本地主机并按回车键时,我想在我的数据库中添加一些数据。但是它是 BasicDataSource 而不是 DataSource 所以我得到一个错误:

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

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

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

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