Exception Mule-Catch异常策略是&x27;正在为指定的异常调用t

Exception Mule-Catch异常策略是&x27;正在为指定的异常调用t,exception,error-handling,mule,esb,Exception,Error Handling,Mule,Esb,我正在尝试为我的Mule应用程序实现一种错误处理策略,该应用程序正在向数据库写入数据。下面是一个异常示例,在尝试插入具有重复标识符的记录时引发该异常: ERROR 2014-03-17 16:02:15,990 [[processes].inputConnector.receiver.01] org.mule.exception.DefaultMessagingExceptionStrategy: ************************************************

我正在尝试为我的Mule应用程序实现一种错误处理策略,该应用程序正在向数据库写入数据。下面是一个异常示例,在尝试插入具有重复标识符的记录时引发该异常:

ERROR 2014-03-17 16:02:15,990 [[processes].inputConnector.receiver.01] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Failed to route event via endpoint: DefaultOutboundEndpoint{endpointUri=jdbc://insertRecord, connector=JdbcConnector
{
  name=dbQueries
  lifecycle=start
  this=4c6f44a0
  numberOfConcurrentTransactedReceivers=4
  createMultipleTransactedReceivers=false
  connected=true
  supportedProtocols=[jdbc]
  serviceOverrides=<none>
}
,  name='endpoint.jdbc.insertRecord', mep=ONE_WAY, properties={queryTimeout=-1}, transactionConfig=Transaction{factory=null, action=INDIFFERENT, timeout=0}, deleteUnacceptedMessages=false, initialState=started, responseTimeout=10000, endpointEncoding=UTF-8, disableTransportTransformer=false}. Message payload is of type: HashMap
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL140317160209540' defined on 'TESTTABLE'. (org.apache.derby.iapi.error.StandardException)
  org.apache.derby.iapi.error.StandardException:-1 (null)
2. The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL140317160209540' defined on 'TESTTABLE'.(SQL Code: 30000, SQL State: + 23505) (org.apache.derby.impl.jdbc.EmbedSQLException)
  org.apache.derby.impl.jdbc.SQLExceptionFactory:-1 (null)
3. The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL140317160209540' defined on 'TESTTABLE'.(SQL Code: 30000, SQL State: + 23505) (java.sql.SQLIntegrityConstraintViolationException)
  org.apache.derby.impl.jdbc.SQLExceptionFactory40:-1 (null)
4. The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL140317160209540' defined on 'TESTTABLE'. Query: INSERT INTO TestTable (Id, Quantity) VALUES (?, ?) Parameters: [261000153085, 7](SQL Code: 30000, SQL State: + 23505) (java.sql.SQLException)
  org.apache.commons.dbutils.QueryRunner:540 (null)
5. Failed to route event via endpoint: DefaultOutboundEndpoint{endpointUri=jdbc://insertRecord, connector=JdbcConnector
{
  name=dbQueries
  lifecycle=start
  this=4c6f44a0
  numberOfConcurrentTransactedReceivers=4
  createMultipleTransactedReceivers=false
  connected=true
  supportedProtocols=[jdbc]
  serviceOverrides=<none>
}
,  name='endpoint.jdbc.insertRecord', mep=ONE_WAY, properties={queryTimeout=-1}, transactionConfig=Transaction{factory=null, action=INDIFFERENT, timeout=0}, deleteUnacceptedMessages=false, initialState=started, responseTimeout=10000, endpointEncoding=UTF-8, disableTransportTransformer=false}. Message payload is of type: HashMap (org.mule.api.transport.DispatchException)
  org.mule.transport.AbstractMessageDispatcher:109 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transport/DispatchException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
ERROR 23505: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL140317160209540' defined on 'TESTTABLE'.
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.impl.sql.execute.IndexChanger.insertAndCheckDups(Unknown Source)
    at org.apache.derby.impl.sql.execute.IndexChanger.doInsert(Unknown Source)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
ERROR 2014-03-17 16:02:15990[[processs].inputConnector.receiver.01]org.mule.exception.defaultMessaginException策略:
********************************************************************************
消息:未能通过端点路由事件:DefaultOutboundEndpoint{endpointUri=jdbc://insertRecord,connector=jdbc连接器
{
name=dbquerys
生命周期=开始
这=4c6f44a0
ConcurrentTransacticedReceiver的数量=4
CreateMultipleTransactedReceiver=false
已连接=真
supportedProtocols=[jdbc]
服务覆盖=
}
,name='endpoint.jdbc.insertRecord',mep=ONE_-WAY,properties={queryTimeout=-1},transactionConfig=Transaction{factory=null,action=interference,timeout=0},deleteUnceptedMessages=false,initialState=started,responseTimeout=10000,endpointEncoding=UTF-8,disableTransportTransformer=false}。消息负载的类型为:HashMap
代码:MULE_错误--2
--------------------------------------------------------------------------------
异常堆栈是:
1.该语句被中止,因为它会导致在“TESTTABLE”上定义的“SQL140317160209540”标识的唯一或主键约束或唯一索引中出现重复的键值。(org.apache.derby.iapi.error.StandardException)
org.apache.derby.iapi.error.StandardException:-1(null)
2.该语句被中止,因为它会导致在“TESTTABLE”上定义的“SQL140317160209540”标识的唯一或主键约束或唯一索引中出现重复的键值。(SQL代码:30000,SQL状态:+23505)(org.apache.derby.impl.jdbc.EmbedSQLException)
org.apache.derby.impl.jdbc.SQLExceptionFactory:-1(null)
3.该语句被中止,因为它会导致在“TESTTABLE”上定义的“SQL140317160209540”标识的唯一或主键约束或唯一索引中出现重复的键值。(SQL代码:30000,SQL状态:+23505)(java.SQL.SQLIntegrityConstraintViolationException)
org.apache.derby.impl.jdbc.SQLExceptionFactory40:-1(null)
4.该语句被中止,因为它会导致在“TESTTABLE”上定义的“SQL140317160209540”标识的唯一或主键约束或唯一索引中出现重复的键值。查询:插入TestTable(Id,Quantity)值(?,)参数:[261000153085,7](SQL代码:30000,SQL状态:+23505)(java.SQL.SQLException)
org.apache.commons.dbutils.QueryRunner:540(空)
5.未能通过端点路由事件:DefaultOutboundEndpoint{endpointUri=jdbc://insertRecord,connector=jdbc连接器
{
name=dbquerys
生命周期=开始
这=4c6f44a0
ConcurrentTransacticedReceiver的数量=4
CreateMultipleTransactedReceiver=false
已连接=真
supportedProtocols=[jdbc]
服务覆盖=
}
,name='endpoint.jdbc.insertRecord',mep=ONE_-WAY,properties={queryTimeout=-1},transactionConfig=Transaction{factory=null,action=interference,timeout=0},deleteUnceptedMessages=false,initialState=started,responseTimeout=10000,endpointEncoding=UTF-8,disableTransportTransformer=false}。消息负载的类型为:HashMap(org.mule.api.transport.DispatchException)
org.mule.transport.AbstractMessageDispatcher:109(http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transport/DispatchException.html)
--------------------------------------------------------------------------------
根异常堆栈跟踪:
错误23505:该语句被中止,因为它会在“TESTTABLE”上定义的“SQL140317160209540”标识的唯一或主键约束或唯一索引中导致重复的键值。
位于org.apache.derby.iapi.error.StandardException.newException(未知源)
位于org.apache.derby.impl.sql.execute.IndexChanger.insertAndCheckDups(未知源)
位于org.apache.derby.impl.sql.execute.IndexChanger.doInsert(未知源)
+3个以上(为所有内容设置调试级别日志或“-Dmule.verbose.exceptions=true”)
********************************************************************************
我已经定义了一个Choice异常策略来处理不同的异常,并使用以下方法来处理概述的案例:

<catch-exception-strategy when="#[exception.causeMatches(org.apache.derby.*)]" doc:name="Catch Exception Strategy">         
     <logger level="ERROR" message="Database error." doc:name="Logger"/>
</catch-exception-strategy>


但是,异常总是被路由到默认策略,而不是上面定义的捕获策略。我假设这是我的'when'表达式的问题,我只是不知道如何解决它。任何帮助都将不胜感激。谢谢

您需要一个用于方法因果匹配的字符串参数:


尝试使用引号:exception.causeMatches('org.apache.derby.*')

这是一个以正则表达式作为参数的方法

试试那个愚蠢的,它应该有用

<catch-exception-strategy when="#[exception.causeMatches('org.?apache.?derby.?.*')]" doc:name="Catch Exception Strategy">         
     <logger level="ERROR" message="Database error." doc:name="Logger"/>
</catch-exception-strategy>

希望这有帮助。

如果错误是

java.sql.SQLIntegrityConstraintViolationException:ORA-00001:违反了唯一约束
这可以通过在代码中使用条件来处理,如图所示,在错误中使用重要关键字,如图所示:

catch (SQLException e) { 
    if(e.getMessage().contains("unique constraint")) {        
        System.out.println("PID Repeated. Enter a different PID");
    } 
    return -1; 
}

请提供完整的流量配置。这可能会提供一些额外的线索。@user1760178,我认为错误在于我定义的“when”表达式。我有另一个很好用的方法:
这仍然不起作用,可能是因为异常堆栈中有多个项?谢谢您的回复。不幸的是,这仍然不起作用,并且调用了默认的异常策略。我是否可以将
exception.causedBy(…)
与其他表达式一起使用?我尝试了
exception.causedBy(org.apache.derby)
,但运气不好……为了使用正则表达式这种场景,只有exception.causeMatches可以工作。再一个