Quartz 调度程序未在战争中运行

发布于 2024-12-22 00:05:26 字数 1692 浏览 3 评论 0原文

我在 gwt Web 应用程序中使用quartz 调度程序。我的应用程序结构如下所示。

我有两个项目。一个是 gwt Web 应用程序客户端项目(用于 ui 部分)&另一个是用于服务器端调用的java项目。(用于数据库交互)。在客户端项目中,我放置了一个服务器项目作为参考。运行时,我从客户端创建一个war,并将服务器项目的jar添加到war文件夹中。

现在我在服务器端使用调度程序来自动完成某些任务。在本地运行它(没有战争)调度程序正常工作。

但是在 jboss 服务器上运行 war 时,调度程序没有运行。

我所有与调度程序相关的代码和 Quartz.jar 都在服务器端。客户端项目中没有任何 Quartz 的引用。这是问题吗???

这是我的调度程序代码

public class QuartzJob implements Job {

public void execute(JobExecutionContext jobExecutionContext)
        throws JobExecutionException {
    JobDataMap map = jobExecutionContext.getJobDetail().getJobDataMap();
    ActivityTransactionSettingsMap map2 = (ActivityTransactionSettingsMap) map
            .get("task");

    if (map2.getAutoCompleteDate() != null) {
        WorkFlowFacade facade = new WorkFlowFacade();
        facade.completeAutoCompleteTask(map2);
        Scheduler scheduler=(Scheduler) map.get("scheduler");
        try {
            scheduler.shutdown();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}

}

Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
    scheduler.start();
    JobDataMap map2 = new JobDataMap();
    map2.put("task", actsMap);
    map2.put("scheduler", scheduler);
    JobDetail job = newJob(QuartzJob.class).withIdentity("job"+String.valueOf(actsMap.getId()))
            .usingJobData(map2).build();
    Trigger trigger = newTrigger().withIdentity("trigger"+String.valueOf(actsMap.getId()))
            .startAt(actsMap.getAutoCompleteDate()).build();
    scheduler.scheduleJob(job, trigger);

或者我是否需要仅在客户端转移我的调度程序相关项目? 我不知道如何解决这个问题。 请帮帮我

I am using quartz scheduler in gwt web application.My application structure is like below.

I have two project.One is gwt web application client project(for ui part) & other is java project for server side call.(for database interaction).In client project I put a server project for reference.While running I create a war from client and add a jar of server project to war folder.

Now I used scheduler at server side for some task to auto complete.While running it locally (with out war) scheduler working properly.

But while running war at jboss server scheduler is not running.

My all scheduler related code and Quartz.jar is at server side.There is no any reference of Quartz in client side project.is this the problem???

Here is my code for scheduler

public class QuartzJob implements Job {

public void execute(JobExecutionContext jobExecutionContext)
        throws JobExecutionException {
    JobDataMap map = jobExecutionContext.getJobDetail().getJobDataMap();
    ActivityTransactionSettingsMap map2 = (ActivityTransactionSettingsMap) map
            .get("task");

    if (map2.getAutoCompleteDate() != null) {
        WorkFlowFacade facade = new WorkFlowFacade();
        facade.completeAutoCompleteTask(map2);
        Scheduler scheduler=(Scheduler) map.get("scheduler");
        try {
            scheduler.shutdown();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}

}

Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
    scheduler.start();
    JobDataMap map2 = new JobDataMap();
    map2.put("task", actsMap);
    map2.put("scheduler", scheduler);
    JobDetail job = newJob(QuartzJob.class).withIdentity("job"+String.valueOf(actsMap.getId()))
            .usingJobData(map2).build();
    Trigger trigger = newTrigger().withIdentity("trigger"+String.valueOf(actsMap.getId()))
            .startAt(actsMap.getAutoCompleteDate()).build();
    scheduler.scheduleJob(job, trigger);

Or do I need to shift my scheduler related project at client side only??
I am not getting how to solve this.
Please help me out

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ˉ厌 2024-12-29 00:05:26

为了让调度程序运行,应该有一些东西来启动它。我不确定这个过程是如何发生的,但您可以在 servlet

public class MySchedulerServlet extends GenericServlet {

private static final long serialVersionUID = 1477091380142883153L;

/**
 * Constant to represent property for the cron expression.
 */
private static final String CRON_EXPRESSION = "0 0 0 ? * SUN";

public void init(ServletConfig servletConfig) throws ServletException {

    super.init(servletConfig);

    // The Quartz Scheduler
    Scheduler scheduler = null;

    try {

        // Initiate a Schedule Factory
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        // Retrieve a scheduler from schedule factory
        scheduler = schedulerFactory.getScheduler();
        // Initiate JobDetail with job name, job group and
        // executable job class
        JobDetail jobDetail = new JobDetail("RetryJob", "RetryGroup", QuartzJob.class);
        // Initiate CronTrigger with its name and group name
        CronTrigger cronTrigger = new CronTrigger("cronTrigger",
                "triggerGroup");
        // setup CronExpression
        CronExpression cexp = new CronExpression(CRON_EXPRESSION);
        // Assign the CronExpression to CronTrigger
        cronTrigger.setCronExpression(cexp);
        // schedule a job with JobDetail and Trigger
        scheduler.scheduleJob(jobDetail, cronTrigger);

        // start the scheduler
        scheduler.start();

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

}

public void service(ServletRequest serveletRequest,
        ServletResponse servletResponse) throws ServletException, IOException {

}

}

和启动时的 web.xml 加载调度程序中编写此调度程序。这对我有用。

       <servlet>
    <servlet-name>QuartzInitializer</servlet-name>
    <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
    <init-param>
        <param-name>shutdown-on-unload</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>start-scheduler-on-load</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

        <servlet-name>MySchedulerServlet </servlet-name>
        <servlet-class>com.servlet.MySchedulerServlet </servlet-class>
    <load-on-startup>2</load-on-startup>

For the scheduler to run , there should be something to kick start it . I am not sure how the process is happening but you could write this scheduler in a servlet

public class MySchedulerServlet extends GenericServlet {

private static final long serialVersionUID = 1477091380142883153L;

/**
 * Constant to represent property for the cron expression.
 */
private static final String CRON_EXPRESSION = "0 0 0 ? * SUN";

public void init(ServletConfig servletConfig) throws ServletException {

    super.init(servletConfig);

    // The Quartz Scheduler
    Scheduler scheduler = null;

    try {

        // Initiate a Schedule Factory
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        // Retrieve a scheduler from schedule factory
        scheduler = schedulerFactory.getScheduler();
        // Initiate JobDetail with job name, job group and
        // executable job class
        JobDetail jobDetail = new JobDetail("RetryJob", "RetryGroup", QuartzJob.class);
        // Initiate CronTrigger with its name and group name
        CronTrigger cronTrigger = new CronTrigger("cronTrigger",
                "triggerGroup");
        // setup CronExpression
        CronExpression cexp = new CronExpression(CRON_EXPRESSION);
        // Assign the CronExpression to CronTrigger
        cronTrigger.setCronExpression(cexp);
        // schedule a job with JobDetail and Trigger
        scheduler.scheduleJob(jobDetail, cronTrigger);

        // start the scheduler
        scheduler.start();

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

}

public void service(ServletRequest serveletRequest,
        ServletResponse servletResponse) throws ServletException, IOException {

}

}

and in your web.xml load sceduler on startup. This works for me.

       <servlet>
    <servlet-name>QuartzInitializer</servlet-name>
    <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
    <init-param>
        <param-name>shutdown-on-unload</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>start-scheduler-on-load</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

        <servlet-name>MySchedulerServlet </servlet-name>
        <servlet-class>com.servlet.MySchedulerServlet </servlet-class>
    <load-on-startup>2</load-on-startup>
去了角落 2024-12-29 00:05:26

在这种情况下我使用了线程。

public class AutoCompleteTaskThread extends Thread {

private ActivityTransactionSettingsMap taskMap;

public AutoCompleteTaskThread(ActivityTransactionSettingsMap map) {
    this.taskMap = map;
}
@Override
public void run() {
    try {
        new AutoCompleteTaskScheduler().ScheduleJob(taskMap);
    } catch (Exception e) {          
        e.printStackTrace();
    }
}

如下

所示,在本地计算机中它工作正常。但对于 jboss 服务器它不工作。

I used thread in this case.

public class AutoCompleteTaskThread extends Thread {

private ActivityTransactionSettingsMap taskMap;

public AutoCompleteTaskThread(ActivityTransactionSettingsMap map) {
    this.taskMap = map;
}
@Override
public void run() {
    try {
        new AutoCompleteTaskScheduler().ScheduleJob(taskMap);
    } catch (Exception e) {          
        e.printStackTrace();
    }
}

}

As below and in local machine it is working fine.but for jboss server it is not working.

守不住的情 2024-12-29 00:05:26

我使用了 AutoCompleteTaskThread 类的实例并调用了它的 start 方法。

    private void addAutoCompleteTask(ActivityTransactionSettingsMap newTask) {
    AutoCompleteTaskThread thread = new AutoCompleteTaskThread(newTask);
    thread.start();
}

在这里,我为任何我想要启动新线程的新任务启动了一个线程。

I used an instance of AutoCompleteTaskThread class and called start method on this.

    private void addAutoCompleteTask(ActivityTransactionSettingsMap newTask) {
    AutoCompleteTaskThread thread = new AutoCompleteTaskThread(newTask);
    thread.start();
}

Here for I started a thread for any new task for which I want to start new thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文