All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.smallrye.stork.servicediscovery.composite.CompositeServiceDiscovery Maven / Gradle / Ivy

Go to download

A service discovery provider that combines multiple service discoveries. The mechanism returns services from _all_ the discovery providers that it combines, That is, it obtains service lists from all services and just then returns a combined list.

The newest version!
package io.smallrye.stork.servicediscovery.composite;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.groups.UniJoin;
import io.smallrye.stork.api.NoSuchServiceDefinitionException;
import io.smallrye.stork.api.Service;
import io.smallrye.stork.api.ServiceDiscovery;
import io.smallrye.stork.api.ServiceInstance;
import io.smallrye.stork.api.StorkServiceRegistry;
import io.smallrye.stork.servicediscovery.composite.util.CombiningList;

/**
 * A implementation of service discovery delegating to other services.
 */
public class CompositeServiceDiscovery implements ServiceDiscovery {

    private final Collection serviceNames;
    private final List elements = new ArrayList<>();
    private final String serviceName;
    private StorkServiceRegistry stork;
    private volatile boolean initialized;

    /**
     * Creates a new CompositeServiceDiscovery
     *
     * @param serviceName the service name
     * @param serviceNames the composed service names
     */
    public CompositeServiceDiscovery(String serviceName, Collection serviceNames) {
        this.serviceNames = Collections.unmodifiableCollection(serviceNames);
        this.serviceName = serviceName;
    }

    @Override
    public Uni> getServiceInstances() {
        if (!initialized) {
            init();
        }
        UniJoin.Builder> builder = Uni.join().builder();
        for (ServiceDiscovery element : elements) {
            builder = builder.add(element.getServiceInstances());
        }

        return builder.joinAll().andFailFast().onItem().transform(CombiningList::new);
    }

    private void init() {
        List list = new ArrayList<>();
        for (String service : serviceNames) {
            Service serviceElement = stork.getServiceOptional(service)
                    .orElseThrow(() -> new NoSuchServiceDefinitionException(
                            service + " (used in composite service discovery " + serviceName + ")"));
            list.add(serviceElement.getServiceDiscovery());
        }
        elements.addAll(list);
        initialized = true;
    }

    @Override
    public void initialize(StorkServiceRegistry stork) {
        this.stork = stork;

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy