gpt4 book ai didi

java - 来自自定义属性文件的 Deltaspike + Quartz + CronExpressions

转载 作者:行者123 更新时间:2023-12-01 10:01:36 25 4
gpt4 key购买 nike

我已经实现了从属性文件配置 CronExpression,但该属性文件是 apache-deltaspike.properties,它位于 .jar 文件内。我需要从自定义配置文件中获取 cron 表达式:

import org.apache.deltaspike.core.api.config.PropertyFileConfig;

public class myOwnPropertyFileConfig implements PropertyFileConfig {
private static final long serialVersionUID = 1L;

@Override
public String getPropertyFileName() {
return "cfg/myOwnPropFile.properties";
}

@Override
public boolean isOptional() {
return false;
}

}

myOwnPropFile.properties

deltaspike_ordinal=500
property1=value1
property2=value2
QuartzJob=0 25 17 * * ?

工作:

@Scheduled(cronExpression = "{QuartzJob}")
public class MyQuartzJob implements Job {
//job code
}

当我设置此属性时,一切顺利:QuartzJob=0 25 17 * * ?在 apache-deltaspike.properties 中,但是当我在自己的属性文件中设置它时,我得到:

java.lang.IllegalStateException: No config-value found for config-key: QuartzJob 

研究后,我发现我的属性文件在 Quartz 初始化后立即加载,这解释了原因。现在,我在 Deltaspike 文档中读到,可以在我的属性文件中使用 deltaspike_ordinal 来随时加载我的属性文件。所以我尝试了,但它似乎忽略了 deltaspike_ordinal=500,并且错误不断出现。

那么,有人知道如何解决这个问题吗? Deltaspike 文档还讨论了 ConfigSource 等,但不太清楚,也没有示例。

提前致谢!

最佳答案

明白了。关键是查看PropertyFileConfig的javadoc:

  1. Automatic pickup via java.util.ServiceLoader mechanism In case you have an EAR or you need the configured values already during the CDI container start then you can also register the PropertyFileConfig via the java.util.ServiceLoader mechanism. To not have this configuration picked up twice it is required to annotate your own PropertyFileConfig implementation with org.apache.deltaspike.core.api.exclude.Exclude.

The ServiceLoader mechanism requires to have a file META-INF/services/org.apache.deltaspike.core.api.config.PropertyFileConfig containing the fully qualified Class name of your own PropertyFileConfig implementation class.
com.acme.my.own.SomeSpecialPropertyFileConfig The implementation will look like the following:

@Exclude
public class SomeSpecialPropertyFileConfig implements PropertyFileConfig {
public String getPropertyFileName() {
return "myconfig/specialconfig.properties"
}
public boolean isOptional() {
return false;
}
}

工作起来很有魅力

关于java - 来自自定义属性文件的 Deltaspike + Quartz + CronExpressions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36780507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com