gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:58:14 30 4
gpt4 key购买 nike

我一直在阅读有关 java/spring/hibernate 并通过“虚拟”示例进行工作,所以我告诉我的 friend 为我推荐一些更难的东西,现在我被困住了.. 这是我最简单的类(class)能想到

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 beans调用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>

现在我得到了这个异常(exception):

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 之类的东西会这么复杂

最佳答案

我不会为 Quartz 烦恼,对于这么简单的事情来说,它是矫枉过正的。 Java5自带调度器,已经够用了。

Pre-Spring 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>

关于java - 每 30 秒执行一次 Java 类的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2476337/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com