io.helidon.microprofile.lra.CoordinatorLocatorService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of helidon-microprofile-lra Show documentation
Show all versions of helidon-microprofile-lra Show documentation
MicroProfile Long Running Actions implementation
/*
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
*
* 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.helidon.microprofile.lra;
import java.net.URI;
import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import io.helidon.common.HelidonServiceLoader;
import io.helidon.common.Reflected;
import io.helidon.lra.coordinator.client.CoordinatorClient;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.enterprise.inject.spi.DeploymentException;
import jakarta.inject.Inject;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import static io.helidon.lra.coordinator.client.CoordinatorClient.CONF_DEFAULT_COORDINATOR_URL;
import static io.helidon.lra.coordinator.client.CoordinatorClient.CONF_KEY_COORDINATOR_TIMEOUT;
import static io.helidon.lra.coordinator.client.CoordinatorClient.CONF_KEY_COORDINATOR_TIMEOUT_UNIT;
import static io.helidon.lra.coordinator.client.CoordinatorClient.CONF_KEY_COORDINATOR_URL;
/**
* Service for locating of proper coordinator client.
*/
@Reflected
public class CoordinatorLocatorService {
private final Optional clientFqdn;
private final Long coordinatorTimeout;
private final TimeUnit coordinatorTimeoutUnit;
private Supplier coordinatorUriSupplier;
@Inject
CoordinatorLocatorService(@ConfigProperty(name = "mp.lra.coordinator.client") Optional clientFqdn,
@ConfigProperty(name = CONF_KEY_COORDINATOR_URL, defaultValue = CONF_DEFAULT_COORDINATOR_URL)
String coordinatorUrl,
@ConfigProperty(name = CONF_KEY_COORDINATOR_TIMEOUT, defaultValue = "30")
Long coordinatorTimeout,
@ConfigProperty(name = CONF_KEY_COORDINATOR_TIMEOUT_UNIT, defaultValue = "SECONDS")
TimeUnit coordinatorTimeoutUnit) {
this.clientFqdn = clientFqdn;
this.coordinatorUriSupplier = () -> URI.create(coordinatorUrl);
this.coordinatorTimeout = coordinatorTimeout;
this.coordinatorTimeoutUnit = coordinatorTimeoutUnit;
}
/**
* Override standard supplier for getting coordinator uri from config.
*
* @param uriSupplier used for locating of coordinator
*/
public void overrideCoordinatorUriSupplier(Supplier uriSupplier) {
coordinatorUriSupplier = uriSupplier;
}
@Produces
@ApplicationScoped
CoordinatorClient coordinatorClient() {
List candidates = HelidonServiceLoader.create(ServiceLoader.load(CoordinatorClient.class)).asList();
if (candidates.isEmpty()) {
throw new DeploymentException("No coordinator adapter found");
}
if (candidates.size() > 1) {
throw new DeploymentException("Ambiguous coordinator adapter candidates found: " + candidates.stream()
.map(CoordinatorClient::getClass)
.map(Class::getName)
.collect(Collectors.joining(","))
);
}
CoordinatorClient client = candidates.stream().findFirst().get();
if (clientFqdn.isPresent()) {
Optional selectedClient = candidates.stream()
.filter(c -> c.getClass().getName().equals(clientFqdn.get()))
.findFirst();
client = selectedClient.orElseThrow(() -> new DeploymentException("Configured coordinator adapter "
+ clientFqdn.get()
+ " not found."));
}
client.init(() -> coordinatorUriSupplier.get(), Duration.ofMillis(coordinatorTimeoutUnit.toMillis(coordinatorTimeout)));
return client;
}
}