Вопрос

I am using Spring scheduler as given bellow.

 @Scheduled(fixedDelay = ((10 * 60 * 1000) / 2))
    public void runDynamic()
    {
      //doing my stuff
    }

Now suppose I have one constant like this

public static final Integer VARIANCE_TIME_IN_MIN = 10;

And I want to use this constant as a part of my expression something like this :

@Scheduled(fixedDelay = ((MyConstants.VARIANCE_TIME_IN_MIN * 60 * 1000) / 2))
public void runDynamic()
{
//doing my stuff
}

but it is giving my compile time error. Any ideas? Thanks in Advance..!

Это было полезно?

Решение

Java annotations take compile time constants, which are defined as final primitives or strings.

SO change your definition to

   public static final int VARIANCE_TIME = 10;
   public static final long FIXED_DELAY = ((VARIANCE_TIME * 60 * 1000) / 2)

   @Scheduled(fixedDelay = FIXED_DELAY)
   public void runDynamic()      

Другие советы

Use Task scheduling using cron expression from properties file

@Scheduled(cron = "${cronTrigger.expression}")
public void runDynamic()
    {
      //doing my stuff
    }

Config in XML File:

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="exampleJob" />
    <!-- run every morning at 6 AM -->
    <property name="expression" value="0 0 6 * * ?" />
</bean>

This link1 and doc might help you

You can also create the Tash Scheduler dynamically(programatically) as explained here

private static final long VARIANCE_TIME_IN_MIN = 10l;

@Scheduled(fixedDelay = ((VARIANCE_TIME_IN_MIN * 60 * 1000) / 2))
public void runDynamic() {
    // ...
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top