user1493140
user1493140

Reputation: 6186

Java Spring Scheduled job not working

I have a web application which is supposed to run a scheduled code:

package com.myproject.daemon.jobs;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class MyDaemonJob  {

    private static final Logger log = LoggerFactory.getLogger(MyDaemonJob.class);

    @PostConstruct
    public void init() {
        log.info("MyDaemonJob is intialized " );
    }

    @Scheduled(fixedDelay = 1000)
    public void startDaemon()  {
        try {
            log.info("MyDaemonJob is running ...");
        } catch (Exception e) {
            log.error("Encountered error running scheduled job: " + e.getMessage());
        }
    }
}

It surely is recognized as a Spring bean and initialized, as I can see from the PostConstruct log. However the method with the @Scheduled annotation never runs although it is supposed to run every 1 second.

Here is app context xml

<?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:context="http://www.springframework.org/schema/context"
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-4.0.xsd">

<context:component-scan base-package="
    com.myproject.daemon.jobs,
    com.myproject.product" />

</beans>

Upvotes: 5

Views: 7569

Answers (2)

Sathyendran a
Sathyendran a

Reputation: 1819

To use the @scheduled annotation it is necessary to include the spring task name space in spring bean configuration xml file. You can update the following namespac e in your xml configuration file.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
    xmlns:context="http://www.springframework.org/schema/context"
    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-4.0.xsd
                       http://www.springframework.org/schema/task 
                       http://www.springframework.org/schema/task/spring-task-4.1.xsd">

Otherwise if you use the spring-boot application you can include the @EnableScheduling in your configuration file

Upvotes: 0

user1493140
user1493140

Reputation: 6186

Thank you ALL for quick help. This is really helpful.

The code started working, once I added config class with annotations as shown below --

package com.myproject;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
public class AppConfig {
    // various @Bean definitions
}

Upvotes: 7

Related Questions