当前位置: 首页 > 知识库问答 >
问题:

如何制作@Scheduled annotation的动态参数?

闻人昊昊
2023-03-14

我有一个计划的工作,我想得到固定利率动态,但不能解决如何做到这一点。

FixedRate以毫秒为单位获取值,但我想以小时为单位给出时间。我还试着从属性文件中读取参数并将其相乘,但没有成功。我该怎么做?

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;
    }
}

我的财产档案也在这里:

keepAlive.timer=86400000

共有1个答案

杜辰龙
2023-03-14

您可以在注释中使用SpEL,如:

@Scheduled(fixedRateString ="#{new Long('${keepAlive.timer}') * 1000 * 3600}")

对表达式进行求值。所以保持活力。计时器将是小时数。

但在我看来,这将是一个丑陋的解决方案。我宁愿把它放在你现在拥有的属性中,只需添加如下注释:

# 24 hours is: 1000 * 3600 * 24  
keepAlive.timer=86400000

另一种使用小时数的方法是使用属性cron,这会给您带来更大的灵活性,但在使用之前可能需要进行一些研究:

在代码中:

@Scheduled(cron = "${keepAlive.timer}")

属性中的cron表达式,例如:

keepAlive.timer="*/60 00 21 * * ?"

这将每天运行@21.00

注意这个"*/60"在这里也应该接受"0",但在我的情况下,它不接受

 类似资料:
  • 问题内容: 在此示例中,我需要获取名称作业,我有一个代码用于使用该代码获取build参数的先前值 现在,工作名称是硬编码的,我需要获得的名称将是当前工作的名称。我该怎么做? 问题答案: Groovy Dynamic参数无法访问其余詹金斯工作所专有的常规环境变量。 这是获取工作名称的有效方法:

  • 问题内容: 我在制作JApplet并遇到动画问题。 这是我的代码: 有了它,就没有动画:在循环过程中什么也没发生,repaint()方法似乎只在精灵停止移动后才起作用。 我只想将Swing用于此目的,关于如何进行的任何想法? 谢谢阅读。 问题答案: 您应该使用a 来执行动画,而不要使用“线程睡眠”。这是一个很好的链接,可以助您一臂之力:http : //java.sun.com/docs/book

  • 如何在侧滑块中制作一些不同类型的动画。有图书馆吗?如果有图书馆,请提供我。

  • 问题内容: 嗨,我是Java GUI的新手,正试图使启动屏幕或图像显示3秒钟。然后,它将进入我的主程序。是否有人有想法做到这一点,或者可以将我链接到任何教程? 到目前为止,我已经做到了这一点,但不确定从何而来。 问题答案: 最简单的方法是创建并添加您的内容,然后使用 试试这个代码: 或者, 您可以使用 SplashScreen 类创建一个启动画面 ****

  • 问题内容: Web应用程序的一个关键组件是面包屑/导航。使用Angular UI Router,将面包屑元数据放在各个状态中而不是放在控制器中是有意义的。手动为需要的每个控制器创建面包屑对象是一项直截了当的任务,但这也是一个非常麻烦的任务。 我已经看到了一些使用Angular的自动面包屑的解决方案,但是老实说,它们相当原始。某些状态,例如对话框或侧面板,不应更新面包屑,但是如果将当前的附加组件添加