Quartz 使用任务队列实现顺序调度

本文介绍了一种利用Quartz框架结合任务队列实现任务顺序调度的方法。通过将多个任务放入任务队列中,并由单一线程按顺序执行,确保了任务执行的有序性。文章详细展示了如何使用Java实现这一方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Quartz 使用任务队列实现顺序调度

把需要并行的任务塞到一个任务队列里面,用一个线程去执行
var:

package com.etc.clear.data.common;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * 变量
 * <p>
 * 
 * @ClassName : Variable
 *            </p>
 *            <p>
 * @Description : TODO
 *              </p>
 *              <p>
 * @Author : HuaZai
 *         </p>
 *         <p>
 * @ContactInformation : 1461522031@qq.com/huazai6789@aliyun.com
 *                     </p>
 * 
 * @Date : 2018年1月4日 下午4:22:14
 * @Version : V1.0.0
 *
 */
public class Variable {

    private static BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(10);
    private static boolean running = false;

    public BlockingQueue<Runnable> getQueue() {
        return queue;
    }

    @SuppressWarnings("static-access")
    public void setQueue(BlockingQueue<Runnable> queue) {
        this.queue = queue;
    }

    public boolean isRunning() {
        return running;
    }

    @SuppressWarnings("static-access")
    public void setRunning(boolean running) {
        this.running = running;
    }

}

job:

package com.etc.clear.data.job;

import java.util.concurrent.TimeUnit;

import com.etc.clear.data.common.Variable;

/**
 * 定义导出数据任务
 * <p>
 * 
 * @ClassName : ExpDataJob
 *            </p>
 *            <p>
 * @Description : TODO
 *              </p>
 *              <p>
 * @Author : HuaZai
 *         </p>
 *         <p>
 * @ContactInformation : 1461522031@qq.com/huazai6789@aliyun.com
 *                     </p>
 * 
 * @Date : 2018年1月4日 下午4:05:10
 * @Version : V1.0.0
 *
 */
public class ExpDataJob implements Runnable {

    Variable var = new Variable();

    @Override
    public void run() {
        while (var.isRunning()) {
            try {
                //取得一个任务
                Runnable job = var.getQueue().poll(5000L, TimeUnit.MILLISECONDS);
                System.out.println("x-x-x-x-x-x-x 进入数据《导出》阶段 ....");
                if (job != null) {
                    job.run();
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e.toString());
            }

        }
    }

}

init:

package com.etc.clear.data.common;

import com.etc.clear.data.utils.OrderdJobUtils;

/**
 * 初始化数据清理任务
 * <p>
 * 
 * @ClassName : InitDataQuartzCommon
 *            </p>
 *            <p>
 * @Description : TODO
 *              </p>
 *              <p>
 * @Author : HuaZai
 *         </p>
 *         <p>
 * @ContactInformation : 1461522031@qq.com/huazai6789@aliyun.com
 *                     </p>
 * 
 * @Date : 2018年1月4日 下午3:45:09
 * @Version : V1.0.0
 *
 */
public class InitDataQuartzCommon {

    private static int count = 0;

    /**
     * 初始化数据清理任务
     * <p>
     * 
     * @Title : startClearData
     *        </p>
     *        <p>
     * @Description : TODO
     *              </p>
     *              <p>
     * @Author : HuaZai
     *         </p>
     * 
     * @Date : 2018年1月4日 下午3:47:47
     */
    public void startClearData() {

        // 初始化任务顺序
        OrderdJobUtils orderdJobUtils = new OrderdJobUtils();
        // 启动任务
        orderdJobUtils.start();

        for (int i = 0; i < 10; i++) {
            orderdJobUtils.insertJob(new Runnable() {
                @Override
                public void run() {

                    System.out.println(count++);
                }
            });
        }

        synchronized (orderdJobUtils) {
            try {
                orderdJobUtils.wait(2000L);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e.toString());
            }
        }

        orderdJobUtils.stop();

    }
}

utils:

package com.etc.clear.data.utils;

import java.util.concurrent.TimeUnit;

import com.etc.clear.data.common.Variable;
import com.etc.clear.data.job.ExpDataJob;

/**
 * 维护任务顺序
 * <p>
 * 
 * @ClassName : OrderdJobUtils
 *            </p>
 *            <p>
 * @Description : TODO
 *              </p>
 *              <p>
 * @Author : HuaZai
 *         </p>
 *         <p>
 * @ContactInformation : 1461522031@qq.com/huazai6789@aliyun.com
 *                     </p>
 * 
 * @Date : 2018年1月4日 下午4:49:35
 * @Version : V1.0.0
 *
 */
public class OrderdJobUtils {

    Variable var = new Variable();

    /**
     * 启动任务
     * <p>
     * 
     * @Title : start
     *        </p>
     *        <p>
     * @Description : TODO
     *              </p>
     *              <p>
     * @Author : HuaZai
     *         </p>
     * 
     * @Date : 2018年1月4日 下午4:40:51
     */
    public void start() {
        var.setRunning(true);
        Thread thread = new Thread(new ExpDataJob());
        thread.start();
    }

    /**
     * 停止任务
     * <p>
     * 
     * @Title : stop
     *        </p>
     *        <p>
     * @Description : TODO
     *              </p>
     *              <p>
     * @Author : HuaZai
     *         </p>
     * 
     * @Date : 2018年1月4日 下午4:41:20
     */
    public void stop() {
        var.setRunning(false);
    }

    /**
     * 添加任务
     * <p>
     * 
     * @Title : insertJob
     *        </p>
     *        <p>
     * @Description : TODO
     *              </p>
     *              <p>
     * @Author : HuaZai
     *         </p>
     * 
     * @Date : 2018年1月4日 下午4:43:08
     */
    public void insertJob(Runnable job) {

        try {
            //插入 一个任务
            if (var.getQueue().offer(job, 5000L, TimeUnit.MILLISECONDS) == false) {
                // 处理任务插入失败的情况
                System.out.println("E-E-E-E-E-E-E: 任务插入失败 !~ ");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.toString());
        }
    }

}

main:

package com.etc.clear;

import com.etc.clear.data.common.InitDataQuartzCommon;

/**
 * 程序启动入口/调用任务的类
 * <p>
 * 
 * @ClassName : ClsMain
 *            </p>
 *            <p>
 * @Description : TODO
 *              </p>
 *              <p>
 * @Author : HuaZai
 *         </p>
 *         <p>
 * @ContactInformation : 1461522031@qq.com/huazai6789@aliyun.com
 *                     </p>
 * 
 * @Date : 2018年1月3日 下午1:21:56
 * 
 * @Version : V1.0.0
 *
 */
public class ApplicationMain {

    public static void main(String[] args) {
        // 初始化清理数据任务
        InitDataQuartzCommon dataQuartzCommon = new InitDataQuartzCommon();
        // 启动数据清理任务
        dataQuartzCommon.startClearData();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TechBro华仔

日拱一卒无有尽,功不唐捐终入海

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值