`

Cube-web系统之Quartz+Spring实现任务监控管理

阅读更多

      Quartz 是 OpenSymphony 开源组织在任务调度领域的一个开源项目,完全基于 Java 实现。作为一个优秀的开源调度框架,Quartz 具有功能强大,应用灵活,易于集成的特点

 

      目前项目处于运维阶段中期,事情不多,所以有时间将自己用过的技术以及想要学习的技术整理一下,所以萌生一个自己做项目的想法,通过自己做项目将这些年使用的技术以及想要学习和深入了解的技术做一个总结,做项目第一件事就是给项目起名字,发现起名字这件事说简单也简单,说难也难,想取一个响亮点名字吧才发现自己语言有多么的匮乏,不过还好,由于最近再玩魔方,所以就用魔方的单词来作为项目的名字,反正最终目的是为了对自己这几年技术的总结,无所谓了.....

 



 

上图就是自己想到的项目模块,当然后续还会在增加,想到一些就加一些,目前只是实现了系统管理也就是权限部分还有系统监控部分的作业监控

 

项目简介:

项目名称:CUBE

项目使用的开发语言:Java

项目框架:Struts2+Spring+Hibernate+Maven

服务器:Tomcat

数据库:Mysql

开发工具:Eclipse

前端:easyui

 

目前CUBE项目的效果如图:

 

登陆界面:

 



 

登陆后的主界面

 



 

美工这活儿真心不好做啊,现在主界面的效果就是这样了,top和bottom还需要在美化一下,后期在做吧

 

下面是作业监控的最终效果:

 



 

触发器添加界面,触发器的时间规则使用CronTrigger

 



 

上边就是CUBE系统中的作业监控部分的最终实现

 

Quartz是Java领域最著名的开源任务调度工具。Quartz提供了极为广泛的特性如持久化任务,集群和分布式任务等,其特点如下:

 

  • 完全由Java写成,方便集成(Spring)
  • 伸缩性
  • 负载均衡
  • 高可用性

Quartz数据库核心表如下:

 

Table Name Description
QRTZ_CALENDARS 存储Quartz的Calendar信息
QRTZ_CRON_TRIGGERS 存储CronTrigger,包括Cron表达式和时区信息
QRTZ_FIRED_TRIGGERS 存储与已触发的Trigger相关的状态信息,以及相联Job的执行信息
QRTZ_PAUSED_TRIGGER_GRPS 存储已暂停的Trigger组的信息
QRTZ_SCHEDULER_STATE 存储少量的有关Scheduler的状态信息,和别的Scheduler实例
QRTZ_LOCKS 存储程序的悲观锁的信息
QRTZ_JOB_DETAILS 存储每一个已配置的Job的详细信息
QRTZ_SIMPLE_TRIGGERS 存储简单的Trigger,包括重复次数、间隔、以及已触的次数
QRTZ_BLOG_TRIGGERS Trigger作为Blob类型存储
QRTZ_TRIGGERS 存储已配置的Trigger的信息

 

目前CUBE系统作业监控只是用到了上述加粗部分的表,其余表没有用到

 

Quartz的执行逻辑如下:

 



 

Quartz 任务调度的核心元素是 scheduler, trigger 和 job,其中 trigger 和 job 是任务调度的元数据, scheduler 是实际执行调度的控制器。

 

    Quartz 中,trigger 是用于定义调度时间的元素,即按照什么时间规则去执行任务。Quartz 中主要提供了四种类型的 trigger:SimpleTrigger,CronTirgger,DateIntervalTrigger,和 NthIncludedDayTrigger。这四种 trigger 可以满足企业应用中的绝大部分需求。我们将在企业应用一节中进一步讨论四种 trigger 的功能。

 

        Quartz 中,job 用于表示被调度的任务。主要有两种类型的 job:无状态的(stateless)和有状态的

(stateful)。对于同一个 trigger 来说,有状态的 job 不能被并行执行,只有上一次触发的任务被执行完之后,才能触发下一次执行。Job 主要有两种属性:volatility 和 durability,其中 volatility 表示任务是否被持久化到数据库存储,而 durability 表示在没有 trigger 关联的时候任务是否被保留。两者都是在值为 true 的时候任务被持久化或保留。一个 job 可以被多个 trigger 关联,但是一个 trigger 只能关联一个 job。

 

     Quartz 中, scheduler 由 scheduler 工厂创建:DirectSchedulerFactory 或者 StdSchedulerFactory。 第二种工厂 StdSchedulerFactory 使用较多,因为 DirectSchedulerFactory 使用起来不够方便,需要作许多详细的手工编码设置。 Scheduler 主要有三种:RemoteMBeanScheduler, RemoteScheduler 和 StdScheduler。CUBE以最常用的 StdScheduler 实现。

 

说了很多Quartz的理论,就不在多说了,自己目前也就理解到这里了,下面是Quartz监控的代码

 

applicationContext-config.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:annotation-config />
	<context:component-scan base-package="com.cube.*" />
	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceUnitName" value="cube"></property>
		<!-- value 对应persistence.xml中的 persistence-unit name -->
	</bean>

	<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory"  />
	</bean>

	<!-- login action -->


	<tx:annotation-driven transaction-manager="txManager" />

</beans>

 

 

 applicationContext-quartz.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


	<bean name="quartzScheduler"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
		<property name="configLocation" value="classpath:quartz.properties" />
	</bean>

	<bean name="jobDetail"
		class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
		
		<property name="jobClass">
			<value>com.cube.service.quartz.QuartzJobService</value>
		</property>
		<property name="durability" value="true" />
	</bean>

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/cube?useUnicode=true&amp;characterEncoding=UTF8" />
		<property name="user" value="root" />
		<property name="password" value="" />
		<property name="initialPoolSize" value="10" />
		<property name="minPoolSize" value="10" />
		<property name="maxPoolSize" value="25" />
		<property name="acquireIncrement" value="5" />
		<property name="maxIdleTime" value="7200" />
	</bean>

</beans>

quartz.properties

 

org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true

org.quartz.jobStore.misfireThreshold = 60000

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_  
org.quartz.jobStore.isClustered = false  
org.quartz.jobStore.maxMisfiresToHandleAtATime=1 

 

Schedule调度器Service

public interface SchedulerService {

    
    public List<Map<String, Object>> getQrtzTriggers();
    
    

    /**
     * 根据 Quartz Cron Expression 调试任务
     * 
     * @param name Quartz CronTrigger名称
     * @param group Quartz CronTrigger组
     * @param cronExpression Quartz Cron 表达式,如 "0/10 * * ? * * *"等
     */
    void schedule(String name, String group, String cronExpression);


    /**
     * 根据 Quartz Cron Expression 调试任务
     * 
     * @param name Quartz CronTrigger名称
     * @param group Quartz CronTrigger组
     * @param cronExpression Quartz CronExpression
     */
    void schedule(String name, String group, CronExpression cronExpression);
    
    
    /**
     * 
     * @param name Quartz CronTrigger名称
     * @param group Quartz CronTrigger组
     * @param cronExpression Quartz CronExpression
     */
    void schedule(String name, String group);



    void schedule(Map<String, Object> map);

    /**
     * 暂停触发器
     * 
     * @param triggerName 触发器名称
     */
    void pauseTrigger(String triggerName);

    /**
     * 暂停触发器
     * 
     * @param triggerName 触发器名称
     * @param group 触发器组
     */
    void pauseTrigger(String triggerName, String group);

    /**
     * 恢复触发器
     * 
     * @param triggerName 触发器名称
     */
    void resumeTrigger(String triggerName);

    /**
     * 恢复触发器
     * 
     * @param triggerName 触发器名称
     * @param group 触发器组
     */
    void resumeTrigger(String triggerName, String group);

    /**
     * 删除触发器
     * 
     * @param triggerName 触发器名称
     * @return
     */
    boolean removeTrigdger(String triggerName);

    /**
     * 删除触发器
     * 
     * @param triggerName 触发器名称
     * @param group 触发器组
     * @return
     */
    boolean removeTrigdger(String triggerName, String group);


}

 

@Transactional
@Service("schedulerService")
public class SchedulerServiceImpl implements SchedulerService {

    @Resource(name = "quartzDao")
    private QuartzDao quartzDao;

    @Autowired
    private Scheduler scheduler;
    @Autowired
    private JobDetail jobDetail;

    private static final String NULLSTRING = null;
    private static final Date NULLDATE = null;

    /**
     * 
     * @param name
     * @param group
     * @param cronExpression
     * @see com.cube.service.SchedulerService#schedule(java.lang.String, java.lang.String,
     *      java.lang.String)
     */
    public void schedule(String name, String group, String cronExpression) {

        try {
            schedule(name, group, new CronExpression(cronExpression));

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    /**
     * @param name
     * @param group
     * @param cronExpression
     * @see com.cube.service.SchedulerService#schedule(java.lang.String, java.lang.String,
     *      org.quartz.CronExpression)
     */
    public void schedule(String name, String group, CronExpression cronExpression) {

        if (name == null || name.trim().equals("")) {
            name = UUID.randomUUID().toString();
        }

        CronTriggerImpl trigger = new CronTriggerImpl();
        trigger.setCronExpression(cronExpression);

        TriggerKey triggerKey = new TriggerKey(name, group);

        trigger.setJobName(jobDetail.getKey().getName());
        trigger.setKey(triggerKey);

        try {
            scheduler.addJob(jobDetail, true);
            if (scheduler.checkExists(triggerKey)) {
                scheduler.rescheduleJob(triggerKey, trigger);
            } else {
                scheduler.scheduleJob(trigger);
            }
        } catch (SchedulerException e) {
            throw new IllegalArgumentException(e);
        }

    }

    /**
     * @param map
     * @see com.cube.service.SchedulerService#schedule(java.util.Map)
     */
    public void schedule(Map<String, Object> map) {
        // TODO Auto-generated method stub

    }

    /**
     * @param triggerName
     * @see com.cube.service.SchedulerService#pauseTrigger(java.lang.String)
     */
    public void pauseTrigger(String triggerName) {
        // TODO Auto-generated method stub

    }

    /**
     * @param triggerName
     * @param group
     * @see com.cube.service.SchedulerService#pauseTrigger(java.lang.String, java.lang.String)
     */
    public void pauseTrigger(String triggerName, String group) {

        try {
            scheduler.pauseTrigger(new TriggerKey(triggerName, group));
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param triggerName
     * @see com.cube.service.SchedulerService#resumeTrigger(java.lang.String)
     */
    public void resumeTrigger(String triggerName) {
        // TODO Auto-generated method stub

    }

    /**
     * @param triggerName
     * @param group
     * @see com.cube.service.SchedulerService#resumeTrigger(java.lang.String, java.lang.String)
     */
    public void resumeTrigger(String triggerName, String group) {

        TriggerKey triggerKey = new TriggerKey(triggerName, group);

        try {
            scheduler.resumeTrigger(triggerKey);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param triggerName
     * @return
     * @see com.cube.service.SchedulerService#removeTrigdger(java.lang.String)
     */
    public boolean removeTrigdger(String triggerName) {

        return removeTrigdger(triggerName, NULLSTRING);

    }

    /**
     * @param triggerName
     * @param group
     * @return
     * @see com.cube.service.SchedulerService#removeTrigdger(java.lang.String, java.lang.String)
     */
    public boolean removeTrigdger(String triggerName, String group) {

        TriggerKey triggerKey = new TriggerKey(triggerName, group);
        try {
            scheduler.pauseTrigger(triggerKey);// 停止触发器
            return scheduler.unscheduleJob(triggerKey);// 移除触发器
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @return
     * @see com.cube.service.SchedulerService#getQrtzTriggers()
     */
    public List<Map<String, Object>> getQrtzTriggers() {
        return quartzDao.getQrtzTriggers();
    }

    /**
     * @param name
     * @param group
     * @see com.cube.service.SchedulerService#schedule(java.lang.String, java.lang.String)
     */
    public void schedule(String name, String group) {

        schedule(name, group, NULLSTRING);
    }

}

 

QuartzDao,主要负责查询触发器的状态信息

 

@Repository("quartzDao")
public class QuartzDao {

    private DataSource dataSource;

    @Autowired
    public void setDataSource(@Qualifier("dataSource") DataSource dataSource) {
        this.dataSource = dataSource;
    }

    // 查询Trigger
    public List<Map<String, Object>> getQrtzTriggers() {
        List<Map<String, Object>> results = getJdbcTemplate().queryForList(
                "select * from QRTZ_TRIGGERS order by start_time");
        long val = 0;
        String temp = null;
        for (Map<String, Object> map : results) {
            temp = MapUtils.getString(map, "trigger_name");
            if (StringUtils.indexOf(temp, "&") != -1) {
                map.put("display_name", StringUtils.substringBefore(temp, "&"));
            } else {
                map.put("display_name", temp);
            }

            val = MapUtils.getLongValue(map, "next_fire_time");
            if (val > 0) {
                map.put("next_fire_time", DateFormatUtils.format(val, "yyyy-MM-dd HH:mm:ss"));
            }

            val = MapUtils.getLongValue(map, "prev_fire_time");
            if (val > 0) {
                map.put("prev_fire_time", DateFormatUtils.format(val, "yyyy-MM-dd HH:mm:ss"));
            }

            val = MapUtils.getLongValue(map, "start_time");
            if (val > 0) {
                map.put("start_time", DateFormatUtils.format(val, "yyyy-MM-dd HH:mm:ss"));
            }

            val = MapUtils.getLongValue(map, "end_time");
            if (val > 0) {
                map.put("end_time", DateFormatUtils.format(val, "yyyy-MM-dd HH:mm:ss"));
            }

            map.put("trigger_state", Constant.status.get(MapUtils.getString(map, "trigger_state")));
        }

        return results;
    }

    private JdbcTemplate getJdbcTemplate() {
        return new JdbcTemplate(this.dataSource);
    }
}

 

JobClass

 

public class QuartzJobService extends QuartzJobBean {

    // 负责所有任务的调度
    private TaskService taskService;

    /**
     * @param context
     * @throws JobExecutionException
     * @see org.springframework.scheduling.quartz.QuartzJobBean#executeInternal(org.quartz.JobExecutionContext)
     */
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {

        String triggerName = context.getTrigger().getKey().getName();

        taskService = (TaskService) getApplicationContext(context).getBean("taskService");

        taskService.execute(triggerName);

    }

    private ApplicationContext getApplicationContext(final JobExecutionContext jobexecutioncontext) {
        try {
            return (ApplicationContext) jobexecutioncontext.getScheduler().getContext()
                    .get("applicationContextKey");
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

}

 

TaskService,负责所有任务的调用

 

public interface TaskService {


    /**
     * @param triggerName
     */
    public void execute(String triggerName);
}

 

@Transactional
@Service("taskService")
public class TaskServiceImpl implements TaskService {

    //此处注入自己业务的Service
    @Resource(name = "reportService")
    private ReportService reportService;

    /**
     * 根据TriggerName 调用不同的业务逻辑service
     * 
     * @param triggerName
     * @see com.cube.service.TaskService#execute(java.lang.String)
     */
    public void execute(String triggerName) {

        //此处根据触发器名称调用相应的业务逻辑Service
        if ("reportTigger".equalsIgnoreCase(triggerName)) {

            reportService.createReport();
        } else {
            System.out.println(triggerName + ":企业业务逻辑");
        }

    }

}

 

下面是Action类的实现,负责添加、删除、暂停、恢复Trigger

 

@Controller
@Scope("prototype")
public class TriggerAction extends BaseAction<TriggerEntity> {

    /**  */
    private static final long serialVersionUID = -3326354633384499660L;

    private TriggerEntity triggerEntity = getModel();

    private Map jsonMap = new HashMap();

    @Resource(name = "schedulerService")
    private SchedulerService schedulerService;

    /**
     * 跳转到tigger 管理界面
     * 
     * @return
     */
    public String trigger() {

        return "trigger";
    }

    /**
     * 分页查询Trigger
     * 
     * @return
     */
    public String list() {

        List<Map<String, Object>> list = schedulerService.getQrtzTriggers();

        jsonMap.put("rows", list);
        jsonMap.put("total", list.size());
        return "list";
    }

    /**
     * 添加触发器
     * 
     * @return
     */
    public String save() {

        // 获取界面以参数
        String triggerName = triggerEntity.getTrigger_name();
        String cronExpression = triggerEntity.getCron();
        String group = triggerEntity.getTrigger_group();

        schedulerService.schedule(triggerName, group, cronExpression);

        jsonMap.put("flag", true);

        return "save";
    }

    /**
     * 暂停
     * 
     * @return
     */
    public String pause() {

        schedulerService.pauseTrigger(triggerEntity.getTrigger_name(),
                triggerEntity.getTrigger_group());

        jsonMap.put("flag", true);

        return "pause";
    }

    /**
     * Trigger恢复
     * 
     * @return
     */
    public String play() {
        schedulerService.resumeTrigger(triggerEntity.getTrigger_name(),
                triggerEntity.getTrigger_group());
        
        jsonMap.put("flag", true);
        return "play";
    }

    /**
     * 删除
     * 
     * @return
     */
    public String deleteTrigger() {

        schedulerService.removeTrigdger(triggerEntity.getTrigger_name(),
                triggerEntity.getTrigger_group());

        jsonMap.put("flag", true);

        return "deleteTrigger";
    }

    public Map getJsonMap() {
        return jsonMap;
    }

    public void setJsonMap(Map jsonMap) {
        this.jsonMap = jsonMap;
    }

}

 

到此,作业监控功能就实现了,当然实现方式不止这一种,希望有别的实现大家可以共享一下吧,互相学习!!

下载地址:http://download.csdn.net/detail/zhouhua0104/9611192

 

  • 大小: 17.5 KB
  • 大小: 24.7 KB
  • 大小: 31.6 KB
  • 大小: 44 KB
  • 大小: 103.7 KB
  • 大小: 33.9 KB
分享到:
评论
19 楼 t153guozw 2016-11-21  
楼主你写的真好,能发我一份学习下吗,邮箱number1is_gzw@163.com 你复制发一份给我,最好sql 包含sql
18 楼 ximenpiaohua 2016-10-09  
js报错啊,plugins没有上传完吧
17 楼 wangxin_cool 2016-09-08  
楼主你好,方便给一份表结构吗,及初始化数据,infowangxin@139.com
16 楼 FireZHFox 2016-08-25  
韩非子 写道
求共享:908716835@qq.com

目前项目源码已经上传到如下地址,你可以自行下载:http://download.csdn.net/detail/zhouhua0104/9611192
15 楼 韩非子 2016-08-24  
求共享:908716835@qq.com
14 楼 FireZHFox 2016-08-24  
FireZHFox 写道
zmwxiaoming 写道
大神,我希望学习一下,能发一份代码给我邮箱么,谢谢.邮箱:563652043@qq.com

邮箱出点问题,晚点发给你

你可以做到这个地址下载:http://download.csdn.net/detail/zhouhua0104/9611192
13 楼 FireZHFox 2016-08-24  
lovexfl 写道
你好,麻烦给我发一份代码744189419@qq.com

邮箱出问题了,你可以到这个地址下载:http://download.csdn.net/detail/zhouhua0104/9611192
12 楼 lovexfl 2016-08-23  
你好,麻烦给我发一份代码744189419@qq.com
11 楼 FireZHFox 2016-08-22  
zmwxiaoming 写道
大神,我希望学习一下,能发一份代码给我邮箱么,谢谢.邮箱:563652043@qq.com

邮箱出点问题,晚点发给你
10 楼 zmwxiaoming 2016-08-22  
大神,我希望学习一下,能发一份代码给我邮箱么,谢谢.邮箱:563652043@qq.com
9 楼 hyd_smile 2016-08-20  
博主,你好,能发一份吗?1336631837@qq.com  十分感谢
8 楼 FireZHFox 2016-08-15  
吃肉_要吃回锅肉 写道
哪里可以下载呢?求分享:QQ:1392585186

文件有点大,没有上传,发你qq邮箱吧
7 楼 吃肉_要吃回锅肉 2016-08-14  
哪里可以下载呢?求分享:QQ:1392585186
6 楼 enjoyxing 2016-08-04  
好文章,目前正在研究,方便发我一份吗,感激不禁,谢谢!enjoyxing@126.com
5 楼 1324673194_e384ea 2016-08-03  
层主你写的真好,能发我一份学习下吗,邮箱1324673194@qq.com
4 楼 FireZHFox 2016-08-02  
tixiang 写道
那里可以下载

最近实在太忙没有及时回复,这个代码由于有点大没有上传,如果需要你可以留下邮箱,我发给你
3 楼 FireZHFox 2016-08-02  
Susan_0115 写道
用到集群吗?在项目中,集群环境下,任务同时运行怎么解决?

这个就是用到集群环境中的,任务同时运行有两种解决方式,一种就是通过JNDI的方式解决,一种就是通过配置定时作业是在配置文件中配置启用集群的标识进行解决,quartz会通过数据库的行锁解决
2 楼 Susan_0115 2016-07-05  
用到集群吗?在项目中,集群环境下,任务同时运行怎么解决?
1 楼 tixiang 2016-06-16  
那里可以下载

相关推荐

Global site tag (gtag.js) - Google Analytics