在Java中如何通过@Scheduled将参数传递给方法?

huangapple go评论43阅读模式
英文:

In java how to pass parameter to a method with @scheduled?

问题

Now I have a scheduled method:

@Scheduled(corn = "0 0 0 * * *")
public void method(){
  // do something;
}

Now I wish to pass a parameter (boolean save) to the method. Here save =true while the method is called scheduled, and save = false while the method is called in another way, which is like:

@Scheduled(corn = "0 0 0 * * *")
public void method(boolean save){
  // do something;
}

But it returns an error:

Only no-arg methods may be annotated with @Scheduled

So How can I achieve this?

英文:

Now I have a scheduled method:

@Scheduled(corn = "0 0 0 * * *")
public void method(){
  // do something;
}

Now I wish to pass a parameter (boolean save) to the method. Here save =true while the method is called scheduled, and save = false while the method is called in another way, which is like:

@Scheduled(corn = "0 0 0 * * *")
public void method(boolean save){
  // do something;
}

But it returns an error:

> Only no-arg methods may be annotated with @Scheduled

So How can I achieve this?

答案1

得分: 2

现在我希望将一个参数(布尔值save)传递给方法。在调用方法时,当save = true时,方法被调用为定时调度,而当save = false时,则以其他方式调用该方法。

你无法在定时调度的方法中使用参数。你可以这样重载你的方法:

@Scheduled(corn = "0 0 0 * * *")
public void method(){
  // 做一些事情;
}

public void method(boolean save){
  if (save) {
     method();
     return;
  }
  // 做其他事情;
}

这样当你调用method(false)时,将调用第二个方法;如果你调用method(true),第一个方法将在第二个方法内部执行,同时仍然可以通过定时调度调用第一个方法。

英文:

> Now I wish to pass a parameter (boolean save) to method. Here save =
> true while the method is called scheduled, and save = false while the
> method is called in other way

You can't use parameters in a sheduled method. What you could do is overload your method like this:

@Scheduled(corn = "0 0 0 * * *")
public void method(){
  // do something;
}

public void method(boolean save){
  if (save) {
     method();
     return;
  }
  // do something else;
}

This way when you call method(false), your second method will be called, if you call method(true) your first method will be executed inside of your second method while still making your first method available for sheduling.

答案2

得分: 1

你可以使用Elastic-job来获取任务的计划调度和手动调用方式。它提供了一个触发器 API 以手动调用任务。

如果你需要使用这个 save 标志来执行不同的操作,我认为你应该将逻辑提取出来,并为不同的入口提供它。
像这样

public void doSomething(boolean flag) {
  // 做一些事情;
}

@Scheduled(cron = "0 0 0 * * *")
public void method(){
  doSomething(true);
}

@RequestMapping("/xxx")
public void method(){
  doSomething(false);
}
英文:

You can get both scheduled and manual call way of tasks by using Elastic-job.It provides a triger api to call task manual.

If you need this save flag to do someting different,I think you should pull away logic here and provide it for different entrance.
Like this

public void doSomething(boolean flag) {
  // do something;
}

@Scheduled(corn = "0 0 0 * * *")
public void method(){
  doSomething(true);
}

@RequestMapping("/xxx")
public void method(){
  doSomething(false);
}

huangapple
  • 本文由 发表于 2020年7月31日 14:34:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63186875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定