我试图为我的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)
********************************************************************************
我已经定义了一个选择异常策略来处理不同的异常,并使用以下方法来处理所概述的案例:
<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>
但是,异常总是被路由到默认策略而不是上面定义的catch策略。我认为这是我的问题所在&#39;当&#39;表达,我只是不确定如何解决它。任何帮助,将不胜感激。感谢
答案 0 :(得分:1)
这是一种以正则表达式作为参数的方法。
尝试使用它,它应该可以工作。
<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>
希望这有帮助。
答案 1 :(得分:0)
您需要一个方法causeMatches的String参数:
http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MessagingException.html
尝试使用引号:exception.causeMatches(&#39; org.apache.derby。*&#39;)
答案 2 :(得分:0)
如果错误是
java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint violated
这可以通过在代码中使用条件来帮助处理,如图所示使用错误中的重要关键字:
catch (SQLException e) {
if(e.getMessage().contains("unique constraint")) {
System.out.println("PID Repeated. Enter a different PID");
}
return -1;
}