-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRuntimePropertyChangeExample.java
More file actions
57 lines (42 loc) · 1.6 KB
/
RuntimePropertyChangeExample.java
File metadata and controls
57 lines (42 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.example;
import java.util.Date;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class RuntimePropertyChangeExample implements ApplicationListener<EnvironmentChangeEvent>{
private Logger log = Logger.getLogger(RuntimePropertyChangeExample.class.getName());
@Autowired
private Environment env;
@Autowired
private TaskScheduler taskScheduler;
private ScheduledFuture<?> future;
@PostConstruct
public void schedule() {
future = taskScheduler.schedule(new Runnable() {
@Override
public void run() {
System.out.println("" + new Date() + " test.property value is : " + env.getProperty("test.property"));
}
}, new CronTrigger(env.getProperty("test.property.cron")));
}
@RequestMapping(path="/testProperty")
public String testProperty(){
return env.getProperty("test.property");
}
@Override
public void onApplicationEvent(EnvironmentChangeEvent arg0) {
future.cancel(false);
schedule();
}
}