spring-cloud / spring-cloud-config

External configuration (server and client) for Spring Cloud
Apache License 2.0
1.94k stars 1.28k forks source link

Using scheduler and refresh scope #2398

Closed devildeepak closed 2 months ago

devildeepak commented 3 months ago
package org.example.configclient.STORAGE;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.endpoint.RefreshEndpoint;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@RefreshScope
@EnableScheduling
public class ConfigPrinter {

    @Value("${Project1.DEV.gamma}")
    private String configValue;

//    private final RestTemplate restTemplate;

    private final RefreshEndpoint refreshEndpoint;

    @Autowired
    public ConfigPrinter(RefreshEndpoint refreshEndpoint) {
        this.refreshEndpoint = refreshEndpoint;
    }

//    @Scheduled(fixedRate = 5000) // Refresh every 10 minutes
//    public void refreshConfig() {
//        refreshEndpoint.refresh();
//    }

    @Scheduled(fixedRate = 5000) // Print every 5 seconds
    public void printConfigValue() {
        System.out.println(configValue);
    }
}

`

whenever I send a post request to the actuator/refresh endpoint it is giving the log and stops printing the value which is printed using a scheduler.

log

2024-04-02T10:29:25.502+05:30 INFO 6264 --- [config-client] [nio-9090-exec-2] o.s.c.c.c.ConfigServerConfigDataLoader : Fetching config from server at : http://localhost:8888 2024-04-02T10:29:27.823+05:30 INFO 6264 --- [config-client] [nio-9090-exec-2] o.s.c.c.c.ConfigServerConfigDataLoader : Located environment: name=config-client, profiles=[default], label=null, version=3761a2d4b89b29503e3be50ef574c8e6169df1af, state=null 2024-04-02T10:29:27.849+05:30 INFO 6264 --- [config-client] [nio-9090-exec-2] o.s.c.c.c.ConfigServerConfigDataLoader : Fetching config from server at : http://localhost:8888 2024-04-02T10:29:29.339+05:30 INFO 6264 --- [config-client] [nio-9090-exec-2] o.s.c.c.c.ConfigServerConfigDataLoader : Located environment: name=config-client, profiles=[development], label=null, version=3761a2d4b89b29503e3be50ef574c8e6169df1af, state=null

ryanjbaxter commented 3 months ago

I presume you are trying to do something else with the scheduler other than print something, can you elaborate on the real world use case?

Can you provide a complete, minimal, verifiable sample that reproduces the problem? It should be available as a GitHub (or similar) project or attached to this issue as a zip file.

spring-cloud-issues commented 2 months ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-cloud-issues commented 2 months ago

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.