io.axoniq.flowcontrol.producer.grpc.shutdown.ProducerCompletedShutDownPolicy Maven / Gradle / Ivy
/*
* Copyright (c) 2021. AxonIQ
*
* 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
*
* http://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.axoniq.flowcontrol.producer.grpc.shutdown;
import io.axoniq.flowcontrol.producer.grpc.ShutdownPolicy;
import java.util.function.Consumer;
/**
* @author Sara Pellegrini
* @author Milan Savic
* @since 1.0
*/
public class ProducerCompletedShutDownPolicy implements ShutdownPolicy {
private final ShutdownPolicy delegate;
private Runnable shutdown;
private Consumer onError;
public ProducerCompletedShutDownPolicy() {
this(new NeverShutdownPolicy<>());
}
public ProducerCompletedShutDownPolicy(ShutdownPolicy delegate) {
this.delegate = delegate;
}
@Override
public void initialize(Runnable shutdown, Consumer onError) {
delegate.initialize(shutdown, onError);
this.shutdown = shutdown;
this.onError = onError;
}
@Override
public void onMessageConsumed(OUT message) {
delegate.onMessageConsumed(message);
}
@Override
public void onPublisherError(Throwable t) {
delegate.onPublisherError(t);
onError.accept(t);
}
@Override
public void onPublisherComplete() {
delegate.onPublisherComplete();
shutdown.run();
}
@Override
public void shutdown() {
delegate.shutdown();
}
}