|
|
@@ -8,6 +8,7 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
+import java.util.concurrent.*;
|
|
|
|
|
|
@Component
|
|
|
public class XinbaoTask {
|
|
|
@@ -21,14 +22,68 @@ public class XinbaoTask {
|
|
|
//定时注解 @cron
|
|
|
@Scheduled(cron = "0 0/3 * * * ?")
|
|
|
public void downLoadSale(){
|
|
|
- logger.info("订单对接 {} ",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
- saleDownLoadTask.execute();
|
|
|
+ logger.info("订单对接 TaskBegin{} ",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
+ // 创建一个线程池,具有一个线程
|
|
|
+ ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
|
+
|
|
|
+ // 定义一个耗时的任务
|
|
|
+ Callable<String> task = () -> {
|
|
|
+ //Thread.sleep(5000); // 模拟耗时操作
|
|
|
+ saleDownLoadTask.execute();
|
|
|
+ return "Task completed";
|
|
|
+ };
|
|
|
+ Future<String> future = null;
|
|
|
+ try {
|
|
|
+ // 提交任务并设置超时时间为2秒
|
|
|
+ future = executor.submit(task);
|
|
|
+ String result = future.get(2*60, TimeUnit.SECONDS); // 获取结果,最多等待2分钟
|
|
|
+ logger.info("订单对接 TaskEnd {} ",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
+ future.cancel(true); // 尝试取消正在执行的任务
|
|
|
+ } catch (TimeoutException e) {
|
|
|
+ // 超时异常处理
|
|
|
+ System.out.println("Task timed out");
|
|
|
+ logger.info("订单对接 Task timed out {} ",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
+ future.cancel(true); // 尝试取消正在执行的任务
|
|
|
+ } catch (InterruptedException | ExecutionException e) {
|
|
|
+ // 其他异常处理
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ // 关闭线程池
|
|
|
+ executor.shutdownNow();
|
|
|
+ }
|
|
|
}
|
|
|
//定时注解 @cron
|
|
|
@Scheduled(cron = "0 0/3 * * * ?")
|
|
|
public void downLoadSaleOut(){
|
|
|
logger.info("出货对接 {} ",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
- saleOutTask.execute();
|
|
|
+ // 创建一个线程池,具有一个线程
|
|
|
+ ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
|
+
|
|
|
+ // 定义一个耗时的任务
|
|
|
+ Callable<String> task = () -> {
|
|
|
+ //Thread.sleep(5000); // 模拟耗时操作
|
|
|
+ saleOutTask.execute();
|
|
|
+ return "Task completed";
|
|
|
+ };
|
|
|
+ Future<String> future = null;
|
|
|
+ try {
|
|
|
+ // 提交任务并设置超时时间为2秒E
|
|
|
+ future = executor.submit(task);
|
|
|
+ String result = future.get(2*60, TimeUnit.SECONDS); // 获取结果,最多等待2分钟
|
|
|
+ logger.info("出货对接 TaskEnd {} ",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
+ future.cancel(true); // 尝试取消正在执行的任务
|
|
|
+ } catch (TimeoutException e) {
|
|
|
+ // 超时异常处理
|
|
|
+ System.out.println("Task timed out");
|
|
|
+ logger.info("出货对接 Task timed out {} ",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
+ future.cancel(true); // 尝试取消正在执行的任务
|
|
|
+ } catch (InterruptedException | ExecutionException e) {
|
|
|
+ // 其他异常处理
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ // 关闭线程池
|
|
|
+ executor.shutdownNow();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//定时注解 @cron
|