remove unused code
This commit is contained in:
@@ -1,36 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2016 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.function.gateway;
|
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
import org.reactivestreams.Publisher;
|
|
||||||
import org.springframework.scheduling.Trigger;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Mark Fisher
|
|
||||||
*/
|
|
||||||
public interface FunctionGateway {
|
|
||||||
|
|
||||||
<T, R> R invoke(String functionName, T request);
|
|
||||||
|
|
||||||
<T, R> void schedule(String functionName, Trigger trigger, Supplier<T> supplier, Consumer<R> consumer);
|
|
||||||
|
|
||||||
<T, R> void subscribe(Publisher<T> publisher, String functionName, final Consumer<R> consumer);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2016 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.function.gateway;
|
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
import org.reactivestreams.Publisher;
|
|
||||||
import org.reactivestreams.Subscriber;
|
|
||||||
import org.reactivestreams.Subscription;
|
|
||||||
|
|
||||||
import org.springframework.cloud.function.invoker.FunctionInvokingRunnable;
|
|
||||||
import org.springframework.cloud.function.registry.FunctionCatalog;
|
|
||||||
import org.springframework.scheduling.TaskScheduler;
|
|
||||||
import org.springframework.scheduling.Trigger;
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Mark Fisher
|
|
||||||
*/
|
|
||||||
public class LocalFunctionGateway implements FunctionGateway {
|
|
||||||
|
|
||||||
private final FunctionCatalog catalog;
|
|
||||||
|
|
||||||
private final TaskScheduler scheduler;
|
|
||||||
|
|
||||||
public LocalFunctionGateway(FunctionCatalog catalog, TaskScheduler scheduler) {
|
|
||||||
Assert.notNull(catalog, "FunctionCatalog must not be null");
|
|
||||||
Assert.notNull(scheduler, "TaskScheduler must not be null");
|
|
||||||
this.catalog = catalog;
|
|
||||||
this.scheduler = scheduler;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T, R> R invoke(String functionName, T request) {
|
|
||||||
Function<T, R> function = this.catalog.lookupFunction(functionName);
|
|
||||||
return function.apply(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T, R> void schedule(String functionName, Trigger trigger,
|
|
||||||
Supplier<T> supplier, Consumer<R> consumer) {
|
|
||||||
Function<T, R> function = this.catalog.lookupFunction(functionName);
|
|
||||||
this.scheduler.schedule(
|
|
||||||
new FunctionInvokingRunnable<T, R>(supplier, function, consumer),
|
|
||||||
trigger);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T, R> void subscribe(Publisher<T> publisher, String functionName,
|
|
||||||
final Consumer<R> consumer) {
|
|
||||||
final Function<T, R> function = this.catalog.lookupFunction(functionName);
|
|
||||||
publisher.subscribe(new Subscriber<T>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onComplete() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(Throwable error) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onNext(T next) {
|
|
||||||
if (consumer != null) {
|
|
||||||
consumer.accept(function.apply(next));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
function.apply(next);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSubscribe(Subscription subscription) {
|
|
||||||
subscription.request(Long.MAX_VALUE);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2016 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.function.invoker;
|
|
||||||
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Mark Fisher
|
|
||||||
*
|
|
||||||
* @param <T> function parameter type
|
|
||||||
* @param <R> function return type
|
|
||||||
*/
|
|
||||||
public abstract class AbstractFunctionInvoker<T, R> {
|
|
||||||
|
|
||||||
private final Function<T, R> function;
|
|
||||||
|
|
||||||
protected AbstractFunctionInvoker(Function<T, R> function) {
|
|
||||||
Assert.notNull(function, "Function must not be null");
|
|
||||||
this.function = function;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected R doInvoke(T input) {
|
|
||||||
return this.function.apply(input);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2016 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.function.invoker;
|
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Mark Fisher
|
|
||||||
*
|
|
||||||
* @param <T> output of supplier, input to function
|
|
||||||
* @param <R> output of function, input to consumer
|
|
||||||
*/
|
|
||||||
public class FunctionInvokingRunnable<T, R> implements Runnable {
|
|
||||||
|
|
||||||
private final Supplier<T> supplier;
|
|
||||||
|
|
||||||
private final Function<T, R> function;
|
|
||||||
|
|
||||||
private final Consumer<R> consumer;
|
|
||||||
|
|
||||||
public FunctionInvokingRunnable(Supplier<T> supplier, Function<T, R> function, Consumer<R> consumer) {
|
|
||||||
this.supplier = supplier;
|
|
||||||
this.function = function;
|
|
||||||
this.consumer = consumer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
this.consumer.accept(this.function.apply(this.supplier.get()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2016 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.function.gateway;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import org.springframework.cloud.function.registry.FunctionCatalog;
|
|
||||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
|
||||||
|
|
||||||
import reactor.core.publisher.Flux;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Mark Fisher
|
|
||||||
*/
|
|
||||||
public class LocalFunctionGatewayTests {
|
|
||||||
|
|
||||||
private final FunctionCatalog catalog = new FunctionCatalog() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> Supplier<T> lookupSupplier(String name) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public Function<Flux<String>, Flux<String>> lookupFunction(String name) {
|
|
||||||
return ("uppercase".equals(name) ? f->f.map(s->s.toString().toUpperCase()) : null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> Consumer<T> lookupConsumer(String name) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private final ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void init() {
|
|
||||||
this.scheduler.initialize();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test() {
|
|
||||||
LocalFunctionGateway gateway = new LocalFunctionGateway(catalog, scheduler);
|
|
||||||
Flux<String> output = gateway.invoke("uppercase", Flux.just("foo", "bar"));
|
|
||||||
List<String> results = output.collectList().block();
|
|
||||||
assertEquals("FOO", results.get(0));
|
|
||||||
assertEquals("BAR", results.get(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testMultiple() {
|
|
||||||
for (int i = 0; i < 100; i++) {
|
|
||||||
test();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user