[英]Select data from table using apache camel
I want to be able to continuously poll database to select data from my table using Camel.我希望能够使用 Camel 连续轮询数据库以从我的表中选择数据。 I have configured Camel in my spring boot application.
我在 Spring Boot 应用程序中配置了 Camel。 Here are the configurations that I am using
这是我正在使用的配置
build.gradle:构建.gradle:
implementation 'org.apache.camel:camel-jdbc-starter:2.24.0'
implementation 'org.apache.camel:camel-sql-starter:2.24.0'
RouteBuilder class: RouteBuilder 类:
@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.我面临的问题是,当我打印 bean 方法参数(generateSalesData(String payload))时,我得到的是查询字符串本身(“select * fromTENTS”),而不是表中的值。 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)". configure 方法中的 setBody() 不接受
sql:select .. statement
,显示“ProcessorDefinition 类型中的方法 setBody(Expression) 不适用于参数 (String)”。
I am new to camel.我是骆驼的新手。 Could anyone please let me know what is that I am missing to do.
任何人都可以让我知道我缺少什么。
The route you have, as written, is simply setting the body of the message to a string which happens to look like a SQL.正如所写的那样,您所拥有的路线只是将消息正文设置为一个字符串,该字符串恰好看起来像一条 SQL。 Camel has no idea, since you haven't use the right component.
Camel 不知道,因为您没有使用正确的组件。
Instead of代替
.setBody(constant("select * from tenders"))
you need to tell Camel to use the sql
component你需要告诉 Camel 使用
sql
组件
.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 .传递给
RouteDataMapper
的结果将是List<Map<String, Object>>
,如文档中所述。 You'd need to adjust your method parameter accordingly.您需要相应地调整您的方法参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.