50.10. Samples


在以下示例中,我们将设置 camel-jdbc 所需的 DataSource。首先,我们将 Camel 注册表中的数据源注册为 testdb

EmbeddedDatabase db = new EmbeddedDatabaseBuilder()
      .setType(EmbeddedDatabaseType.DERBY).addScript("sql/init.sql").build();

CamelContext context = ...
context.getRegistry().bind("testdb", db);

然后,我们将配置路由到 JDBC 组件的路由,以便执行 SQL。请注意,我们如何引用上一步中绑定的 testdb 数据源:

from("direct:hello")
    .to("jdbc:testdb");

我们创建一个端点,将 SQL 查询添加到 IN 消息的正文,然后发送交换。查询的结果会在 OUT 正文中返回:

Endpoint endpoint = context.getEndpoint("direct:hello");
Exchange exchange = endpoint.createExchange();
// then we set the SQL on the in body
exchange.getMessage().setBody("select * from customer order by ID");
// now we send the exchange to the endpoint, and receives the response from Camel
Exchange out = template.send(endpoint, exchange);

如果您想一次在一行中工作,而不是整个 ResultSet,您需要使用 Splitter EIP,例如:

from("direct:hello")
// here we split the data from the testdb into new messages one by one
// so the mock endpoint will receive a message per row in the table
// the StreamList option allows to stream the result of the query without creating a List of rows
// and notice we also enable streaming mode on the splitter
.to("jdbc:testdb?outputType=StreamList")
  .split(body()).streaming()
  .to("mock:result");
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.