Skip to content

Commit

Permalink
定时任务支持并发控制
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzongzhuan committed Mar 13, 2019
1 parent 9fc4251 commit e34b4ea
Show file tree
Hide file tree
Showing 24 changed files with 424 additions and 294 deletions.
11 changes: 3 additions & 8 deletions ruoyi-common/src/main/java/com/ruoyi/common/config/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Global
/**
* 当前对象实例
*/
private static Global global = null;
private static Global global;

/**
* 保存全局属性值
Expand All @@ -34,18 +34,13 @@ private Global()
}

/**
* 静态工厂方法 获取当前对象实例 多线程安全单例模式(使用双重同步锁)
* 静态工厂方法
*/

public static synchronized Global getInstance()
{
if (global == null)
{
synchronized (Global.class)
{
if (global == null)
global = new Global();
}
global = new Global();
}
return global;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
*/
public interface ScheduleConstants
{
public static final String TASK_CLASS_NAME = "__TASK_CLASS_NAME__";
public static final String TASK_CLASS_NAME = "TASK_CLASS_NAME";

public static final String TASK_PROPERTIES = "__TASK_PROPERTIES__";
/** 执行目标key */
public static final String TASK_PROPERTIES = "TASK_PROPERTIES";

/** 默认 */
public static final String MISFIRE_DEFAULT = "0";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public Object value(final String name)
{
return endArray(matcher.group(1), matcher.group(2), new EndArrayCallback<Object>()
{
@Override
public Object callback(JSONArray arr, int index)
{
return elementAt(arr, index);
Expand Down Expand Up @@ -257,6 +258,7 @@ public JSONObject value(final String name, final Object value)
{
endArray(matcher.group(1), matcher.group(2), new EndArrayCallback<Void>()
{
@Override
public Void callback(JSONArray arr, int index)
{
elementAt(arr, index, value);
Expand Down Expand Up @@ -285,6 +287,7 @@ public JSONObject obj(final String name)
{
return endArray(matcher.group(1), matcher.group(2), new EndArrayCallback<JSONObject>()
{
@Override
public JSONObject callback(JSONArray arr, int index)
{
return objAt(arr, index);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.ruoyi.common.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import org.apache.commons.lang3.exception.ExceptionUtils;

/**
* 错误信息处理类。
*
* @author ruoyi
*/
public class ExceptionUtil
{

/**
* 获取exception的详细错误信息。
*/
public static String getExceptionMessage(Throwable e)
{
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw, true));
String str = sw.toString();
return str;
}

public static String getRootErrorMseeage(Exception e)
{
Throwable root = ExceptionUtils.getRootCause(e);
root = (root == null ? e : root);
if (root == null)
{
return "";
}
String msg = root.getMessage();
if (msg == null)
{
return "null";
}
return StringUtils.defaultString(msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static Object getProperty(Map<?, ?> map, Object qualifiedKey)
if (map != null && !map.isEmpty() && qualifiedKey != null)
{
String input = String.valueOf(qualifiedKey);
if (!input.equals(""))
if (!"".equals(input))
{
if (input.contains("."))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,9 @@ private void createExcelField()
{
tempClass = tempClass.getSuperclass();
if (tempClass != null)
{
tempFields.addAll(Arrays.asList(tempClass.getDeclaredFields()));
}
}
putToFields(tempFields);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
Expand All @@ -15,6 +16,7 @@
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
Expand Down Expand Up @@ -65,18 +67,10 @@ public AjaxResult export(SysJob job)
@RequiresPermissions("monitor:job:remove")
@PostMapping("/remove")
@ResponseBody
public AjaxResult remove(String ids)
public AjaxResult remove(String ids) throws SchedulerException
{
try
{
jobService.deleteJobByIds(ids);
return success();
}
catch (Exception e)
{
e.printStackTrace();
return error(e.getMessage());
}
jobService.deleteJobByIds(ids);
return success();
}

@RequiresPermissions("monitor:job:detail")
Expand All @@ -90,14 +84,12 @@ public String detail(@PathVariable("jobId") Long jobId, ModelMap mmap)

/**
* 任务调度状态修改
*
* @throws Exception
*/
@Log(title = "定时任务", businessType = BusinessType.UPDATE)
@RequiresPermissions("monitor:job:changeStatus")
@PostMapping("/changeStatus")
@ResponseBody
public AjaxResult changeStatus(SysJob job)
public AjaxResult changeStatus(SysJob job) throws SchedulerException
{
return toAjax(jobService.changeStatus(job));
}
Expand All @@ -109,9 +101,10 @@ public AjaxResult changeStatus(SysJob job)
@RequiresPermissions("monitor:job:changeStatus")
@PostMapping("/run")
@ResponseBody
public AjaxResult run(SysJob job)
public AjaxResult run(SysJob job) throws SchedulerException
{
return toAjax(jobService.run(job));
jobService.run(job);
return success();
}

/**
Expand All @@ -125,7 +118,6 @@ public String add()

/**
* 新增保存调度
* @throws Exception
*/
@Log(title = "定时任务", businessType = BusinessType.INSERT)
@RequiresPermissions("monitor:job:add")
Expand All @@ -148,13 +140,12 @@ public String edit(@PathVariable("jobId") Long jobId, ModelMap mmap)

/**
* 修改保存调度
* @throws Exception
*/
@Log(title = "定时任务", businessType = BusinessType.UPDATE)
@RequiresPermissions("monitor:job:edit")
@PostMapping("/edit")
@ResponseBody
public AjaxResult editSave(SysJob job ) throws Exception
public AjaxResult editSave(SysJob job) throws SchedulerException, TaskException
{
return toAjax(jobService.updateJobCron(job));
}
Expand Down
18 changes: 16 additions & 2 deletions ruoyi-quartz/src/main/java/com/ruoyi/quartz/domain/SysJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public class SysJob extends BaseEntity implements Serializable
@Excel(name = "计划策略 ")
private String misfirePolicy = ScheduleConstants.MISFIRE_DEFAULT;

/** 是否并发执行(0允许 1禁止) */
private String concurrent;

/** 任务状态(0正常 1暂停) */
@Excel(name = "任务状态", readConverterExp = "0=正常,1=暂停")
private String status;
Expand Down Expand Up @@ -130,6 +133,16 @@ public void setMisfirePolicy(String misfirePolicy)
this.misfirePolicy = misfirePolicy;
}

public String getConcurrent()
{
return concurrent;
}

public void setConcurrent(String concurrent)
{
this.concurrent = concurrent;
}

public String getStatus()
{
return status;
Expand All @@ -139,7 +152,7 @@ public void setStatus(String status)
{
this.status = status;
}

@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
Expand All @@ -151,6 +164,7 @@ public String toString() {
.append("cronExpression", getCronExpression())
.append("nextValidTime", getNextValidTime())
.append("misfirePolicy", getMisfirePolicy())
.append("concurrent", getConcurrent())
.append("status", getStatus())
.append("createBy", getCreateBy())
.append("createTime", getCreateTime())
Expand All @@ -159,4 +173,4 @@ public String toString() {
.append("remark", getRemark())
.toString();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.ruoyi.quartz.domain;

import java.util.Date;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;

Expand Down Expand Up @@ -46,6 +48,14 @@ public class SysJobLog extends BaseEntity
@Excel(name = "异常信息")
private String exceptionInfo;

/** 开始时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date startTime;

/** 结束时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date endTime;

public Long getJobLogId()
{
return jobLogId;
Expand Down Expand Up @@ -125,6 +135,26 @@ public void setExceptionInfo(String exceptionInfo)
{
this.exceptionInfo = exceptionInfo;
}

public Date getStartTime()
{
return startTime;
}

public void setStartTime(Date startTime)
{
this.startTime = startTime;
}

public Date getEndTime()
{
return endTime;
}

public void setEndTime(Date endTime)
{
this.endTime = endTime;
}

@Override
public String toString() {
Expand All @@ -137,7 +167,8 @@ public String toString() {
.append("jobMessage", getJobMessage())
.append("status", getStatus())
.append("exceptionInfo", getExceptionInfo())
.append("createTime", getCreateTime())
.append("startTime", getStartTime())
.append("endTime", getEndTime())
.toString();
}
}
Loading

0 comments on commit e34b4ea

Please sign in to comment.