scalajdbcapache-cameldatasource

BasicDataSource to DataSource


I am trying to use camels sql: and have googled myself into this:

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()
  }
}

and my DlrToDb route is this:

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')")
  }
}

i.e. when I telnet to localhost and press enter I would like some data to be added in my database. However it is a BasicDataSource and not a DataSource so I get an error:

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

Do I need to change/convert from the BasicDatasource, or do I need to do something to the registry to make it work?


Solution

  • You need to append query option "dataSource" to the URI:

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