Select data from table using apache camel

continuousLearner :

I want to be able to continuously poll database to select data from my table using Camel. I have configured Camel in my spring boot application. Here are the configurations that I am using

build.gradle:

implementation 'org.apache.camel:camel-jdbc-starter:2.24.0'
implementation 'org.apache.camel:camel-sql-starter:2.24.0'

RouteBuilder class:

@Component
public class CustomCamelConfig extends RouteBuilder {

    Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    RouteDataMapper dataMapper;

    @Override
    public void configure() throws Exception {

         from("timer://timer1?period=2s").log("Called every 2 seconds")
         .setBody(constant("select * from tenders"))
         .bean(dataMapper,"generateSalesData")
         .noDelayer();
    }

}

Bean:

@Component
public class RouteDataMapper {

    Logger log = LoggerFactory.getLogger(getClass());

    public void generateSalesData(String payload) {

        log.info("RouteDataMapper - [generateSalesData]");
        log.info("payload : {}", payload);
    }

}

application.properties

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@xxx:xxx/zzz
spring.datasource.username=zzz
spring.datasource.password=zzz

Issue I am facing is that, when I print the bean method parameter (generateSalesData(String payload)), I get the query string itself ("select * from tenders") and not the value from the table. The setBody() in configure method doesn't accept a sql:select .. statement, shows "The method setBody(Expression) in the type ProcessorDefinition is not applicable for the arguments (String)".

I am new to camel. Could anyone please let me know what is that I am missing to do.

Adam Hawkes :

The route you have, as written, is simply setting the body of the message to a string which happens to look like a SQL. Camel has no idea, since you haven't use the right component.

Instead of

.setBody(constant("select * from tenders"))

you need to tell Camel to use the sql component

.to("sql:select * from tenders")

The result that's passed on to RouteDataMapper will be a List<Map<String, Object>>, as described in the documentation. You'd need to adjust your method parameter accordingly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=237410&siteId=1