有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我已经通过一个属性文件实现了对CronExpression的配置,但是这个属性文件是ApacheDeltaspike。属性,它位于。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。属性

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**? 阿帕奇·德尔塔斯派克内部。属性,但当我在自己的属性文件中设置它时,我得到:

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

通过研究,我发现我的属性文件在Quartz初始化后立即加载,这就解释了原因。现在,我在Deltaspike文档中读到,可以在任何时候加载我的属性文件,在属性文件中使用Deltaspike_序数。所以我试过了,但它似乎忽略了deltaspike_ordinal=500,错误不断出现

那么,有人知道如何解决这个问题吗?Deltaspike doc也谈到了ConfigSource等等,但它不太清楚,也没有例子

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    明白了。 关键是查看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;
          }
      }
    

    很有魅力