每30秒执行一次Java类的最简单方法是什么?

时间:2010-03-19 10:04:35

标签: java spring jboss spring-mvc quartz-scheduler

我一直在阅读有关java / spring / hibernate的内容并通过“虚拟”示例工作,所以我告诉我的朋友为我推荐一些更难的东西,现在我被卡住了......这是最简单的类我可以想到

package spring.com.practice;

public class Pitcher {

    private String shout;

    public String getShout() {
        return shout;
    }

    public void setShout(String shout) {
        this.shout = shout;
    }

    public void voice()
    {
        System.out.println(getShout());
    }

}

通过从spring bean调用metod voice()来打印出来的最简单的方法是什么,并且每隔30秒重复一次就可以说,这就是我到目前为止所做的:

                      

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
    <property name="jobDetail" ref="jobSchedulerDetail" />
    <property name="startDelay" value="0" />
    <property name="repeatInterval" value="30" />
</bean>


<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="schedulerName" value="pitcherScheduler" />
    <property name="triggers">
        <list>
            <ref bean="simpleTrigger" />
        </list>
    </property>
</bean>
 <bean id="pitcher" class="spring.com.practice.Pitcher">
 <property name="shout" value="I started executing..."></property>
 </bean>

是的,我正试图在Jboss 5上运行它,我正在用maven建立一个项目。

我收到了一些建议,现在我的申请背景如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:sched="http://www.springinaction.com/schema/sched"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springinaction.com/schema/sched
       http://www.springinaction.com/schema/sched-1.0.xsd"
       default-lazy-init="true">

   <bean id="stuffDoer" class="spring.com.practice">
   <property name="shout" value="I'm executing"/>
   </bean>

  <sched:timer-job
       target-bean="stuffDoer"
       target-method="voice"
       interval="5000" 
       start-delay="1000"
       repeat-count="10" />

</beans>

这是我的web.xml:

<web-app id="simple-webapp" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>spring app</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/conf/applicationContext.xml
</param-value>
    </context-param>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.properties</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.util.Log4jConfigListener
</listener-class>
    </listener>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
</listener-class>
    </listener>
</web-app>

现在我得到了这个例子:

12:35:51,657 ERROR [01-SNAPSHOT]] Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

我没有意识到每隔30秒执行一次像hello world这样的事情会是这么复杂的

5 个答案:

答案 0 :(得分:28)

我不打算使用Quartz,这对于这么简单的事情来说太过分了。 Java5带有自己的调度程序,它已经足够了。

春季前3,这是最简单的方法:

<bean class="org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean">
    <property name="scheduledExecutorTasks">
        <list>
            <bean class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">
                <property name="period" value="30000"/>
                <property name="runnable">
                    <bean class="org.springframework.scheduling.support.MethodInvokingRunnable">
                        <property name="targetObject" ref="pitcher"/>
                        <property name="targetMethod" value="voice"/>
                    </bean>
                </property>
            </bean>
        </list>
    </property>
</bean>

使用Spring 3,它可能非常简单:

@Scheduled(fixedRate=30000)
public void voice() {
    System.out.println(getShout());
}

<beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:task="http://www.springframework.org/schema/task"
           xsi:schemaLocation="
                http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
           "> 

  <bean id="pitcher" class="spring.com.practice.Pitcher">
     <property name="shout" value="I started executing..."></property>
  </bean>

  <task:annotation-driven/>

</beans>

答案 1 :(得分:2)

使用Spring 3,您可以快速使用@Scheduled@Async

检查出来:Implement @Scheduled in Spring Application

答案 2 :(得分:1)

看起来很复杂,但这确实是最好的方法。您可以在应用程序外部配置它,并让spring / quartz执行。

当您需要调用的方法是启用事务的服务调用时,这尤其有用。

答案 3 :(得分:1)

我有类似的东西,但在mule中使用QuartzConnector类,每20秒运行一次。见例子。另一种方法是使用cron类型的时间条目,参见Quartz Cron

    <endpoint name="poller" address="quartz://poller1" type="sender" connector="QuartzConnector">
      <properties>
        <property name="repeatInterval" value="20000"/>
        <property name="payloadClassName" value="org.jdom.Document" />
        <property name="startDelay" value="10000"/>                
      </properties>
    </endpoint>  

答案 4 :(得分:0)

您可以使用预定任务

<bean id="EmptyScopesRemover" class="com.atypon.pagebuilder.core.tasks.impl.EmptyScopesRemoverImpl"/>

<task:scheduled-tasks>
    <task:scheduled ref="EmptyScopesRemover" method="remove" cron="0 */1 * * * *"/>
</task:scheduled-tasks>

这是一段视频https://www.youtube.com/watch?v=mt5R_KBlhTU

您可以查看玉米价值的答案 https://stackoverflow.com/a/32521238/4251431