add support for Supplier and Consumer
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.task;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "lambda")
|
||||
public class LambdaConfigurationProperties {
|
||||
|
||||
private String supplier;
|
||||
|
||||
private String function;
|
||||
|
||||
private String consumer;
|
||||
|
||||
public String getSupplier() {
|
||||
return supplier;
|
||||
}
|
||||
|
||||
public void setSupplier(String supplier) {
|
||||
this.supplier = supplier;
|
||||
}
|
||||
|
||||
public String getFunction() {
|
||||
return function;
|
||||
}
|
||||
|
||||
public void setFunction(String function) {
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
public String getConsumer() {
|
||||
return consumer;
|
||||
}
|
||||
|
||||
public void setConsumer(String consumer) {
|
||||
this.consumer = consumer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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.task;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class TaskApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TaskApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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.task;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.function.registry.FileSystemFunctionRegistry;
|
||||
import org.springframework.cloud.function.registry.FunctionRegistry;
|
||||
import org.springframework.cloud.task.configuration.EnableTask;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
@Configuration
|
||||
@EnableTask
|
||||
@EnableConfigurationProperties(LambdaConfigurationProperties.class)
|
||||
public class TaskConfiguration {
|
||||
|
||||
@Autowired
|
||||
private LambdaConfigurationProperties properties;
|
||||
|
||||
@Bean
|
||||
public FunctionRegistry registry() {
|
||||
return new FileSystemFunctionRegistry();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandLineRunner commandLineRunner(FunctionRegistry registry) {
|
||||
final Supplier<Flux<String>> supplier = registry.lookupSupplier(properties.getSupplier());
|
||||
final Function<Flux<String>, Flux<String>> function = registry.lookupFunction(properties.getFunction());
|
||||
final Consumer<String> consumer = registry.lookupConsumer(properties.getConsumer());
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
final AtomicBoolean status = new AtomicBoolean();
|
||||
CommandLineRunner runner = new CommandLineRunner() {
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
function.apply(supplier.get()).subscribe(consumer,
|
||||
new CompletionConsumer(latch, status, false),
|
||||
new CompletionConsumer(latch, status, true));
|
||||
latch.await();
|
||||
}
|
||||
};
|
||||
return runner;
|
||||
}
|
||||
|
||||
private static class CompletionConsumer implements Consumer<Throwable>, Runnable {
|
||||
|
||||
private final CountDownLatch latch;
|
||||
|
||||
private final AtomicBoolean status;
|
||||
|
||||
private final boolean value;
|
||||
|
||||
private CompletionConsumer(CountDownLatch latch, AtomicBoolean status, boolean value) {
|
||||
this.latch = latch;
|
||||
this.status = status;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(Throwable t) {
|
||||
System.err.println("task failed: " + t);
|
||||
this.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
this.status.set(this.value);
|
||||
this.latch.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user