diff --git a/admin-core/src/main/java/com/ibeetl/admin/core/conf/AsyncConfig.java b/admin-core/src/main/java/com/ibeetl/admin/core/conf/AsyncConfig.java new file mode 100644 index 00000000..5feeaf03 --- /dev/null +++ b/admin-core/src/main/java/com/ibeetl/admin/core/conf/AsyncConfig.java @@ -0,0 +1,60 @@ +package com.ibeetl.admin.core.conf; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.AsyncConfigurer; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.lang.reflect.Method; +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; + +/** + * 功能描述:
+ * 配置异步线程池 + * + * @Author: 87966 + * @Date: 2022/12/8 10:48 + */ +@Configuration +@EnableAsync +@Slf4j +public class AsyncConfig implements AsyncConfigurer { + + @Override + @Bean("taskExecutor") + public Executor getAsyncExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + // 核心数 + executor.setCorePoolSize(20); + // 最大数 + executor.setMaxPoolSize(50); + // 队列大小 + executor.setQueueCapacity(200); + executor.setKeepAliveSeconds(60); + executor.setThreadNamePrefix("-TaskExecutor-"); + // 拒绝策略 任务提交线程自身执行,不会拒绝抛异常导致任务失败 + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + return executor; + } + + // 配置线程池执行异常的处理类(当多线程任务本身无catch处理时才抛给这个类进行处理) + // AsyncConfigurer 接口中的这个方法用于返回异常时的处理类,就是上面自己定义的类 + + @Override + public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { + return new MyAsyncExceptionHandler(); + } + + // 定义一个实现AsyncUncaughtExceptionHandler 接口的类,自定义多线程任务执行异常的处理逻辑 + + class MyAsyncExceptionHandler implements AsyncUncaughtExceptionHandler { + @Override + public void handleUncaughtException(Throwable throwable, Method method, Object... objects) { + log.error("Exception in async method:"+ throwable.getMessage()); + } + } +} \ No newline at end of file diff --git a/web/src/main/java/com/ibeetl/jlw/service/questionSettingQueue/AutoSubmitQuestionSetting.java b/web/src/main/java/com/ibeetl/jlw/service/questionSettingQueue/AutoSubmitQuestionSetting.java new file mode 100644 index 00000000..caa3f9aa --- /dev/null +++ b/web/src/main/java/com/ibeetl/jlw/service/questionSettingQueue/AutoSubmitQuestionSetting.java @@ -0,0 +1,113 @@ +//package com.ibeetl.jlw.service.questionSettingQueue; +// +//import com.alibaba.fastjson.JSON; +//import com.alibaba.fastjson.JSONObject; +//import com.alibaba.fastjson.TypeReference; +//import com.ibeetl.jlw.service.TeacherOpenCourseQuestionLogService; +//import lombok.RequiredArgsConstructor; +//import lombok.extern.slf4j.Slf4j; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.boot.autoconfigure.data.redis.RedisProperties; +//import org.springframework.data.redis.core.StringRedisTemplate; +//import org.springframework.scheduling.annotation.Async; +//import org.springframework.scheduling.annotation.EnableAsync; +//import org.springframework.stereotype.Service; +//import redis.clients.jedis.Jedis; +// +//import java.lang.reflect.Type; +//import java.util.Set; +//import java.util.UUID; +// +///** +// * 功能描述:
+// * 作业、考试、章节练习自动延时交卷 +// * +// * @Author: 87966 +// * @Date: 2022/12/8 10:55 +// */ +//@Service +//@Slf4j +//@EnableAsync +//@RequiredArgsConstructor +//public class AutoSubmitQuestionSetting { +// +// final private TeacherOpenCourseQuestionLogService teacherOpenCourseQuestionLogService; +// +// static class TaskItem { +// public String id; +// public T msg; +// } +// +// // fastjson序列化对象中存在generic类型时,需要使用TypeReference +// private Type TaskType = new TypeReference>() { +// }.getType(); +// +// /** +// * 将指定延迟消费的消息放入延时队列 +// * +// * @param msg +// */ +// public void delay(StringRedisTemplate jedis, String queueKey, T msg, long outTime) { +// TaskItem task = new TaskItem(); +// // 分配唯一的uuid +// task.id = UUID.randomUUID().toString(); +// task.msg = msg; +// // fastjson 序列化 +// String s = JSON.toJSONString(task); +// // 塞入延时队列, outTime 后再试 +// jedis.opsForZSet().add(queueKey, System.currentTimeMillis() + outTime, s); +// } +// +// /** +// * 轮训zset获取到期的任务进行处理 +// */ +// @Async("taskExecutor") +// public void loop(RedisProperties.Jedis jedis, String queueKey) { +// while (!Thread.interrupted()) { +// // 只取一条 +// Set values = jedis.zrangeByScore(queueKey, 0, System.currentTimeMillis(), 0, 1); +// if (values.isEmpty()) { +// try { +// // 歇会继续 +// Thread.sleep(500); +// } catch (InterruptedException e) { +// break; +// } +// continue; +// } +// String s = values.iterator().next(); +// // 抢到了并移除该元素 +// if (jedis.zrem(queueKey, s) > 0) { +// // fastjson反序列化 +// TaskItem task = JSON.parseObject(s, TaskType); +// this.handleMsg(task.msg); +// } +// } +// } +// +// /** +// * 消费消息 +// * +// * @param msg +// */ +// private void handleMsg(T msg) { +// // 将订单信息的json字符串转成json对象 +// JSONObject orderInfo = JSONObject.parseObject((String) msg); +// String tenant = orderInfo.getString("tenant"); +// String flowNo = orderInfo.getString("flowNo"); +// // 根据 tenant+flowNo 得到该订单 +// Order order = orderDao.getByTenantAndFlowNo(tenant, flowNo); +// // 判断该订单的状态 +// // 如果仍然为已提交(OrderStatusEnum.SUBMITTED),则将该订单状态自动修改为已取消(OrderStatusEnum.PAID) +// OrderState dbOrderState = orderStateDao.findByTenantAndOwner(tenant, order.getUuid()); +// OrderStatusEnum dbState = dbOrderState.getState(); +// if (OrderStatusEnum.SUBMITTED == dbState) { +// try { +// orderService.cancel(tenant, flowNo, "订单超时未支付自动取消订单"); +// } catch (Exception e) { +// log.error("订单自动取消失败,flowNo={}", flowNo); +// e.printStackTrace(); +// } +// } +// } +//} \ No newline at end of file diff --git a/web/src/main/java/com/ibeetl/jlw/service/questionSettingQueue/package-info.java b/web/src/main/java/com/ibeetl/jlw/service/questionSettingQueue/package-info.java new file mode 100644 index 00000000..ca695b52 --- /dev/null +++ b/web/src/main/java/com/ibeetl/jlw/service/questionSettingQueue/package-info.java @@ -0,0 +1,8 @@ +/** + * 功能描述:
+ * 作业、考试、章节练习自动延时交卷 + * + * @Author: 87966 + * @Date: 2022/12/8 10:37 + */ +package com.ibeetl.jlw.service.questionSettingQueue; \ No newline at end of file