|
| 1 | +package com.lts.job.queue.mysql; |
| 2 | + |
| 3 | +import com.lts.job.core.cluster.Config; |
| 4 | +import com.lts.job.core.file.FileUtils; |
| 5 | +import com.lts.job.core.util.DateUtils; |
| 6 | +import com.lts.job.core.util.JobQueueUtils; |
| 7 | +import com.lts.job.queue.CronJobQueue; |
| 8 | +import com.lts.job.queue.domain.JobPo; |
| 9 | +import com.lts.job.queue.exception.JobQueueException; |
| 10 | +import com.lts.job.queue.mysql.support.ResultSetHandlerHolder; |
| 11 | + |
| 12 | +import java.io.InputStream; |
| 13 | +import java.sql.SQLException; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author Robert HG (254963746@qq.com) on 5/31/15. |
| 17 | + */ |
| 18 | +public class MysqlCronJobQueue extends AbstractMysqlJobQueue implements CronJobQueue { |
| 19 | + |
| 20 | + public MysqlCronJobQueue(Config config) { |
| 21 | + super(config); |
| 22 | + // create table |
| 23 | + try { |
| 24 | + InputStream is = this.getClass().getClassLoader().getResourceAsStream("sql/lts_cron_job_queue.sql"); |
| 25 | + String sql = FileUtils.read(is); |
| 26 | + sql = sql.replace("{tableName}", JobQueueUtils.CRON_JOB_QUEUE); |
| 27 | + getSqlTemplate().update(sql); |
| 28 | + } catch (Exception e) { |
| 29 | + throw new JobQueueException("create table error!", e); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public boolean add(JobPo jobPo) { |
| 35 | + jobPo.setGmtCreated(DateUtils.currentTimeMillis()); |
| 36 | + jobPo.setGmtModified(jobPo.getGmtCreated()); |
| 37 | + return super.add(JobQueueUtils.CRON_JOB_QUEUE, jobPo); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public JobPo finish(String jobId) { |
| 42 | + String sql = "SELECT * FROM `{tableName}` WHERE job_id = ?".replace("{tableName}", JobQueueUtils.CRON_JOB_QUEUE); |
| 43 | + try { |
| 44 | + return getSqlTemplate().query(sql, ResultSetHandlerHolder.JOB_PO_RESULT_SET_HANDLER, jobId); |
| 45 | + } catch (SQLException e) { |
| 46 | + throw new JobQueueException(e); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public boolean remove(String jobId) { |
| 52 | + String sql = "DELETE FROM `{tableName}` WHERE job_id = ?".replace("{tableName}", JobQueueUtils.CRON_JOB_QUEUE); |
| 53 | + try { |
| 54 | + return getSqlTemplate().update(sql, jobId) == 0; |
| 55 | + } catch (SQLException e) { |
| 56 | + throw new JobQueueException(e); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments