add task sample

This commit is contained in:
markfisher
2017-05-17 22:35:10 -04:00
parent 120d2da496
commit 56b9be9b6e
7 changed files with 181 additions and 45 deletions

View File

@@ -16,6 +16,7 @@
<module>spring-cloud-function-sample</module>
<module>spring-cloud-function-sample-pojo</module>
<module>spring-cloud-function-sample-compiler</module>
<module>spring-cloud-function-sample-task</module>
</modules>
</project>

View File

@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>function-sample-task</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-cloud-function-sample-task</name>
<description>Spring Cloud Function Task Support</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud-function.version>1.0.0.BUILD-SNAPSHOT</spring-cloud-function.version>
<wrapper.version>1.0.1.RELEASE</wrapper.version>
<reactor.version>3.0.6.RELEASE</reactor.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-task</artifactId>
<version>${spring-cloud-function.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-context</artifactId>
<version>${spring-cloud-function.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-compiler</artifactId>
<version>${spring-cloud-function.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>${wrapper.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2017 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 com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SampleApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleApplication.class, args);
}
}

View File

@@ -0,0 +1,18 @@
spring:
cloud:
function:
task:
supplier: words
function: uppercase
consumer: print
compile:
words:
type: supplier
lambda: ()->Flux.just("hello","world")
uppercase:
lambda: s->s.toUpperCase()
inputType: String
outputType: String
print:
type: consumer
lambda: System.out::println

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2017 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.
@@ -16,8 +16,6 @@
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;
@@ -38,62 +36,28 @@ import reactor.core.publisher.Flux;
*/
@Configuration
@EnableTask
@EnableConfigurationProperties(LambdaConfigurationProperties.class)
@EnableConfigurationProperties(TaskConfigurationProperties.class)
@ConditionalOnClass({ EnableTask.class })
public class TaskConfiguration {
@Autowired
private LambdaConfigurationProperties properties;
private TaskConfigurationProperties properties;
@Bean
public CommandLineRunner commandLineRunner(FunctionCatalog registry) {
final Supplier<Flux<Object>> supplier = registry
.lookupSupplier(properties.getSupplier());
String functionName = properties.getFunction();
Function<Flux<Object>, Flux<Object>> function = registry
.lookupFunction(functionName);
final Consumer<Object> consumer = registry
final Function<Flux<Object>, Flux<Object>> function = registry
.lookupFunction(properties.getFunction());
final Consumer<Flux<Object>> 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();
consumer.accept(function.apply(supplier.get()));
}
};
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();
}
}
}

View File

@@ -21,8 +21,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author Mark Fisher
*/
@ConfigurationProperties(prefix = "lambda")
public class LambdaConfigurationProperties {
@ConfigurationProperties(prefix = "spring.cloud.function.task")
public class TaskConfigurationProperties {
private String supplier;