欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

chx 学习jForum笔记十九 jForum定时作业

程序员文章站 2022-05-29 19:38:47
...

参照jforum原有的两个定时作业SummaryScheduler.java和POPJobStarter.java新建UserSyncScheduler.java

 

修改ConfigKeys.java文件,新增以下语句

    public static final String USERSYNC_IS_ENABLED = "usersync.enabled";

 

修改SystemGlobals.properties文件,新增以下语句

usersync.enabled = true

 

修改quartz-forum.properties文件,新增以下语句:

# user sync 每天12点
org.quartz.context.usersync.cron.expression = 0 0 12 * * ?

 

仿照SummaryScheduler.java新建UserSyncSCheduler.java

package net.jforum.summary;

import net.jforum.util.preferences.ConfigKeys;
import net.jforum.util.preferences.SystemGlobals;
import org.apache.log4j.Logger;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import java.text.ParseException;


public final class UserSyncScheduler {
    private static Scheduler scheduler;
    private static final Logger LOGGER = Logger.getLogger(UserSyncScheduler.class);
    private static boolean isStarted = false;
    private static final Object MUTEX = new Object();

    /**
     * Starts the usersync Job. Conditions to start: Is not started yet and is enabled on the file
     * SystemGlobasl.properties. The to enable it is "usersync.enabled"
     * (ConfigKeys.USERSYNC_IS_ENABLED).
     *
     * @throws org.quartz.SchedulerException
     */
    public static void startJob() throws SchedulerException {
        final boolean isEnabled = SystemGlobals.getBoolValue(ConfigKeys.USERSYNC_IS_ENABLED);
        synchronized (MUTEX) {
            if (!isStarted && isEnabled) {
                final String filename = SystemGlobals.getValue(ConfigKeys.QUARTZ_CONFIG);
                final String cronExpression = SystemGlobals.getValue("org.quartz.context.usersync.cron.expression");
                scheduler = new StdSchedulerFactory(filename).getScheduler();
                Trigger trigger = null;  //触发器
                try {
                    trigger = new CronTrigger(UserSyncJob.class.getName(), "usersyncJob", cronExpression);
                    LOGGER.info("Starting quartz usersync expression " + cronExpression);
                    scheduler.scheduleJob(new JobDetail(UserSyncJob.class.getName(), "usersyncJob", UserSyncJob.class), trigger);
                    scheduler.start();
                }
                catch (ParseException e) {
                    LOGGER.error(e.getMessage(), e);
                }
            }
            isStarted = true;
        }
    }

    /**
     * Stops the usersync Job. Conditions to stop: Is started and is enabled on the file
     * SystemGlobasl.properties. The to enable it is "usersync.enabled"
     * (ConfigKeys.USERSYNC_IS_ENABLED).
     *
     * @throws org.quartz.SchedulerException
     */
    public static void stopJob() throws SchedulerException {
        final boolean isEnabled = SystemGlobals.getBoolValue(ConfigKeys.USERSYNC_IS_ENABLED);
        synchronized (MUTEX) {
            if (isStarted && isEnabled) {
                final String filename = SystemGlobals.getValue(ConfigKeys.QUARTZ_CONFIG);
                final String cronExpression = SystemGlobals.getValue("org.quartz.context.usersync.cron.expression");
                scheduler = new StdSchedulerFactory(filename).getScheduler();
                try {
                    LOGGER.info("Stopping quartz usersync expression " + cronExpression);
                    scheduler.shutdown();
                }
                catch (SchedulerException e) {
                    LOGGER.error(e.getMessage(), e);
                }
            }
            isStarted = false;
        }
    }

    private UserSyncScheduler() {
    }
}

 仿照SummaryJob.java新建 UserSyncJob.java

package net.jforum.summary;

import net.jforum.dao.DataAccessDriver;
import net.jforum.dao.GroupDAO;
import net.jforum.entities.Group;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.List;

public class UserSyncJob implements Job {
    private static final Logger LOGGER = Logger.getLogger(UserSyncJob.class);

    /**
     * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
     */
    public void execute(final JobExecutionContext context) throws JobExecutionException {
        final GroupDAO groupdao = DataAccessDriver.getInstance().newGroupDAO();
        try {
            groupdao.SynchronizationGroup(); //同步组
            final List<Group> groups = groupdao.selectAll();
            if (groups != null) {
                for (Group group : groups) { //同步组用户
                    groupdao.SynchronizationGroupUsers(group.getId());
                }
            }
        }
        catch (Exception e) {
            LOGGER.warn(e);
        }
    }
}

等着看效果了。

在ConfigLoader.java中新增

    public static void startUserSyncJob() throws SchedulerException
    {
        UserSyncScheduler.startJob();
    }
 

在JForum.java中的public void destroy()中新增

UserSyncScheduler.stopJob();

 

在JForumBaseServlet.java中的protected void startApplication()中新增

 ConfigLoader.startUserSyncJob();

 

修改quartz-forum.properties文件内容相当重要,原先使用的

org.quartz.context.usersync.cron.expression = 0 0 12 * * *

没有任何效果,改成

org.quartz.context.usersync.cron.expression = 0 0 12 * * ?

后,就成功了.