1

我遵循了这个例子:https ://spring.io/guides/gs/scheduling-tasks/ 。有用。然后,我将其更改为 scala 代码。我的斯卡拉代码:

@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!!!")
  }
}

为什么我的 scala 代码不起作用?谢谢

4

1 回答 1

0

基于转换http://javatoscala.com/这里是代码,也许你应该试试。

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

}
于 2019-05-07T02:11:27.267 回答