Commit d537a4a4 by 林永健

feat(async): 添加异步任务执行配置及异步关注消息推送

- 新增AsyncConfig配置类,定义线程池taskExecutor
- 在MerchantBiFollowService中引入异步推送方法triggerFollowPush
- 使用@Async注解实现triggerFollowPush异步执行
- 在事务提交后调用异步推送,保证数据一致性
- 通过TransactionSynchronizationManager绑定事务提交后回调触发推送任务
1 parent 866dd6d4
package cn.com.glsx.dj.peanalysis.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@Configuration
@EnableAsync
public class AsyncConfig {
@Bean(name = "taskExecutor")
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(3); // 核心线程数
executor.setMaxPoolSize(8); // 最大线程数
executor.setQueueCapacity(100); // 队列容量
executor.setThreadNamePrefix("async-");
executor.initialize();
return executor;
}
}
...@@ -29,9 +29,13 @@ import org.apache.commons.collections.CollectionUtils; ...@@ -29,9 +29,13 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.*; import java.util.*;
...@@ -83,9 +87,14 @@ public class MerchantBiFollowService { ...@@ -83,9 +87,14 @@ public class MerchantBiFollowService {
*/ */
@Autowired @Autowired
private WechatPushService wechatPushService; private WechatPushService wechatPushService;
@Autowired @Autowired
private Environment environment; private Environment environment;
@Autowired
@Lazy
private MerchantBiFollowService thiz;
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Boolean followMerchant(MerchantBiFollowReq request) { public Boolean followMerchant(MerchantBiFollowReq request) {
if (request.getMerchantId() == null || request.getBusinessType() == null || StringUtils.isBlank(request.getStaffUuid())) { if (request.getMerchantId() == null || request.getBusinessType() == null || StringUtils.isBlank(request.getStaffUuid())) {
...@@ -133,7 +142,16 @@ public class MerchantBiFollowService { ...@@ -133,7 +142,16 @@ public class MerchantBiFollowService {
// 推送逻辑 (场景1) // 推送逻辑 (场景1)
if (success) { if (success) {
triggerFollowPush(request); if (TransactionSynchronizationManager.isActualTransactionActive()) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
thiz.triggerFollowPush(request);
}
});
} else {
thiz.triggerFollowPush(request);
}
} }
} else if (Objects.equals(request.getOperateType(), 0)) { // 取消关注 } else if (Objects.equals(request.getOperateType(), 0)) { // 取消关注
...@@ -621,7 +639,8 @@ public class MerchantBiFollowService { ...@@ -621,7 +639,8 @@ public class MerchantBiFollowService {
businessType, merchantIds.size(), settings == null ? 0 : settings.size(), System.currentTimeMillis() - startMs); businessType, merchantIds.size(), settings == null ? 0 : settings.size(), System.currentTimeMillis() - startMs);
} }
private void triggerFollowPush(MerchantBiFollowReq request) { @Async
public void triggerFollowPush(MerchantBiFollowReq request) {
try { try {
// 指定人员才推送关注消息 // 指定人员才推送关注消息
String pushStaffPhone = environment.getProperty("dj.pe.analysis.follow.push.staff", String.class); String pushStaffPhone = environment.getProperty("dj.pe.analysis.follow.push.staff", String.class);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!