@Scheduled of SpringIO not working in scala?

148 Views Asked by At

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

1

There are 1 best solutions below

0
Jonathan JOhx On

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

}