io.micronaut.management.endpoint.refresh.RefreshEndpoint Maven / Gradle / Ivy
 The newest version!
        
        /*
 * Copyright 2017-2020 original 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 io.micronaut.management.endpoint.refresh;
import io.micronaut.context.env.Environment;
import io.micronaut.context.event.ApplicationEventPublisher;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.management.endpoint.annotation.Endpoint;
import io.micronaut.management.endpoint.annotation.Write;
import io.micronaut.runtime.context.scope.refresh.RefreshEvent;
import java.util.Map;
import java.util.Set;
import static io.micronaut.core.util.StringUtils.EMPTY_STRING_ARRAY;
/**
 * Exposes an {@link Endpoint} to refresh application state via a {@link RefreshEvent}.
 *
 * @author Graeme Rocher
 * @see io.micronaut.runtime.context.scope.refresh.RefreshScope
 * @see io.micronaut.runtime.context.scope.refresh.RefreshEvent
 * @see io.micronaut.runtime.context.scope.Refreshable
 * @since 1.0
 */
@Endpoint("refresh")
public class RefreshEndpoint {
    private final Environment environment;
    private final ApplicationEventPublisher eventPublisher;
    /**
     * @param environment The Environment
     * @param eventPublisher The Application event publisher
     */
    public RefreshEndpoint(Environment environment, ApplicationEventPublisher eventPublisher) {
        this.environment = environment;
        this.eventPublisher = eventPublisher;
    }
    /**
     * Refresh application state only if environment has changed (unless force is set to true).
     *
     * @param force {@link Nullable} body property to indicate whether to force all {@link io.micronaut.runtime.context.scope.Refreshable} beans to be refreshed
     * @return array of change keys if applicable
     */
    @Write
    public String[] refresh(@Nullable Boolean force) {
        if (force != null && force) {
            eventPublisher.publishEvent(new RefreshEvent());
            return EMPTY_STRING_ARRAY;
        } else {
            Map changes = environment.refreshAndDiff();
            if (!changes.isEmpty()) {
                eventPublisher.publishEvent(new RefreshEvent(changes));
            }
            Set keys = changes.keySet();
            return keys.toArray(EMPTY_STRING_ARRAY);
        }
    }
}
        © 2015 - 2025 Weber Informatics LLC | Privacy Policy