How can I log database errors at ERROR level when using Camel and MyBatis in my Spring Boot app?

huangapple go评论60阅读模式
英文:

How can I log database errors at ERROR level when using Camel and MyBatis in my Spring Boot app?

问题

我有一个基于Java、Spring Boot、Apache Camel、MyBatis和Oracle构建的应用程序。它使用了Java DSL方法进行配置。

许多路由用于从数据库轮询数据:

@Component
public class DataChangesRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        from("mybatis:selectDataChanges?statementType=SelectList&initialDelay=1300&delay=5000")
                .autoStartup("true")
                .routeId("dataChanges")
                .choice().when(body().isNotNull())
                .log("Consuming data change: ${body}")
                .to("direct:sendDataChangeMessage");

        ...

如果在轮询过程中发生数据库错误,即在“from”子句中,我想将这些错误记录为ERROR级别的日志。

但是似乎默认情况下它们会以WARN级别记录。

为了重新创建错误情况,我已经撤销了涉及数据库查询的表之一上的授权。结果记录如下:

[2023-05-24T15:32:40.040Z] [org.apache.camel.spi.CamelLogger] [Camel (camel-1) thread #7 - mybatis://selectDataChanges] [214] [WARN ] Consumer Consumer[mybatis://selectDataChanges?delay=5000&initialDelay=1300&statementType=SelectList] 
failed polling endpoint: mybatis://selectDataChanges?delay=5000&initialDelay=1300&statementType=SelectList. 
Caused by: [org.apache.ibatis.exceptions.PersistenceException - 
### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: ORA-01031: insufficient privileges

阅读了文档后,我尝试为路由添加了一个errorHandler。我还尝试为路由添加了一个onException(Exception.class)处理程序。

但是这两者都没有任何影响。似乎异常不会传递给它们中的任何一个。

尝试使用try() ... catch()方法似乎不合适,因为我所看到的所有示例都是在路由内的步骤周围使用它,而不是在“from”步骤周围使用它。

关于如何捕获这些错误并记录它们为ERROR级别的建议将不胜感激。

英文:

I have an app built on Java, Spring Boot, Apache Camel, MyBatis, Oracle. It used the Java DSL approach to configuration.

Many of the routes poll the database for data:

@Component
public class DataChangesRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        from("mybatis:selectDataChanges?statementType=SelectList&initialDelay=1300&delay=5000")
                .autoStartup("true")
                .routeId("dataChanges")
                .choice().when(body().isNotNull())
                .log("Consuming data change: ${body}")
                .to("direct:sendDataChangeMessage");

        ...

If a database error occurs during the polling, i.e. in the "from" clause, I would like to log these at ERROR level.

However it seems that the default is that they get logged at WARN level.

To re-create an error situation, I have revoked a grant on one of the tables involved in the database query. This results in the following being logged.

[2023-05-24T15:32:40.040Z] [org.apache.camel.spi.CamelLogger] [Camel (camel-1) thread #7 - mybatis://selectDataChanges] [214] [WARN ] Consumer Consumer[mybatis://selectDataChanges?delay=5000&initialDelay=1300&statementType=SelectList] 
failed polling endpoint: mybatis://selectDataChanges?delay=5000&initialDelay=1300&statementType=SelectList. 
Will try again at next poll. 
Caused by: [org.apache.ibatis.exceptions.PersistenceException - 
### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: ORA-01031: insufficient privileges

Having read the docs, I have tried to adding an errorHandler to the Route. I have also tried adding an onException(Exception.class) handler to the route.

Neither make any difference. It is as though the Exception doesn't get passed to either of those.

The try() ... catch() approach doesn't seem appropriate as all the examples I have seen show that being used around the steps within the route, but not around the from step itself.

Any suggestions about how to catch these errors and log them at ERROR level would be appreciated.

答案1

得分: 1

我在这里找到了对我的问题的答案:

https://camel.apache.org/components/3.20.x/mybatis-component.html

这里有一个组件配置选项 bridgeErrorHandler,它的作用是:

> 允许将消费者与Camel路由错误处理程序桥接,
> 这意味着在消费者尝试接收传入消息或类似操作时发生的任何异常现在都将被处理为消息并由路由错误处理程序处理。默认情况下,消费者将使用org.apache.camel.spi.ExceptionHandler来处理异常,这将记录为WARN或ERROR级别并被忽略。

默认情况下,它设置为 false,所以要将其设置为 true,我必须在我的application.properties中添加这一行:

camel.component.mybatis.bridge-error-handler=true
英文:

I found the answer to my own question here:

https://camel.apache.org/components/3.20.x/mybatis-component.html

There is a component configuration option bridgeErrorHandler that:

> Allows for bridging the consumer to the Camel routing Error Handler,
> which mean any exceptions occurred while the consumer is trying to
> pickup incoming messages, or the likes, will now be processed as a
> message and handled by the routing Error Handler. By default the
> consumer will use the org.apache.camel.spi.ExceptionHandler to deal
> with exceptions, that will be logged at WARN or ERROR level and
> ignored.

By default, that is set to false, so to set the to true I had to add this line to my application.properties:

camel.component.mybatis.bridge-error-handler=true

huangapple
  • 本文由 发表于 2023年5月24日 23:22:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定