Spring的监听器和org.quartz.Job的使用

JavaWeb工程中配置监听器

一.在项目启动时就执行的监听器,使用ServletContextListener

1.监听类的书写

自定义的listener需要实现ServletContextListener 接口

package com.glite.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
import com.glite.thread.UDPSaveThread;
import com.glite.thread.UDPSocketThread;
public class UDPSocketListener implements ServletContextListener {
    private static final Logger logger = Logger.getLogger(UDPSocketListener.class);
    @Override
    public void contextDestroyed(ServletContextEvent event) {
        logger.info("-----------contextDestroyed---------");
    }
    @Override
    public void contextInitialized(ServletContextEvent event) {
        logger.info("-----------contextInitialized---------");
        UDPSocketThread socket = new UDPSocketThread();
        UDPSaveThread save = new UDPSaveThread();
        socket.start();
        save.start();
    }
}

2.web.xml的书写

注入自定义的listener和context-param

...

contextConfigLocation
    classpath:applicationContext.xml


    com.glite.listener.UDPSocketListener

...

二.在系统启动的监听器中配置org.quartz.Job

1. web.xml的书写

注入ContextLoaderListener

...
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
...

2.applicationContext.xml

<context:annotation-config />
    <context:component-scan base-package="cn.gilight,cn.gilight.listener">
context:component-scan>

<bean id="jobFactory" class="cn.gilight.task.MyJobFactory">bean>

<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="jobFactory" ref="jobFactory">property>
bean>

3.spring-mvc.xml

<context:annotation-config />
    <context:component-scan base-package="cn.gilight.business.**.controller">
context:component-scan>

4.具体类的关系图

Spring的监听器和org.quartz.Job的使用_第1张图片

5.具体类的书写

1.SystemStartListener类的书写

需要实现InitializingBean和ServletContextAware 接口

package cn.gilight.listener;
import javax.servlet.ServletContext;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ServletContextAware;
import cn.gilight.task.job.bean.Plan;
import cn.gilight.task.service.TaskService;
@Service
public class SystemStartListener implements InitializingBean, ServletContextAware {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(SystemStartListener.class);
    @Autowired
    private TaskService taskService;
    @Override
    public void setServletContext(ServletContext arg0) {
        logger.info("-------------------System starting--------------------");
        try {
            Plan plan = new Plan();
            plan.setId(1);
            plan.setName("onlineEquipment");
            plan.setGroup_("group");
            //plan.setCron_expression("00 30 12 * * ?");//每天12:30执行一次
            plan.setCron_expression("0 0 0/2 * * ?");//每隔2小时执行一次
            plan.setDesc_("Trigger time:Execute every two hours");
            plan.setIsTrue("1");
            plan.setJobClass("cn.gilight.task.job.impl.OnlineEquipmentPublicJob");
            try {
                logger.info("plan : " + plan);
                taskService.addJob(plan);
                logger.info("--Added student online device task successfully-----");
            } catch (Exception e) {
                logger.info("---Add student online device task exception-----");
                e.printStackTrace();
            }
        } catch (Exception e) {
            logger.info("-----Task initialization failed------");
            e.printStackTrace();
        }
    }
}

2.自定义的Plan类的书写

package cn.gilight.task.job.bean;
/**
 * job实体类
 */
public class Plan {
    private Integer id;
    private String name;
    private String group_;
    private String cron_expression;
    private String desc_;
    private String isTrue = "1";
    private String jobClass;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGroup_() {
        return group_;
    }
    public void setGroup_(String group_) {
        this.group_ = group_;
    }
    public String getCron_expression() {
        return cron_expression;
    }
    public void setCron_expression(String cron_expression) {
        this.cron_expression = cron_expression;
    }
    public String getDesc_() {
        return desc_;
    }
    public void setDesc_(String desc_) {
        this.desc_ = desc_;
    }
    public String getIsTrue() {
        return isTrue;
    }
    public void setIsTrue(String isTrue) {
        this.isTrue = isTrue;
    }
    public String getJobClass() {
        return jobClass;
    }
    public void setJobClass(String jobClass) {
        this.jobClass = jobClass;
    }
    @Override
    public String toString() {
        return "Plan [id=" + id + ", name=" + name + ", group_=" + group_ + ", cron_expression=" + cron_expression + ", desc_=" + desc_ + ", isTrue=" + isTrue + ", jobClass=" + jobClass + "]";
    }

}

3. TaskService 任务接口

package cn.gilight.task.service;
import org.quartz.SchedulerException;
import cn.gilight.task.job.bean.Plan;
/**
 * 任务接口
 */
public interface TaskService {
    /**
     * 创建或者更新任务
     * @param job
     * @throws SchedulerException
     * @throws ClassNotFoundException
     */
    public void updateOrInsert(Plan plan) throws Exception;
    /**
     * 创建任务
     * @param job
     * @throws SchedulerException
     * @throws ClassNotFoundException
     */
    public void addJob(Plan plan) throws Exception;
    /**
     * 更新任务
     * @param job
     * @throws SchedulerException
     * @throws ClassNotFoundException
     */
    public void updateOrInsertJob(Plan plan) throws Exception;
    /**
     * 暂停任务
     * @param job
     * @throws SchedulerException
     */
    public void stopJob(Plan plan) throws Exception;
    /**
     * 恢复任务
     * @param job
     * @throws SchedulerException
     */
    public void startJob(Plan plan) throws Exception;
    /**
     * 删除任务
     * @param job
     * @throws SchedulerException
     */
    public void delJob(Plan plan) throws Exception;
    /**
     * 运行计划JOB
     * @param plan
     * @throws SchedulerException
     */
    public void runJob(Plan plan) throws Exception;

}

4.TaskServiceImpl 任务实现类

package cn.gilight.task.service.impl;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
import cn.gilight.task.job.bean.Plan;
import cn.gilight.task.service.TaskService;
@Service(value = "taskService")
public class TaskServiceImpl implements TaskService {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;
    private Scheduler scheduler = null;
    public void setSchedulerFactoryBean(SchedulerFactoryBean schedulerFactoryBean) {
        this.schedulerFactoryBean = schedulerFactoryBean;
        scheduler = schedulerFactoryBean.getScheduler();
    }
    @Override
    public void updateOrInsert(Plan plan) throws Exception {
        // 这里获取任务信息数据
        TriggerKey triggerKey = TriggerKey.triggerKey(plan.getName(), plan.getGroup_());
        // 获取trigger,即在spring配置文件中定义的 bean id="myTrigger"
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        if (null == trigger) {
            addJob(plan);
        } else {
            updateOrInsertJob(plan);
        }
    }
    @Override
    public void updateOrInsertJob(Plan plan) throws Exception {
        // 采用的方式是:先删除后新增
        this.delJob(plan);
        this.addJob(plan);
    }
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public void addJob(Plan plan) throws Exception {
        // 初始化scheduler
        scheduler = schedulerFactoryBean.getScheduler();
        Class clazz = Class.forName(plan.getJobClass());
        JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(plan.getName(), plan.getGroup_()).build();
        jobDetail.getJobDataMap().put("planKey", plan);
        // 表达式调度构建器
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(plan.getCron_expression());
        // 按新的cronExpression表达式构建一个新的trigger
        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(plan.getName(), plan.getGroup_()).withSchedule(scheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, trigger);
        // job.getIsTrue()为0表示停止
        if ("0".equals(plan.getIsTrue())) {
            this.stopJob(plan);
        }
    }
    @Override
    public void stopJob(Plan plan) throws Exception {
        scheduler.pauseJob(new JobKey(plan.getName(), plan.getGroup_()));
    }
    @Override
    public void startJob(Plan plan) throws Exception {
        scheduler.resumeJob(new JobKey(plan.getName(), plan.getGroup_()));
    }
    @Override
    public void delJob(Plan plan) throws Exception {
        scheduler.deleteJob(new JobKey(plan.getName(), plan.getGroup_()));
    }
    @Override
    public void runJob(Plan plan) throws Exception {
        scheduler.triggerJob(new JobKey(plan.getName(), plan.getGroup_()));
    }
}

5.SpringBeanUtils

package cn.gilight.utils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringBeanUtils {
    private static ApplicationContext ac;
    static {
        ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    }
    public static  T getBean(Class clazz, String beanName) {
        return ac.getBean(clazz, beanName);
    }
}

6.PublicJob

package cn.gilight.task.job;
import org.apache.log4j.Logger;
import org.quartz.Job;
public abstract class PublicJob implements Job {
    protected static final Logger logger = Logger.getLogger(Job.class);
}

7. OnlineEquipmentPublicJob 具体任务类的书写

package cn.gilight.task.job.impl;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import cn.gilight.business.service.DataService;
import cn.gilight.task.job.PublicJob;
import cn.gilight.utils.SpringBeanUtils;
/**
 * 在线设备
 */
public class OnlineEquipmentPublicJob  extends PublicJob{
    private DataService dataService = SpringBeanUtils.getBean(DataService.class, "dataService");
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        executePhoenix();
    }
    public void executePhoenix() {
        dataService.saveOnlineEquipment();//业务层启动任务
    }
}

你可能感兴趣的:(Spring的监听器和org.quartz.Job的使用)