问题描述
我已经从属性文件配置CronExpression,但此属性文件是apache-deltaspike.properties,它在.jar文件中。我需要从我的自定义配置文件中获取cron表达式:
I've achieved configuring CronExpression from a propery file, but this property file is apache-deltaspike.properties, which is inside the .jar file. I need to take the cron expression from my custom config file:
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
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内,但是当我在自己的属性文件中设置它时,我得到:
Everything goes good when I set this property: QuartzJob=0 25 17 * * ?inside apache-deltaspike.properties, but when I set it in my own property file, I get:
java.lang.IllegalStateException: No config-value found for config-key: QuartzJob
研究,我发现我的属性文件是在Quartz初始化之后加载的,这解释了为什么。现在,我读在Deltaspike doc,它可以获得我的属性文件加载任何我想要的,使用deltaspike_ordinal在我的属性文件。所以,我试过,但似乎忽略deltaspike_ordinal = 500,并且错误不断产生。
Researching, I found that my property file is loaded right after Quartz initialization, and that explains the why. Now, I read in Deltaspike doc that it's possible to get my property file loaded whenever I want, using deltaspike_ordinal inside my property file. So I tried, but it seems to ignore the deltaspike_ordinal=500, and error keeps arising.
那么,有人知道如何排序吗? Deltaspike doc也谈到一个ConfigSource等等,但它不是很清楚,没有例子。
So, does someone know how to sort this out? Deltaspike doc also talks about a ConfigSource and so, but it's not so clear and there are no examples.
提前感谢!
推荐答案
知道了。
关键是查看PropertyFileConfig的javadoc:
Got it.The key was to look into the javadoc of PropertyFileConfig:
- 自动通过java.util.ServiceLoader机制获取如果您有一个EAR或,您需要在CDI
容器启动期间已配置的值,那么您还可以通过
注册PropertyFileConfig java.util.ServiceLoader机制。为了没有这个配置
拾起两次,需要用org.apache.deltaspike.core.api.exclude.Exclude注释你自己的PropertyFileConfig
实现。
- 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.
ServiceLoader机制需要有一个文件
META-INF / services / org.apache.deltaspike.core.api.config.PropertyFileConfig包含完全限定的类名您自己的
PropertyFileConfig实现类。
com.acme.my.own.SomeSpecialPropertyFileConfig实现
将如下所示:
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;
}
}
魅力
这篇关于Deltaspike + Quartz + CronExpressions从自定义属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!