1

I followed the example: https://spring.io/guides/gs/scheduling-tasks/. it works. Then, I changed it to scala codes. My scala codes:

@Component
class ScheduledConsumer {

  private val log = LoggerFactory.getLogger(classOf[ScheduledConsumer])
  private val dateFormat = new SimpleDateFormat("HH:mm:ss")

  @Scheduled(fixedRate = 50)
  def reportCurrentTime(): Unit = {
    log.info("The time is now {}!!!", ScheduledConsumer.dateFormat.format(new Date))
    println("This is for testing!!!")
  }
}

Why my scala codes not work? Thanks

BAE
  • 8,550
  • 22
  • 88
  • 171
  • Maybe you're missing the `package` declaration? Spring Boot scans the `@SpringBootApplication`'s package and subpackages recursively. – insan-e May 07 '19 at 07:54
  • Did you add `@EnableScheduling` annotation? Can you check if `ScheduledConsumer` is in a subpackage of a package of a class annotated with `@SpringBootApplication`? – Krzysztof Atłasik May 07 '19 at 08:23

1 Answers1

0

Based conversion http://javatoscala.com/ here is the code, perhaps you should try it.

package hello

import java.text.SimpleDateFormat    
import java.util.Date    
import org.slf4j.Logger   
import org.slf4j.LoggerFactory   
import org.springframework.scheduling.annotation.Scheduled  
import org.springframework.stereotype.Component   
import ScheduledTasks._

//remove if not needed
import scala.collection.JavaConversions._

object ScheduledTasks {

  private val log: Logger = LoggerFactory.getLogger(classOf[ScheduledTasks])
  private val dateFormat: SimpleDateFormat = new SimpleDateFormat("HH:mm:ss")

}

@Component
class ScheduledTasks {

  @Scheduled(fixedRate = 5000)
  def reportCurrentTime(): Unit = {
    log.info("The time is now {}", dateFormat.format(new Date()))
  }

}
Jonathan JOhx
  • 5,784
  • 2
  • 17
  • 33