1

Am new to spring Scheduling. After some research i found that it can be done using cron expression. I have two questions to the forum.I need to schedule a task to run everyday at 8pm.. is the below cron expression schedule task at 8pm everyday?

 @Scheduled(cron="0 0 20 * * ?")

For my testing purpose i scheduled a task to run every 5 minutes. And the expression used was

 @Scheduled(cron="0 0/5 * * * *")

But i observed that above expression would fire my method every 5 minutes. And every 5 minutes my piece of code runs twice. I don't know why it is running twice every 5 minutes.

Below is my code

@Scheduled(cron="0 0/5 * * * *")
    public void demo(){
        counter++;
        System.out.println("Method "+new Date()+" counter "+counter); }

output of the above code is :

Method Thu Nov 05 22:26:00 IST 2015 counter 1
Method Thu Nov 05 22:26:00 IST 2015 counter 2

My static variable counter says that this method is called twice every 5 minutes.

my spring.xml is as below

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.controller" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="Daily" class="com.scheduler.Daily">
    </bean>

    <mvc:resources mapping="/resources/**" location="/resources/"  
    cache-period="31556926"/>
    <mvc:annotation-driven />
    <task:annotation-driven />

</beans>

Any help appreciated.

RK3
  • 1,221
  • 9
  • 26
  • 37

0 Answers0