2010-03-19 3 views
12

나는 java/spring/hibernate에 대해 읽었으며 물마루를 "더미"예제로 사용하여 친구에게 내게 조금 더 열심히 추천 할 것을 말했고 지금은 붙어있다. 내가 할 수있는 가장 간단한 클래스가있다. repeadatly 30 초마다 여기, 말할 수 봄 콩에서 metod voice()를 호출하여 뭔가를 인쇄 할 수있는 가장 간단한 방법은 무엇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()); 
    } 

} 

생각하고, 그것을하는 것은 내가 지금까지있어 무엇 :

<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으로 프로젝트를 만들고 있습니다.

나는 몇 가지 제안을했고처럼 내 애플리케이션 컨텍스트는 이제 같습니다

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 

:

<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를 얻을 : 여기

<?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 파일입니다 나는 30 초마다 hello world와 같은 것을 실행하는 것이 복잡하다는 것을 깨닫지 못했다.

답변

28

, 그것은이 간단한 뭔가 잔인한 볼 수있는 다른 방법은 크론 유형 시간 항목을 사용하는 것입니다. Java5는 자체 스케줄러와 함께 제공됩니다.

전 봄 (3)이 가장 쉬운 접근했다입니다 : 봄 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> 

, 그것은 말도 안되게 쉽게 될 수 있습니다

@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

+0

@skaffman 답장을 보내 주셔서 감사합니다. 지금 해보겠습니다. 좀 더 복잡한 작업을 수행하고 싶습니다. 그래서 처음부터 시작하고 싶습니다. "서로 메시지를주고받는 2 개의 객체를 만들고 싶습니다. 하나는 JBossS 5에서 JMS 메시징으로 응답하기 위해 다른 객체를 맞이하고, 30 초마다이 작업을 수행하기를 원합니다."그래서 고려해야 할 첫 번째 작업은 30 초마다 실행되는 작업입니다. 그리고 나서 .. –

+0

@skaffman 훌륭한 대답, 나는 그것을 두 가지 방법으로 모두 시도했다. w00w tnx –

1

복잡해 보이지만 그렇게하는 것이 가장 좋습니다. 응용 프로그램 외부에서 구성 할 수 있으며 spring/quartz가 실행되도록 처리 할 수 ​​있습니다.

이것은 호출해야하는 메소드가 트랜잭션 가능 서비스 호출 인 경우 특히 유용합니다.

+0

@Reverend이 답을 확인하실 수 있습니다 Gonzo 분명히이 문제가 있습니다. 나는 예외가 없지만 석영은 ex ecute, jboss dir ..에 복사 된 전쟁 .. 그리고 여전히 아무것도, 나는 간단한 webapp hello world를 만들었습니다. 내 jboss가 적절히 구성되어 있습니다. 그렇습니다. –

1

비슷한 점이 있지만 20 초마다 실행되는 QuartzConnector 클래스를 사용합니다. 예제를 참조하십시오. 내가 석영 신경 쓰지 것 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

@ 와이어 탭 감사합니다. 저는 봄에 가능한 한 많은 것을 배우려고합니다. 봄 석영을 사용하고 싶습니다. –

+0

비슷한 상황 인 것 같아서 유용 할 것 같습니다. http : // www. jroller.com/habuma/entry/a_funny_thing_happened_while – Wiretap

+0

지금까지는 web.xml과 내 응용 프로그램 컨텍스트를 연결하지 않은 것으로 보입니다. 이제 오류가 발생합니다. '12 : 35 : 51,657 오류 [01-SNAPSHOT]] 오류 구성 org.springframework.web.context.ContextLoaderListener 클래스의 응용 프로그램 수신기 java.lang.ClassNotFoundException : org.springframework.web.context.ContextLoaderListener' appcontext로 질문을 업데이트합니다. –

관련 문제