[英]How can I make dynamic parameter of @Scheduled annotation?
I have a scheduled job and I want to get fixedRate dynamically but couldn't solve how to do it.我有一个预定的工作,我想动态地获得 fixedRate 但无法解决如何去做。
FixedRate gets value as milliseconds but I want to give time as hours. FixedRate 以毫秒为单位获取值,但我想以小时为单位给出时间。 And I also tried read parameter from property file and multiply it but I did not work.
而且我还尝试从属性文件中读取参数并将其相乘,但我没有工作。 How can I make this?
我怎样才能做到这一点?
package com.ipera.communicationsuite.scheduleds;
import com.ipera.communicationsuite.models.FreeDbSize;
import com.ipera.communicationsuite.repositories.interfaces.IFreeDbSizeRepository;
import com.ipera.communicationsuite.repositories.interfaces.settings.IPropertiesRepository;
import com.ipera.communicationsuite.utilities.mail.SMTPConnection;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
@Component
@AllArgsConstructor
@PropertySource("classpath:scheduled.properties")
public class KeepAlive {
private static Logger logger = LoggerFactory.getLogger(KeepAlive.class);
private IFreeDbSizeRepository freeDbSizeRepository;
private SMTPConnection smtpConnection;
private IPropertiesRepository propertiesRepository;
@Scheduled(fixedRateString ="${keepAlive.timer}")
public void keepAliveMailSender() {
StringBuilder content = new StringBuilder();
ArrayList<File> files = getDrivers();
List<FreeDbSize> list = freeDbSizeRepository.getFreeDbSize();
FreeDbSize dbDiskInfo = freeDbSizeRepository.dbDiskSize();
content.append("DB file size is: ").append(list.get(0).getType().equals("mdf") ? list.get(0).getFileSize() : list.get(1).getFileSize()).append(" MB\n")
.append("DB log size is: ").append(list.get(0).getType().equals("ldf") ? list.get(0).getFileSize() : list.get(1).getFileSize()).append(" MB\n");
propertiesRepository.updateByKey("DatabaseSize", list.get(0).getType().equals("mdf") ? list.get(0).getFileSize().toString() : list.get(1).getFileSize().toString());
propertiesRepository.updateByKey("DatabaseLogSize", list.get(0).getType().equals("ldf") ? list.get(0).getFileSize().toString() : list.get(1).getFileSize().toString());
propertiesRepository.updateByKey("FreeDiskSpaceForDb", dbDiskInfo.getFreeSpace().toString());
for (int i = 0; i < files.size(); i++) {
content.append("Free size for driver ").append(files.get(i)).append(" is ").append(files.get(i).getFreeSpace() / (1024 * 1024)).append(" MB\n");
propertiesRepository.createIfNotExistOrUpdate(("FreeSpaceInDisk".concat(Character.toString(files.get(i).toString().charAt(0)))), Long.toString(files.get(i).getFreeSpace() / (1024 * 1024)));
}
if (dbDiskInfo.getName().equals("-1")) {
content.append("This application has not permission to run query for calculate free size of disk.");
} else {
content.append("Free size of disk which contains Db is: ").append(dbDiskInfo.getFreeSpace());
}
smtpConnection.sendMail(content.toString(), "Server Is Up!!!", "fkalabalikoglu@iperasolutions.com", "", "", "", "");
logger.info("KeepAlive has runned.");
}
public ArrayList<File> getDrivers() {
ArrayList<File> list = new ArrayList<>();
File[] drives = File.listRoots();
if (drives != null && drives.length > 0) {
for (File aDrive : drives) {
list.add(aDrive);
}
}
return list;
}
}
And also my propery file is here:而且我的propery文件在这里:
keepAlive.timer=86400000
You could use SpEL in your annotation like:您可以在注释中使用 SpEL,例如:
@Scheduled(fixedRateString ="#{new Long('${keepAlive.timer}') * 1000 * 3600}")
to have expression evaluated.评估表达式。 So keepAlive.timer would be the amount of hours.
所以keepAlive.timer将是小时数。
But in my opinion it would be an ugly solution.但在我看来,这将是一个丑陋的解决方案。 I would rather put it in the properties as you have it now and just add a comment like:
我宁愿把它放在你现在拥有的属性中,然后添加如下评论:
# 24 hours is: 1000 * 3600 * 24
keepAlive.timer=86400000
Another way to use hours would be to use attribute cron that gives you more flexibility but might need some study before using:另一种使用小时数的方法是使用属性cron ,它可以为您提供更大的灵活性,但在使用之前可能需要进行一些研究:
In your code:在您的代码中:
@Scheduled(cron = "${keepAlive.timer}")
and the cron expression in your properties - for example - like:以及您的属性中的 cron 表达式 - 例如 - 如:
keepAlive.timer="*/60 00 21 * * ?"
This would run every day @ 21.00这将每天运行@ 21.00
Note this "*/60" it should accept also "0" here but in my case it did not请注意这个“* / 60”它应该在这里也接受“0”但在我的情况下它没有
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.