2010-03-19 16 views
12

मैं जावा/वसंत/हाइबरनेट के बारे में पढ़ रहा हूं और एक "डमी" उदाहरणों को कड़ी मेहनत कर रहा हूं इसलिए मैंने अपने दोस्त को मेरे लिए कुछ कठिन सलाह देने के लिए कहा, और अब मैं फंस गया हूं .. यहां सबसे सरल वर्ग है जो मैं कर सकता था repeadatly हर 30 सेकंड का कहना है की सुविधा देता है, यहाँप्रत्येक 30 सेकंड में जावा क्लास निष्पादित करने का सबसे आसान तरीका क्या है?

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

} 

के बारे में सोच क्या वसंत बीन्स से metod voice() फोन करके कुछ बाहर मुद्रित करने के लिए सबसे आसान तरीका है, और यह कर मैं अब तक क्या मिल गया है है:

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

और हाँ, मैं इसे जेबॉस 5 पर चलाने की कोशिश कर रहा हूं, मैं मैवेन के साथ एक परियोजना का निर्माण कर रहा हूं।

मैं कुछ सुझाव मिला है और अपने आवेदन संदर्भ अब लगता है कि:

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> 

अब मैं इस अपवाद प्राप्त करें:

<?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 सेकंड यह जटिल होगा

उत्तर

28

मैं क्वार्ट्ज से परेशान नहीं होता, यह कुछ इस सरल के लिए overkill है क्रॉन प्रकार समय प्रविष्टि का उपयोग करने के लिए किया जाएगा। जावा 5 अपने शेड्यूलर के साथ आता है, और यह काफी अच्छा है।

पूर्व स्प्रिंग 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> 

स्प्रिंग 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

के साथ अपना प्रश्न अपडेट करूंगा कि 'अनुसूचित' एनोटेशन वास्तव में अच्छा लग रहा है। – BalusC

+0

@skaffman आपकी प्रतिक्रिया के लिए धन्यवाद, मैं अब कोशिश करूँगा, बात यह है कि मैं कुछ और जटिल करना चाहता हूं इसलिए मैं नीचे से शुरू करना चाहता हूं। 'मैं 2 ऑब्जेक्ट्स बनाना चाहता हूं जो एक-दूसरे को संदेश भेजते हैं, एक को जेबीएसएस 5 पर गड़बड़ जेएमएस मैसेजिंग का जवाब देने के लिए अन्य को बधाई देने के लिए, और मैं चाहता हूं कि यह हर 30 सेकंड में किया जाए।' इसलिए विचार करने वाली पहली बात हर 30 सेकंड में निष्पादन कार्य होगी और उसके बाद .. –

+0

@ स्काफमैन महान उत्तर, मैंने इसे दोनों तरीकों से और इसके कामकाज की कोशिश की .. w00w tnx –

1

यह जटिल लग रहा है, लेकिन यह वास्तव में ऐसा करने का सबसे अच्छा तरीका है। आप इसे एप्लिकेशन के बाहर बाहरी रूप से कॉन्फ़िगर कर सकते हैं, और वसंत/क्वार्ट्ज हैंडल निष्पादन को चलो।

यह विशेष रूप से उपयोगी होता है जब आपको कॉल करने की आवश्यकता एक लेनदेन-सक्षम सेवा कॉल है।

+0

@Reverend को यह जवाब की जांच कर सकते गोंजो स्पष्ट रूप से इसमें कुछ गड़बड़ है .. मुझे कोई छूट नहीं मिलती है, और फिर भी क्वार्ट्ज पूर्व नहीं है ecute, jboss dir के लिए युद्ध की प्रतिलिपि बनाई .. और अभी भी कुछ नहीं, मैंने सरल वेबपैप हैलो दुनिया को chk बनाया है, मेरा jboss ठीक से कॉन्फ़िगर किया गया है, हाँ यह है। –

1

मेरे पास कुछ समान है लेकिन खच्चर में क्वार्ट्जकनेक्टर क्लास का उपयोग करके, जो हर 20 सेकंड में चलता है। उदाहरण देखें। दूसरी तरह देखना 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 धन्यवाद, मैं वास्तव में वसंत सीखने की कोशिश कर रहा हूं और इसके बारे में जितना संभव हो, इसलिए मैं स्प्रिंग्स क्वार्ट्ज –

+0

का उपयोग करना चाहता हूं, ऐसा लगता है कि यह उपयोगी हो सकता है, जैसा कि समान स्थिति की तरह लगता है http: // www। jroller.com/habuma/entry/a_funny_thing_happened_while – Wiretap

+0

ऐसा प्रतीत होता है कि अब तक मैंने अपने आवेदन संदर्भ को web.xml से लिंक नहीं किया है, यह त्रुटि है जो मुझे अब मिलती है '12: 35: 51,657 त्रुटि [01-स्नैपशॉट]] त्रुटि कॉन्फ़िगर करना कक्षा org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener' मैं appcontext –

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

संबंधित मुद्दे

 संबंधित मुद्दे