提交 cac1b401 编写于 作者: L leejoker

使用并行流和异步处理多线程

上级 c3a1f53d
......@@ -6,6 +6,7 @@ import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.scheduling.annotation.EnableAsync;
/**
* 启动程序
......@@ -15,6 +16,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, PageHelperAutoConfiguration.class })
@MapperScan("com.linkwechat.*.mapper")
@ForestScan(basePackages = "com.linkwechat.wecom.client")
@EnableAsync
public class LinkWeChatApplication
{
public static void main(String[] args)
......
......@@ -106,16 +106,12 @@ public class WeCustomerController extends BaseController
@PreAuthorize("@ss.hasPermi('customerManage:customer:sync')")
@Log(title = "企业微信客户同步接口", businessType = BusinessType.DELETE)
@GetMapping("/synchWeCustomer")
public AjaxResult synchWeCustomer(){
SecurityContext securityContext = SecurityContextHolder.getContext();
public AjaxResult synchWeCustomer() {
try {
Threads.SINGLE_THREAD_POOL.execute(new Runnable() {
@Override
public void run() {
SecurityContextHolder.setContext(securityContext);
SecurityContext context = SecurityContextHolder.getContext();
SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
SecurityContextHolder.setContext(context);
weCustomerService.synchWeCustomer();
}
});
} catch (Exception e) {
e.printStackTrace();
}
......
......@@ -56,6 +56,11 @@ spring:
max-file-size: 10MB
# 设置总上传的文件大小
max-request-size: 20MB
task:
execution:
pool:
keep-alive: 10s
thread-name-prefix: common-pool-
# 服务模块
devtools:
restart:
......
......@@ -26,6 +26,7 @@ import com.linkwechat.wecom.domain.vo.WeMakeCustomerTag;
import com.linkwechat.wecom.mapper.WeCustomerMapper;
import com.linkwechat.wecom.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
......@@ -121,21 +122,18 @@ public class WeCustomerServiceImpl extends ServiceImpl<WeCustomerMapper, WeCusto
* @return
*/
@Override
@Async
@Transactional(rollbackFor = Exception.class)
public void synchWeCustomer() {
FollowUserList followUserList = weCustomerClient.getFollowUserList();
if (WeConstans.WE_SUCCESS_CODE.equals(followUserList.getErrcode())
&& ArrayUtil.isNotEmpty(followUserList.getFollow_user())) {
SecurityContext securityContext = SecurityContextHolder.getContext();
Arrays.asList(followUserList.getFollow_user())
.stream().forEach(k -> {
.parallelStream().forEach(k -> {
try {
Threads.SINGLE_THREAD_POOL.execute(() -> {
SecurityContextHolder.setContext(securityContext);
weFlowerCustomerHandle(k);
});
} catch (Exception e) {
e.printStackTrace();
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册