从属性文件中以常规间隔在运行时读取属性。

huangapple go评论90阅读模式
英文:

Read properties from properties file in a regular interval at runtime

问题

有一些属性,我想在一个时间间隔内读取这些属性,如果属性有任何更新,我会根据这些更新采取一些操作。

@Value("${sms.smpp.country.list:{}}")
private String smppCountryList;

@Value("${sms.smpp.country.config.list:{}}")
private String smppCountryConfigList;

@Value("${sms.smpp.properties.file.location:{}}")
private String propertiesFileLocation;

我已经编写了一个代码来使用cron读取属性,下面是代码示例:

@Scheduled(cron = "0 0/7 * * * ?")
public Properties readPropertiesFile() {
    Properties prop = null;
    
    try (InputStream input = new FileInputStream(propertiesFileLocation)) {
        prop = new Properties();
        prop.load(input);
    } catch (IOException ex) {
        logger.info("[SMPP] [CONFIG CRON] Exception occurred while reading the properties file: [{}]", ex.getMessage());
    }
    
    return prop;
}

该代码由cron调用,或者我也可以在文件上编写一个onFileChangeHandler。

但是,是否有更简单和优雅的方法来实现这一点,比如Spring是否可以处理这个任务?

英文:

There are some properties, and I want to read these properties in an interval, and if there is any update in the properties, I am taking some action on that basis.

	@Value("${sms.smpp.country.list:{}}")
	private String smppCountryList;

	@Value("${sms.smpp.country.config.list:{}}")
	private String smppCountryConfigList;

	@Value("${sms.smpp.properties.file.location:{}}")
	private String propertiesFileLocation;
	 

I have written a code to read the properties using cron, here is the code.

	@Scheduled(cron = "0 0/7 * * * ?")
	public Properties readPropertiesFile() {
		Properties prop = null;
		
		try (InputStream input = new FileInputStream(propertiesFileLocation)) {
			prop = new Properties();
			prop.load(input);
		} catch (IOException ex) {
			logger.info("[SMPP] [CONFIG CRON] Exception occurred while reading the properties file: [{}]",ex.getMessage());
		}
		
		return prop;
	}

The code is getting called by the cron, or I can also write an onFileChangeHandler on the file.

But is there any easy and elegant way to do this, like can this be handled by spring?

答案1

得分: 1

Edit 1: 查看您的代码,我想指出一件事情,即 @Value 变量即使连续运行 cron 作业也不会更新为更新的值

如果您定期想要查找配置更改,Spring Boot 配备了一个 Spring 配置服务器。

您的微服务将随着配置的更改而保持最新,而无需编写任何代码。

查看 Spring Cloud 配置服务器。

英文:

Edit 1: Looking at your code i want to point one thing that @Value variables won't be updated with updated value even if you continuously run cron job

If you regularly want to look for configuration changes Spring Boot comes with a spring config server.

Your micro service will be up to date with the configuration without writing any lines to code.

Have a look a Spring Cloud Config Server

答案2

得分: 1

你需要使用Spring Config Server,并且如果你想要更新用@Value注解标记的属性,你需要给类加上@RefreshScope注解。

英文:

You have to use Spring Config Server and if you want to update property annotated with @Value, you need to annotate the class with @RefreshScope.

huangapple
  • 本文由 发表于 2020年7月30日 16:11:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63168891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定