2010-03-19 41 views
12

我一直在閱讀關於java/spring/hibernate的內容,並通過一個「虛擬」的例子,所以我告訴我的朋友推薦一些對我來說更難的東西,現在我被卡住了......這裏是最簡單的類,我可以想到每30秒執行一次Java類最簡單的方法是什麼?

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()); 
    } 

} 

什麼是從春天豆呼籲梅託德​​voice()打印出來的東西是最簡單的方法,並做到這一點repeadatly每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> 

現在我得到這個exeption:

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秒執行一次像你好世界這樣的事情會是這個複雜的

回答

28

我不會打擾石英,這是矯枉過正的東西這麼簡單。 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> 
+2

'@ Scheduled'註釋看起來非常好。 – BalusC 2010-03-19 12:32:27

+0

@skaffman謝謝你的迴應,我現在就試試,事情是我想做一些更復雜的事情,所以我想從頭開始。 '我想創建兩個對象互相發送消息,一個讓其他人通過JBOSS 5響應JMS消息,我希望每30秒完成一次。「因此,首先要考慮的是每30秒執行一次任務然後開始.. – 2010-03-19 12:36:52

+0

@skaffman偉大的答案,我試過了它的兩種方式和它的工作.. w00w tnx – 2010-03-19 13:10:47

1

看起來很複雜,但這確實是最好的方法。你可以在應用程序外部配置它,並讓spring/quartz處理執行。

當您需要調用的方法是啓用事務的服務調用時,這非常有用。

+0

@Reverend奇聞趣事顯然這是有問題的..我沒有得到任何豁免,但石英並沒有ecute,將戰爭複製到jboss dir ..並且仍然沒有,我創建簡單的webapp hello world來chk是我的jboss配置正確,是的。 – 2010-03-19 10:13:45

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> 
+0

@Wiretap謝謝我真的很想學習春天和儘可能多的關於它,所以我想使用彈簧石英 – 2010-03-19 10:29:46

+0

看起來像這可能是有用的,因爲聽起來像類似的情況 http:// www。 jroller.com/habuma/entry/a_funny_thing_happened_while – Wiretap 2010-03-19 10:52:30

+0

看來,直到現在我沒有鏈接我的應用程序上下文與web.xml這裏是我現在得到的錯誤'12:35:51,657錯誤[01-SNAPSHOT]]錯誤配置應用程序監聽器類org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener'我將使用appcontext更新我的問題 – 2010-03-19 11:48:47

相關問題