提交 cac1b401 编写于 作者: L leejoker

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

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