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