如何安排任务每隔“N”发生一次Spring框架中的秒数

发布于 2024-12-20 18:06:03 字数 314 浏览 1 评论 0 原文

我如何连接我的 web.xml 以使任务每 n 秒发生一次。此外,我需要通过方法调用每 5 秒刷新一次服务器方法。

非常感谢提前

解决:

http://javaprogrammingtips4u .blogspot.com/2010/05/how-to-implement-task-scheduler-job.html

How do I wire up my web.xml to have a task happen every n many seconds. Moreover, I need to have a server method refresh every 5 seconds via a method call.

Much thanks in advance

SOLVED:

http://javaprogrammingtips4u.blogspot.com/2010/05/how-to-implement-task-scheduler-job.html

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

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

发布评论

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

评论(5

一杯敬自由 2024-12-27 18:06:03

您可以使用注释所需的例程

public class Foo {

    @Scheduled(fixedDelay=5000)
    public void Bar() {
       // ...
    }
}

,但为了让 Spring 查找并识别注释,您必须声明类 Foo 所在的基包,并配置 Spring 来查找调度任务。将以下内容放入 Spring XML 配置中(并且不要忘记导入 XML 命名空间 contexttask)。

<context:component-scan base-package="com.company.scheduling"/>
<task:annotation-driven />

或者,您可以将 @EnableScheduling 放在类声明之前,它会立即为您执行 XML 配置。

另请参见 上下文命名空间任务命名空间

You can annotate the desired routine using

public class Foo {

    @Scheduled(fixedDelay=5000)
    public void Bar() {
       // ...
    }
}

But in order to for Spring for find and recognize the annotation you must declare which base package the class Foo lies in as well as configure Spring to look for scheduling tasks. Put the following in your spring XML configuration (and don't forget to import the XML namespaces contextand task).

<context:component-scan base-package="com.company.scheduling"/>
<task:annotation-driven />

Alternatively you can put @EnableScheduling before the class declaration and it does the XML config for you out of the box.

See also the context namespace and the task namespace.

我恋#小黄人 2024-12-27 18:06:03

您可以使用 Quartz 调度程序集成,如下所述:
http://static.springsource.org/spring/docs/2.0 .8/reference/scheduling.html

我不明白您关于刷新服务器方法的问题的第二部分。

You can use the Quartz scheduler integration as documented here:
http://static.springsource.org/spring/docs/2.0.8/reference/scheduling.html

I do not understand the 2nd part of your question about refreshing a server method.

南笙 2024-12-27 18:06:03

如何使用石英和弹簧。通过拆分作业、触发器和计时器,这是高度可配置的。您还可以使用 cron 表达式并集成 JMX 之类的东西:

Spring 和 Quartz

How abt using Quartz with spring. This is highly configurable by spliting Jobs, triggers and timers up. You can also use cron expressions and integrate things like JMX:

Spring and Quartz

往日情怀 2024-12-27 18:06:03

查看 Spring 手册,第 25 节, 任务执行和调度。当我完成这类事情时,我使用了注释,如 第 25.5 节。简而言之,在 Spring 管理的 bean 中,您可以使用 @Scheduled 注释要运行的方法,并向 applicationContext.xml 添加几行。

Look at the Spring manual, section 25, Task Execution and Scheduling. When I've done this sort of thing I used annotations, described in section 25.5. In short, in a Spring-managed bean you annotate the method you want to run with @Scheduled and add a couple of lines to your applicationContext.xml.

掩于岁月 2024-12-27 18:06:03

该表达式将在工作日每 5 秒运行一次:

@Scheduled(cron = "*/5 * * * * MON-FRI")
    public void updateAllStatements() {
...........
}

要检查 cron 表达式,我使用以下方法:

/**
     * Checks provided Cron expression
     *
     * @param cronExpression
     */
    public static void checkCronExpression(final String cronExpression) {
        try {
            Date now = new Date();

            System.out.println(now);

            final CronSequenceGenerator gen = new CronSequenceGenerator(cronExpression);

            for (int i = 0; i < 5; i++) {
                now = gen.next(now);

                System.out.println(now);
            }
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }

This expression will run every 5 seconds in working days:

@Scheduled(cron = "*/5 * * * * MON-FRI")
    public void updateAllStatements() {
...........
}

To check cron expression I'm using this method:

/**
     * Checks provided Cron expression
     *
     * @param cronExpression
     */
    public static void checkCronExpression(final String cronExpression) {
        try {
            Date now = new Date();

            System.out.println(now);

            final CronSequenceGenerator gen = new CronSequenceGenerator(cronExpression);

            for (int i = 0; i < 5; i++) {
                now = gen.next(now);

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