使用场景
前端轮询
适用于处理前端轮询,避免轮询请求次数过于频繁造成服务器压力
实现效果
前端请求后不能立刻获得返回,在延时期间若某项业务完成,则主动返回,若期间未有指定业务完成,则指定延时时间后进行下一次长轮询。
实现方式
启动类
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public static class WebConf implements WebMvcConfigurer {
//配置异步支持,设置了一个用来异步执行业务逻辑的工作线程池,设置了默认的超时时间是40秒
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
configurer.setTaskExecutor(mvcTaskExecutor());
configurer.setDefaultTimeout(40000L);
}
}
}
接口
public interface CountersignLockService {
/**
* 解锁
* @param corpId
* @param processId
*/
void unLock(String corpId,String processId,String userId);
/**
* 尝试获取锁
* @param corpId
* @param taskId
* @return
*/
DeferredResult<Result<CountersignLockModel>> tryLock(String corpId, String taskId);
}
Service
@Slf4j
@Service
public class CountersignLockServiceImpl implements CountersignLockService {
@Autowired
private RedissonClient redisson;
private final String KEY_PREFIX = "countersign:";
@Autowired
private MissiveProcessTaskService missiveProcessTaskService;
@Autowired
private DingtalkProperites dingtalkProperites;
@Autowired
private OrgApiClient orgApiClient;
/**
* 客户端映射 key:processId value:包含了taskId跟延时请求对象
*/
private final ConcurrentMap<String,List<CountersignClientModel>> clientResultMap = Maps.newConcurrentMap();
@PostConstruct
private void init(){
RTopic topic = redisson.getTopic(KEY_PREFIX