Adds API and implementation of ReactiveLoadBalancer
API lives in spring-cloud-commons module and implementation in spring-cloud-loadbalancer.
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright 2013-2018 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
|
||||
*
|
||||
* 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 org.springframework.cloud.context.named;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Spliterator;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Special ObjectProvider that allows the actual ObjectProvider to be resolved
|
||||
* later because of the creation of the named child context.
|
||||
* @param <T>
|
||||
*/
|
||||
class ClientFactoryObjectProvider<T> implements ObjectProvider<T> {
|
||||
|
||||
private final NamedContextFactory clientFactory;
|
||||
private final String name;
|
||||
private final Class<T> type;
|
||||
private ObjectProvider<T> provider;
|
||||
|
||||
public ClientFactoryObjectProvider(NamedContextFactory clientFactory, String name, Class<T> type) {
|
||||
this.clientFactory = clientFactory;
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getObject(Object... args) throws BeansException {
|
||||
return delegate().getObject(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public T getIfAvailable() throws BeansException {
|
||||
return delegate().getIfAvailable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getIfAvailable(Supplier<T> defaultSupplier) throws BeansException {
|
||||
return delegate().getIfAvailable(defaultSupplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ifAvailable(Consumer<T> dependencyConsumer) throws BeansException {
|
||||
delegate().ifAvailable(dependencyConsumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public T getIfUnique() throws BeansException {
|
||||
return delegate().getIfUnique();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getIfUnique(Supplier<T> defaultSupplier) throws BeansException {
|
||||
return delegate().getIfUnique(defaultSupplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ifUnique(Consumer<T> dependencyConsumer) throws BeansException {
|
||||
delegate().ifUnique(dependencyConsumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return delegate().iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<T> stream() {
|
||||
return delegate().stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getObject() throws BeansException {
|
||||
return delegate().getObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(Consumer<? super T> action) {
|
||||
delegate().forEach(action);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Spliterator<T> spliterator() {
|
||||
return delegate().spliterator();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private ObjectProvider<T> delegate() {
|
||||
if (this.provider == null) {
|
||||
provider = this.clientFactory.getProvider(name, type);
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
}
|
||||
@@ -11,10 +11,12 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
|
||||
/**
|
||||
@@ -26,6 +28,7 @@ import org.springframework.core.env.MapPropertySource;
|
||||
* @author Spencer Gibb
|
||||
* @author Dave Syer
|
||||
*/
|
||||
//TODO: add javadoc
|
||||
public abstract class NamedContextFactory<C extends NamedContextFactory.Specification>
|
||||
implements DisposableBean, ApplicationContextAware {
|
||||
|
||||
@@ -131,6 +134,35 @@ public abstract class NamedContextFactory<C extends NamedContextFactory.Specific
|
||||
return null;
|
||||
}
|
||||
|
||||
public <T> ObjectProvider<T> getLazyProvider(String name, Class<T> type) {
|
||||
return new ClientFactoryObjectProvider<>(this, name, type);
|
||||
}
|
||||
|
||||
public <T> ObjectProvider<T> getProvider(String name, Class<T> type) {
|
||||
AnnotationConfigApplicationContext context = getContext(name);
|
||||
return context.getBeanProvider(type);
|
||||
}
|
||||
|
||||
public <T> T getInstance(String name, Class<?> clazz, Class<?>... generics) {
|
||||
ResolvableType type = ResolvableType.forClassWithGenerics(clazz, generics);
|
||||
return getInstance(name, type);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getInstance(String name, ResolvableType type) {
|
||||
AnnotationConfigApplicationContext context = getContext(name);
|
||||
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,
|
||||
type);
|
||||
if (beanNames.length > 0) {
|
||||
for (String beanName : beanNames) {
|
||||
if (context.isTypeMatch(beanName, type)) {
|
||||
return (T) context.getBean(beanName);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public <T> Map<String, T> getInstances(String name, Class<T> type) {
|
||||
AnnotationConfigApplicationContext context = getContext(name);
|
||||
if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,
|
||||
|
||||
Reference in New Issue
Block a user