com.us.listener.ScheduleInitListener.java Source code

Java tutorial

Introduction

Here is the source code for com.us.listener.ScheduleInitListener.java

Source

/*
 * @(#)ScheduleInitListener.java 2011-10-17
 * ?us??  2008-2011, Inc. All rights reserved.
 * s.server. Use is subject to license terms.
 */

package com.us.listener;

import java.util.HashSet;
import java.util.Set;

import lombok.Setter;

import org.apache.commons.logging.Log;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import com.us.config.App;
import com.us.interfaces.jobs.UsJob;
import com.us.util.ClassUtil;
import com.us.util.LogHelper;

/**
 * Spring???
 * 
 * @author <a href="mailto:monlyu.hong@gmail.com">monlyu</a>
 * @version --0.0.0--, 2011-10-17
 * @since JDK6.0
 */
public class ScheduleInitListener extends SchedulerFactoryBean {

    @Setter
    private Set<String> scanList = new HashSet<String>();

    public static Scheduler SCHEDULER = null;

    Log log = LogHelper.getLog();// log

    public void doWork() {
        log.info("?");
        Set<String> paks = getPackgeList();
        log.info("?" + paks);
        try {
            SCHEDULER = StdSchedulerFactory.getDefaultScheduler();
            for (String pak : paks) {
                Set<Class<?>> allClass = ClassUtil.getClassesByPackage(pak, true);
                for (Class<?> clazz : allClass) {
                    try {
                        final Object jobInstance = clazz.newInstance();
                        if (jobInstance instanceof UsJob) {
                            UsJob job = (UsJob) jobInstance;
                            if (!job.isDisable()) {
                                Trigger trigger = job.initTrigger();
                                if (trigger.getName() == null) {
                                    trigger.setName(jobInstance.getClass().getName());
                                }
                                log.info(String.format("Class[%s],Trigger:[%s]",
                                        jobInstance.getClass().getName(), trigger.getName()));
                                SCHEDULER.scheduleJob(job.makeJob(), trigger);
                            }
                        }
                    } catch (InstantiationException e) {
                        log.error("??" + e.getMessage());
                    } catch (IllegalAccessException e) {
                        log.error("??" + e.getMessage());
                    }
                }
            }
            SCHEDULER.start();
            log.info("??");
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    private Set<String> getPackgeList() {
        scanList.add(App.SCHEDULE_PACKAGE);
        return scanList;
    }
}