我正在使用我的项目中计划的Java。在项目中调用rest api之后,我开始在程序中进行计划。同样,我使用另一个rest api停止了此计划。换句话说,我在运行时计划,创建,停止,运行等。
我想将此代码从Java计划的转换为春季计划。
当我查看代码示例时,可以按时间开始计划。或者,我可以根据在application.properties中提供的参数启动它。但是,他们都不符合我的意愿。我的要求如下。
if(condition) {
startScheduled();
}
else {
stopScheduled();
}
但是我找不到这样的例子。你能帮我吗?
答案 0 :(得分:0)
我无法发表评论,因此我将在此处发布。看起来与您的问题类似:How to restart scheduled task on runtime with EnableScheduling annotation in spring?
答案 1 :(得分:0)
首先看看有关{Scheduled注解)的Spring tutorial。
然后,您需要确定计划任务的执行频率。为此,您将创建一个满足您需要的CRON表达式。 website可以帮助您实现这一目标。
几乎在任务内部,只需检查条件并相应地执行代码即可。
这是一些示例代码:
<?php
//getting menu first
$sql = 'SELECT id,intitule FROM rubriques ';
$stmt = $conn->query($sql);
if ($stmt->num_rows > 0) {
while ($row = $stmt->fetch_assoc()){
//getting values
$intitule =$row['intitule'];
$id = $row['id']; ?>
<!--your menu-->
<li class="active"><a href="index.html"><?php
echo $intitule;
?></a>
<?php
//passing the id from first to action table for compare and retrieve that rows only
$sql1 = 'SELECT * FROM actions where idr= ' . $id;
$stmt1 = $conn->query($sql1);
?>
<ul class="dropdown">
<?php
while ($row1 = $stmt1->fetch_assoc()) {
$lien = $row1['lien'];
$intitulee = $row1['intitulee'];
?>
<!--your submenu-->
<li><a href="<?php echo $lien;?>"><?php echo $intitulee; ?></a></li>
<?php
}
?>
</ul> <!--closing ul -->
</li>
<?php
}}
$conn->close();
//closing if
?>
注意@Component注释,并且这个玉米表达式每天都会在我的时区的00:05执行任务。
希望这会有所帮助
答案 2 :(得分:0)
就我而言,我使用@Scheduled
来安排一个较短的周期。我们正在该时间表中使用CronSequenceGenerator
在运行时检查cron。