Spring 轮询器在特定重试次数后回滚异常
Spring poller roll back on exception after a particular retry count
我的轮询器从数据库中获取数据并将其传递给服务激活器。
如果在服务激活器方法中发生任何异常,我应该将获取的数据回滚到其先前的状态,并且应该再次将相同的数据发送到服务激活器,仅针对特定的重试次数(比如 3)。 **是否可以在 xml 配置 中执行此操作。 ?有关详细信息,我将分享轮询器配置和服务激活器。
poller.xml
<int-jdbc:inbound-channel-adapter id="datachannel"
query="select loyalty_id,process_id,mobile_uid from TBL_RECEIPT where r_cre_time
=(select min(r_cre_time) from TBL_RECEIPT where receipt_status=0)"
data-source="dataSource" max-rows-per-poll="1"
update="update TBL_RECEIPT set receipt_status=11 where process_id in (:process_id)">
<int:poller fixed-rate="5000">
<int:transactional/>
</int:poller>
</int-jdbc:inbound-channel-adapter>
<int:channel id="errors">
<int:queue/>
</int:channel>
<bean id="poller" class="main.java.com.as.poller.PollerService" />
<int:channel id="executerchannel">
<int:dispatcher task-executor="taskExecutor" />
</int:channel>
<task:executor id="taskExecutor" pool-size="2" />
<int:service-activator input-channel="datachannel"
output-channel="executerchannel" ref="poller" method="getRecordFromPoller">
<int:request-handler-advice-chain>
<int:retry-advice recovery-channel="errors" />
</int:request-handler-advice-chain>
</int:service-activator>
<int:service-activator input-channel="executerchannel"
ref="poller" method="getDataFromExecuterChannel">
</int:service-activator>
服务激活方法
@SuppressWarnings({ "unchecked", "rawtypes" })
@ServiceActivator
public void processMessage(Message message) throws IOException {
int capLimit = Integer.parseInt(env.getProperty("capping_limit"));
List<Map<String, Object>> rows = (List<Map<String, Object>>) message
.getPayload();
for (Map<String, Object> row : rows) {
String loyaltyId = (String) row.get("loyalty_id");
String processId = (String) row.get("process_id");
String xid=(String)row.get("mobile_uid");
我听说 int:transactional 在轮询器 configuration.But 中使用,当我添加它时,即使在成功交易后它也会获取相同的记录。(意味着它每次都会回滚)。
谁能帮我解决这个问题?
您可以在服务激活器中添加一个retry request-handler-advice。
要使重试有状态(抛出异常以便事务回滚),您需要提供 RetryStateGenerator
。否则线程将在重试期间被挂起。
无论使用有状态重试还是无状态重试,您确实应该使用事务轮询器,以便仅在成功后应用更新。
means its getting rolled back everytime
事务轮询器只会在抛出异常时回滚。因此,如果您看到同一行,则一定有问题。
为所有 org.springframework
打开 DEBUG 日志记录以跟踪消息流和事务 activity。
我的轮询器从数据库中获取数据并将其传递给服务激活器。 如果在服务激活器方法中发生任何异常,我应该将获取的数据回滚到其先前的状态,并且应该再次将相同的数据发送到服务激活器,仅针对特定的重试次数(比如 3)。 **是否可以在 xml 配置 中执行此操作。 ?有关详细信息,我将分享轮询器配置和服务激活器。 poller.xml
<int-jdbc:inbound-channel-adapter id="datachannel"
query="select loyalty_id,process_id,mobile_uid from TBL_RECEIPT where r_cre_time
=(select min(r_cre_time) from TBL_RECEIPT where receipt_status=0)"
data-source="dataSource" max-rows-per-poll="1"
update="update TBL_RECEIPT set receipt_status=11 where process_id in (:process_id)">
<int:poller fixed-rate="5000">
<int:transactional/>
</int:poller>
</int-jdbc:inbound-channel-adapter>
<int:channel id="errors">
<int:queue/>
</int:channel>
<bean id="poller" class="main.java.com.as.poller.PollerService" />
<int:channel id="executerchannel">
<int:dispatcher task-executor="taskExecutor" />
</int:channel>
<task:executor id="taskExecutor" pool-size="2" />
<int:service-activator input-channel="datachannel"
output-channel="executerchannel" ref="poller" method="getRecordFromPoller">
<int:request-handler-advice-chain>
<int:retry-advice recovery-channel="errors" />
</int:request-handler-advice-chain>
</int:service-activator>
<int:service-activator input-channel="executerchannel"
ref="poller" method="getDataFromExecuterChannel">
</int:service-activator>
服务激活方法
@SuppressWarnings({ "unchecked", "rawtypes" })
@ServiceActivator
public void processMessage(Message message) throws IOException {
int capLimit = Integer.parseInt(env.getProperty("capping_limit"));
List<Map<String, Object>> rows = (List<Map<String, Object>>) message
.getPayload();
for (Map<String, Object> row : rows) {
String loyaltyId = (String) row.get("loyalty_id");
String processId = (String) row.get("process_id");
String xid=(String)row.get("mobile_uid");
我听说 int:transactional 在轮询器 configuration.But 中使用,当我添加它时,即使在成功交易后它也会获取相同的记录。(意味着它每次都会回滚)。 谁能帮我解决这个问题?
您可以在服务激活器中添加一个retry request-handler-advice。
要使重试有状态(抛出异常以便事务回滚),您需要提供 RetryStateGenerator
。否则线程将在重试期间被挂起。
无论使用有状态重试还是无状态重试,您确实应该使用事务轮询器,以便仅在成功后应用更新。
means its getting rolled back everytime
事务轮询器只会在抛出异常时回滚。因此,如果您看到同一行,则一定有问题。
为所有 org.springframework
打开 DEBUG 日志记录以跟踪消息流和事务 activity。