Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh Scope on restart. #1266

Merged
merged 6 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.springframework.cloud.context.refresh.ConfigDataContextRefresher;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.cloud.context.refresh.LegacyContextRefresher;
import org.springframework.cloud.context.refresh.RefreshScopeLifecycle;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.cloud.endpoint.event.RefreshEventListener;
import org.springframework.cloud.logging.LoggingRebinder;
Expand Down Expand Up @@ -117,6 +118,12 @@ public ConfigDataContextRefresher configDataContextRefresher(ConfigurableApplica
return new ConfigDataContextRefresher(context, scope, properties);
}

@ConditionalOnProperty(value = "spring.cloud.refresh.on-restart.enabled", matchIfMissing = true)
@Bean
RefreshScopeLifecycle refreshScopeLifecycle(ContextRefresher contextRefresher) {
return new RefreshScopeLifecycle(contextRefresher);
}

@Bean
public RefreshEventListener refreshEventListener(ContextRefresher contextRefresher) {
return new RefreshEventListener(contextRefresher);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.context.refresh;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.context.Lifecycle;

/**
* @author Olga Maciaszek-Sharma
*/
public class RefreshScopeLifecycle implements Lifecycle {

private static final Log LOG = LogFactory.getLog(RefreshScopeLifecycle.class);

private final ContextRefresher contextRefresher;

private final Object lifecycleMonitor = new Object();

private volatile boolean running = true;

public RefreshScopeLifecycle(ContextRefresher contextRefresher) {
this.contextRefresher = contextRefresher;
}

@Override
public void start() {
synchronized (lifecycleMonitor) {
if (!isRunning()) {
if (LOG.isInfoEnabled()) {
LOG.info("Refreshing context on restart.");
}
contextRefresher.refresh();
}
running = true;
}
}

@Override
public void stop() {
synchronized (lifecycleMonitor) {
if (isRunning()) {
running = false;
}
}
}

@Override
public boolean isRunning() {
return running;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
"type": "java.lang.Boolean",
"description": "Enable the DecryptEnvironmentPostProcessor.",
"defaultValue": true
},
{
"name": "spring.cloud.refresh.on-restart.enabled",
"type": "java.lang.Boolean",
"description": "Enable refreshing context on start.",
"defaultValue": true
}
]
}
Expand Down