16

As of Spring 3.0 the ScheduledTimerTask is deprecated and I can't understand how to upgrade to org.springframework.scheduling.concurrent.

    <bean id="timerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean">
        <property name="scheduledTimerTasks">
            <list>
                 <ref bean="onlineTimeSchedule" />
            </list>
            </property>
    </bean>

    <bean id="onlineTimeSchedule" class="org.springframework.scheduling.timer.ScheduledTimerTask">
        <property name="timerTask" class="com.example.OnlineTimerTask" />
        </property>
        <property name="period" value="60000" />
        <property name="delay" value="1000" />
    </bean>

Where the OnlineTimerTask extends java.util.TimerTask. It's simple task which publishes a message to publisher every minute. I checked the documentation, but nothing.. I can't understand which way to use from the concurrent package and which suits the best.

Also I want to turn this xml into @Bean in Java.

EDIT: So I tried to implement the xml with @Bean and @Configuration instead and here is what I got.

@Configuration
public class ContextConfiguration {
    @Bean
    public ScheduledExecutorFactoryBean scheduledExecutorFactoryBean() {
        ScheduledExecutorFactoryBean scheduledFactoryBean = new ScheduledExecutorFactoryBean();
        scheduledFactoryBean.setScheduledExecutorTasks(new ScheduledExecutorTask[] {onlineTimeSchedule()});

        return scheduledFactoryBean;
    }

    @Bean
    public ScheduledExecutorTask onlineTimeSchedule() {
        ScheduledExecutorTask scheduledTask = new ScheduledExecutorTask();
        scheduledTask.setDelay(1000);
        scheduledTask.setPeriod(60000);
        scheduledTask.setRunnable(new OnlineTimerTask());

        return scheduledTask;
    }
}

Will the code above be correct replacement for xml? Will in my case the setScheduledExecutorTasks work properly? I mean will the referencing to the same bean instance, if onlineTimeSchedule() is called more than once, will work here?

scheduledFactoryBean.setScheduledExecutorTasks(new ScheduledExecutorTask[] {onlineTimeSchedule()});
Rihards
  • 10,241
  • 14
  • 58
  • 78

3 Answers3

31

Use org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean in place of org.springframework.scheduling.timer.TimerFactoryBean and use org.springframework.scheduling.concurrent.ScheduledExecutorTask in place of org.springframework.scheduling.timer.ScheduledTimerTask. You will need to adjust the property names and values as needed but, that should be pretty self evident.

Optionally, you could refactor your com.example.OnlineTimerTask to not extend java.util.TimeTask as the ScheduledTimerTask only requires a runnable.

Brent Worden
  • 10,624
  • 7
  • 52
  • 57
  • @Richards Correct, the executor's do not require tasks be TimerTask instances. They merely need to implement Runnable. – Brent Worden Mar 20 '11 at 16:00
  • You have a typo there, that's why i said it. However, yea, removed the TimerTask. What does the fixedRate mean? I did set the period, delay and task, but can't figure out the point of fixedRate. – Rihards Mar 20 '11 at 16:05
  • Trying to do as you said i get this: Error creating bean with name 'timerFactoryBean' defined in ... Cannot resolve reference to bean 'onlineTimeSchedule' while setting bean property 'scheduledExecutorTasks' with key [0]; nested exception is ... BeanCreationException: Error creating bean with name 'onlineTimeSchedule' defined in ... – Rihards Mar 25 '11 at 00:45
  • Error setting property values; nested exception is ... NotWritablePropertyException: Invalid property 'executorTask' of bean class ... ScheduledExecutorTask: Bean property 'executorTask' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? – Rihards Mar 25 '11 at 00:47
  • @Brent - What to use in place of org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean – Anjanaa Mar 26 '19 at 17:44
  • 1
    @Anjanaa Does https://stackoverflow.com/questions/9825827/methodinvokingtimertaskfactorybean-is-deprecated-what-class-should-i-be-using-i help answer your question? – Brent Worden Mar 27 '19 at 19:35
7

Spring 4 configuration - Below configuration working after spring migration from 3.2.x to 4.6.x

<bean id="schedulerTask"
        class="org.springframework.scheduling.support.MethodInvokingRunnable">
        <property name="targetObject" ref="springJmsListnerContainer" />
        <property name="targetMethod" value="execute" />
    </bean>
    <bean id="timerTask" class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">
        <property name="runnable" ref="schedulerTask" />
        <property name="delay" value="100" />
        <property name="period" value="60000" />
    </bean>
    <bean class="org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean">
        <property name="scheduledExecutorTasks">
            <list>
                <ref bean="timerTask" />
            </list>
        </property>
    </bean>
Balu Gulla
  • 71
  • 1
  • 1
2

The answer is - add one "runnable" field

 <bean id="scheduledExecutorTask" 
    class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">
    <!-- wait 10 milli seconds before starting repeated execution -->
    <property name="delay">
        <value>10</value>
    </property>
    <!-- run every 1 second -->
    <property name="period">
        <value>1000</value>
    </property>
    <property name="runnable">
        <ref bean="checkInvokingTask"/>
    </property>
</bean>
Aman
  • 21
  • 1