使用 Camel 在 table 上收听插入

Listen insertions on a table with Camel

我在 Java 中有一个带有 Spring boot 2 的应用程序,我想收听 table 上的插入,这是由触发器制作的,是强制性的我用骆驼,因为它很容易做,但我没有找到答案。

我找到了这个,但链接已损坏。

更新和答案

我找到了一个方法,使用 SQL 组件: (Groovy 代码)

@Component
class PagosRouter extends RouteBuilder {

  @Autowired
  private OracleDataSource mydataSource

  @Override
  void configure() {

    String rowId = 'row-ID'

    from('sql:select * from THE_TABLE where IS_NEW = 1?dataSource=mydataSource')
        // Catch every sigle row  
        .process { exchange ->
          // Set in the header the md5 row
          exchange.in.setHeader(rowId, exchange.in.body.toString().md5())
        }
        // Set a component that filter every rowId, discarting the repeated ones
        .idempotentConsumer( header(rowId), new JdbcMessageIdRepository(mydataSource, 'theTableWatcher') )
        .split(body()).parallelProcessing(true)
        // Process the new rows 
        .process( { exchange ->
          // Do somthing here
        })
  }
}

如果 table B 是由 table A 上的触发器创建的,您可以使用那里的触发器从 table B 上的插入构建 table C。您必须使用 select 轮询 table C 并删除已处理的行。 Camel好像可以监控table C.

您不能在 table 上收听。但是你可以有一个定期轮询的骆驼组件(例如每 5 秒执行一次查询)。

例如,如果您有一个增量整数作为主键,并且查询将有一个 id 大于上次读取的插入的 where 条件。

或者可能取决于记录的创建时间

https://camel.apache.org/components/latest/sql-component.html