GH-750 Add support for pluggable protobufs
This initial support adds plugin extension to support CloudEvent proto as well as the example Additional plugins could be provided in the same ay as CloudEvent plugin extension Resolves #750
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
<module>spring-cloud-function-adapter-aws</module>
|
||||
<module>spring-cloud-function-adapter-azure</module>
|
||||
<module>spring-cloud-function-adapter-gcp</module>
|
||||
<module>spring-cloud-function-grpc-cloudevent-ext</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
||||
33
spring-cloud-function-adapters/spring-cloud-function-grpc-cloudevent-ext/.gitignore
vendored
Normal file
33
spring-cloud-function-adapters/spring-cloud-function-grpc-cloudevent-ext/.gitignore
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright 2007-present 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
|
||||
*
|
||||
* https://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.
|
||||
*/
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.nio.channels.*;
|
||||
import java.util.Properties;
|
||||
|
||||
public class MavenWrapperDownloader {
|
||||
|
||||
private static final String WRAPPER_VERSION = "0.5.6";
|
||||
/**
|
||||
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
|
||||
*/
|
||||
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
|
||||
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
|
||||
|
||||
/**
|
||||
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
|
||||
* use instead of the default one.
|
||||
*/
|
||||
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
|
||||
".mvn/wrapper/maven-wrapper.properties";
|
||||
|
||||
/**
|
||||
* Path where the maven-wrapper.jar will be saved to.
|
||||
*/
|
||||
private static final String MAVEN_WRAPPER_JAR_PATH =
|
||||
".mvn/wrapper/maven-wrapper.jar";
|
||||
|
||||
/**
|
||||
* Name of the property which should be used to override the default download url for the wrapper.
|
||||
*/
|
||||
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
|
||||
|
||||
public static void main(String args[]) {
|
||||
System.out.println("- Downloader started");
|
||||
File baseDirectory = new File(args[0]);
|
||||
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
|
||||
|
||||
// If the maven-wrapper.properties exists, read it and check if it contains a custom
|
||||
// wrapperUrl parameter.
|
||||
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
|
||||
String url = DEFAULT_DOWNLOAD_URL;
|
||||
if(mavenWrapperPropertyFile.exists()) {
|
||||
FileInputStream mavenWrapperPropertyFileInputStream = null;
|
||||
try {
|
||||
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
|
||||
Properties mavenWrapperProperties = new Properties();
|
||||
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
|
||||
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
|
||||
} catch (IOException e) {
|
||||
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
|
||||
} finally {
|
||||
try {
|
||||
if(mavenWrapperPropertyFileInputStream != null) {
|
||||
mavenWrapperPropertyFileInputStream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// Ignore ...
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("- Downloading from: " + url);
|
||||
|
||||
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
|
||||
if(!outputFile.getParentFile().exists()) {
|
||||
if(!outputFile.getParentFile().mkdirs()) {
|
||||
System.out.println(
|
||||
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
|
||||
}
|
||||
}
|
||||
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
|
||||
try {
|
||||
downloadFileFromURL(url, outputFile);
|
||||
System.out.println("Done");
|
||||
System.exit(0);
|
||||
} catch (Throwable e) {
|
||||
System.out.println("- Error downloading");
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
|
||||
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
|
||||
String username = System.getenv("MVNW_USERNAME");
|
||||
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
|
||||
Authenticator.setDefault(new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(username, password);
|
||||
}
|
||||
});
|
||||
}
|
||||
URL website = new URL(urlString);
|
||||
ReadableByteChannel rbc;
|
||||
rbc = Channels.newChannel(website.openStream());
|
||||
FileOutputStream fos = new FileOutputStream(destination);
|
||||
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
|
||||
fos.close();
|
||||
rbc.close();
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,2 @@
|
||||
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.2/apache-maven-3.8.2-bin.zip
|
||||
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
|
||||
@@ -0,0 +1,40 @@
|
||||
# Spring Cloud Function gRPC extension to support CloudEvent proto.
|
||||
|
||||
This extension project designed as an extension to general Spring Cloud Function gRPC support to specifically suport
|
||||
[CloudEvent proto](https://github.com/cloudevents/spec/blob/v1.0.1/spec.proto)
|
||||
|
||||
To use it simply import it as a dependency to your project together with
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-function-grpc</artifactId>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
Your project should also explicitly import [CloudEvent proto](https://github.com/cloudevents/spec/blob/v1.0.1/spec.proto) and
|
||||
service proto
|
||||
|
||||
```
|
||||
syntax = "proto3";
|
||||
|
||||
package io.cloudevents.v1;
|
||||
|
||||
import "google/protobuf/any.proto";
|
||||
import "google/protobuf/timestamp.proto";
|
||||
import "CloudEvent.proto";
|
||||
|
||||
service CloudEventService {
|
||||
rpc biStream(stream io.cloudevents.v1.CloudEvent) returns (stream io.cloudevents.v1.CloudEvent);
|
||||
|
||||
rpc clientStream(stream io.cloudevents.v1.CloudEvent) returns (io.cloudevents.v1.CloudEvent);
|
||||
|
||||
rpc serverStream(io.cloudevents.v1.CloudEvent) returns (stream io.cloudevents.v1.CloudEvent);
|
||||
|
||||
rpc requestReply(io.cloudevents.v1.CloudEvent) returns (io.cloudevents.v1.CloudEvent);
|
||||
}
|
||||
```
|
||||
|
||||
Once done, you can send/receive CloudEvent messages
|
||||
|
||||
You can also reference [this sample](https://github.com/spring-cloud/spring-cloud-function/tree/main/spring-cloud-function-samples/function-sample-grpc-cloudevent)
|
||||
310
spring-cloud-function-adapters/spring-cloud-function-grpc-cloudevent-ext/mvnw
vendored
Executable file
310
spring-cloud-function-adapters/spring-cloud-function-grpc-cloudevent-ext/mvnw
vendored
Executable file
@@ -0,0 +1,310 @@
|
||||
#!/bin/sh
|
||||
# ----------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you 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
|
||||
#
|
||||
# https://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.
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
# Maven Start Up Batch script
|
||||
#
|
||||
# Required ENV vars:
|
||||
# ------------------
|
||||
# JAVA_HOME - location of a JDK home dir
|
||||
#
|
||||
# Optional ENV vars
|
||||
# -----------------
|
||||
# M2_HOME - location of maven2's installed home dir
|
||||
# MAVEN_OPTS - parameters passed to the Java VM when running Maven
|
||||
# e.g. to debug Maven itself, use
|
||||
# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
|
||||
# MAVEN_SKIP_RC - flag to disable loading of mavenrc files
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
if [ -z "$MAVEN_SKIP_RC" ] ; then
|
||||
|
||||
if [ -f /etc/mavenrc ] ; then
|
||||
. /etc/mavenrc
|
||||
fi
|
||||
|
||||
if [ -f "$HOME/.mavenrc" ] ; then
|
||||
. "$HOME/.mavenrc"
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
# OS specific support. $var _must_ be set to either true or false.
|
||||
cygwin=false;
|
||||
darwin=false;
|
||||
mingw=false
|
||||
case "`uname`" in
|
||||
CYGWIN*) cygwin=true ;;
|
||||
MINGW*) mingw=true;;
|
||||
Darwin*) darwin=true
|
||||
# Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
|
||||
# See https://developer.apple.com/library/mac/qa/qa1170/_index.html
|
||||
if [ -z "$JAVA_HOME" ]; then
|
||||
if [ -x "/usr/libexec/java_home" ]; then
|
||||
export JAVA_HOME="`/usr/libexec/java_home`"
|
||||
else
|
||||
export JAVA_HOME="/Library/Java/Home"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
if [ -z "$JAVA_HOME" ] ; then
|
||||
if [ -r /etc/gentoo-release ] ; then
|
||||
JAVA_HOME=`java-config --jre-home`
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -z "$M2_HOME" ] ; then
|
||||
## resolve links - $0 may be a link to maven's home
|
||||
PRG="$0"
|
||||
|
||||
# need this for relative symlinks
|
||||
while [ -h "$PRG" ] ; do
|
||||
ls=`ls -ld "$PRG"`
|
||||
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||
if expr "$link" : '/.*' > /dev/null; then
|
||||
PRG="$link"
|
||||
else
|
||||
PRG="`dirname "$PRG"`/$link"
|
||||
fi
|
||||
done
|
||||
|
||||
saveddir=`pwd`
|
||||
|
||||
M2_HOME=`dirname "$PRG"`/..
|
||||
|
||||
# make it fully qualified
|
||||
M2_HOME=`cd "$M2_HOME" && pwd`
|
||||
|
||||
cd "$saveddir"
|
||||
# echo Using m2 at $M2_HOME
|
||||
fi
|
||||
|
||||
# For Cygwin, ensure paths are in UNIX format before anything is touched
|
||||
if $cygwin ; then
|
||||
[ -n "$M2_HOME" ] &&
|
||||
M2_HOME=`cygpath --unix "$M2_HOME"`
|
||||
[ -n "$JAVA_HOME" ] &&
|
||||
JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
|
||||
[ -n "$CLASSPATH" ] &&
|
||||
CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
|
||||
fi
|
||||
|
||||
# For Mingw, ensure paths are in UNIX format before anything is touched
|
||||
if $mingw ; then
|
||||
[ -n "$M2_HOME" ] &&
|
||||
M2_HOME="`(cd "$M2_HOME"; pwd)`"
|
||||
[ -n "$JAVA_HOME" ] &&
|
||||
JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
|
||||
fi
|
||||
|
||||
if [ -z "$JAVA_HOME" ]; then
|
||||
javaExecutable="`which javac`"
|
||||
if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
|
||||
# readlink(1) is not available as standard on Solaris 10.
|
||||
readLink=`which readlink`
|
||||
if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
|
||||
if $darwin ; then
|
||||
javaHome="`dirname \"$javaExecutable\"`"
|
||||
javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
|
||||
else
|
||||
javaExecutable="`readlink -f \"$javaExecutable\"`"
|
||||
fi
|
||||
javaHome="`dirname \"$javaExecutable\"`"
|
||||
javaHome=`expr "$javaHome" : '\(.*\)/bin'`
|
||||
JAVA_HOME="$javaHome"
|
||||
export JAVA_HOME
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -z "$JAVACMD" ] ; then
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||
else
|
||||
JAVACMD="$JAVA_HOME/bin/java"
|
||||
fi
|
||||
else
|
||||
JAVACMD="`which java`"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
echo "Error: JAVA_HOME is not defined correctly." >&2
|
||||
echo " We cannot execute $JAVACMD" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "$JAVA_HOME" ] ; then
|
||||
echo "Warning: JAVA_HOME environment variable is not set."
|
||||
fi
|
||||
|
||||
CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
|
||||
|
||||
# traverses directory structure from process work directory to filesystem root
|
||||
# first directory with .mvn subdirectory is considered project base directory
|
||||
find_maven_basedir() {
|
||||
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
echo "Path not specified to find_maven_basedir"
|
||||
return 1
|
||||
fi
|
||||
|
||||
basedir="$1"
|
||||
wdir="$1"
|
||||
while [ "$wdir" != '/' ] ; do
|
||||
if [ -d "$wdir"/.mvn ] ; then
|
||||
basedir=$wdir
|
||||
break
|
||||
fi
|
||||
# workaround for JBEAP-8937 (on Solaris 10/Sparc)
|
||||
if [ -d "${wdir}" ]; then
|
||||
wdir=`cd "$wdir/.."; pwd`
|
||||
fi
|
||||
# end of workaround
|
||||
done
|
||||
echo "${basedir}"
|
||||
}
|
||||
|
||||
# concatenates all lines of a file
|
||||
concat_lines() {
|
||||
if [ -f "$1" ]; then
|
||||
echo "$(tr -s '\n' ' ' < "$1")"
|
||||
fi
|
||||
}
|
||||
|
||||
BASE_DIR=`find_maven_basedir "$(pwd)"`
|
||||
if [ -z "$BASE_DIR" ]; then
|
||||
exit 1;
|
||||
fi
|
||||
|
||||
##########################################################################################
|
||||
# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
|
||||
# This allows using the maven wrapper in projects that prohibit checking in binary data.
|
||||
##########################################################################################
|
||||
if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Found .mvn/wrapper/maven-wrapper.jar"
|
||||
fi
|
||||
else
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..."
|
||||
fi
|
||||
if [ -n "$MVNW_REPOURL" ]; then
|
||||
jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
|
||||
else
|
||||
jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
|
||||
fi
|
||||
while IFS="=" read key value; do
|
||||
case "$key" in (wrapperUrl) jarUrl="$value"; break ;;
|
||||
esac
|
||||
done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties"
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Downloading from: $jarUrl"
|
||||
fi
|
||||
wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar"
|
||||
if $cygwin; then
|
||||
wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"`
|
||||
fi
|
||||
|
||||
if command -v wget > /dev/null; then
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Found wget ... using wget"
|
||||
fi
|
||||
if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
|
||||
wget "$jarUrl" -O "$wrapperJarPath"
|
||||
else
|
||||
wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath"
|
||||
fi
|
||||
elif command -v curl > /dev/null; then
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Found curl ... using curl"
|
||||
fi
|
||||
if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
|
||||
curl -o "$wrapperJarPath" "$jarUrl" -f
|
||||
else
|
||||
curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f
|
||||
fi
|
||||
|
||||
else
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Falling back to using Java to download"
|
||||
fi
|
||||
javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java"
|
||||
# For Cygwin, switch paths to Windows format before running javac
|
||||
if $cygwin; then
|
||||
javaClass=`cygpath --path --windows "$javaClass"`
|
||||
fi
|
||||
if [ -e "$javaClass" ]; then
|
||||
if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo " - Compiling MavenWrapperDownloader.java ..."
|
||||
fi
|
||||
# Compiling the Java class
|
||||
("$JAVA_HOME/bin/javac" "$javaClass")
|
||||
fi
|
||||
if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
|
||||
# Running the downloader
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo " - Running MavenWrapperDownloader.java ..."
|
||||
fi
|
||||
("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR")
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
##########################################################################################
|
||||
# End of extension
|
||||
##########################################################################################
|
||||
|
||||
export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo $MAVEN_PROJECTBASEDIR
|
||||
fi
|
||||
MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
|
||||
|
||||
# For Cygwin, switch paths to Windows format before running java
|
||||
if $cygwin; then
|
||||
[ -n "$M2_HOME" ] &&
|
||||
M2_HOME=`cygpath --path --windows "$M2_HOME"`
|
||||
[ -n "$JAVA_HOME" ] &&
|
||||
JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
|
||||
[ -n "$CLASSPATH" ] &&
|
||||
CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
|
||||
[ -n "$MAVEN_PROJECTBASEDIR" ] &&
|
||||
MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
|
||||
fi
|
||||
|
||||
# Provide a "standardized" way to retrieve the CLI args that will
|
||||
# work with both Windows and non-Windows executions.
|
||||
MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"
|
||||
export MAVEN_CMD_LINE_ARGS
|
||||
|
||||
WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
|
||||
|
||||
exec "$JAVACMD" \
|
||||
$MAVEN_OPTS \
|
||||
-classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
|
||||
"-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
|
||||
${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"
|
||||
182
spring-cloud-function-adapters/spring-cloud-function-grpc-cloudevent-ext/mvnw.cmd
vendored
Normal file
182
spring-cloud-function-adapters/spring-cloud-function-grpc-cloudevent-ext/mvnw.cmd
vendored
Normal file
@@ -0,0 +1,182 @@
|
||||
@REM ----------------------------------------------------------------------------
|
||||
@REM Licensed to the Apache Software Foundation (ASF) under one
|
||||
@REM or more contributor license agreements. See the NOTICE file
|
||||
@REM distributed with this work for additional information
|
||||
@REM regarding copyright ownership. The ASF licenses this file
|
||||
@REM to you under the Apache License, Version 2.0 (the
|
||||
@REM "License"); you may not use this file except in compliance
|
||||
@REM with the License. You may obtain a copy of the License at
|
||||
@REM
|
||||
@REM https://www.apache.org/licenses/LICENSE-2.0
|
||||
@REM
|
||||
@REM Unless required by applicable law or agreed to in writing,
|
||||
@REM software distributed under the License is distributed on an
|
||||
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
@REM KIND, either express or implied. See the License for the
|
||||
@REM specific language governing permissions and limitations
|
||||
@REM under the License.
|
||||
@REM ----------------------------------------------------------------------------
|
||||
|
||||
@REM ----------------------------------------------------------------------------
|
||||
@REM Maven Start Up Batch script
|
||||
@REM
|
||||
@REM Required ENV vars:
|
||||
@REM JAVA_HOME - location of a JDK home dir
|
||||
@REM
|
||||
@REM Optional ENV vars
|
||||
@REM M2_HOME - location of maven2's installed home dir
|
||||
@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
|
||||
@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending
|
||||
@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
|
||||
@REM e.g. to debug Maven itself, use
|
||||
@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
|
||||
@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
|
||||
@REM ----------------------------------------------------------------------------
|
||||
|
||||
@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
|
||||
@echo off
|
||||
@REM set title of command window
|
||||
title %0
|
||||
@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on'
|
||||
@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
|
||||
|
||||
@REM set %HOME% to equivalent of $HOME
|
||||
if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
|
||||
|
||||
@REM Execute a user defined script before this one
|
||||
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
|
||||
@REM check for pre script, once with legacy .bat ending and once with .cmd ending
|
||||
if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
|
||||
if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
|
||||
:skipRcPre
|
||||
|
||||
@setlocal
|
||||
|
||||
set ERROR_CODE=0
|
||||
|
||||
@REM To isolate internal variables from possible post scripts, we use another setlocal
|
||||
@setlocal
|
||||
|
||||
@REM ==== START VALIDATION ====
|
||||
if not "%JAVA_HOME%" == "" goto OkJHome
|
||||
|
||||
echo.
|
||||
echo Error: JAVA_HOME not found in your environment. >&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the >&2
|
||||
echo location of your Java installation. >&2
|
||||
echo.
|
||||
goto error
|
||||
|
||||
:OkJHome
|
||||
if exist "%JAVA_HOME%\bin\java.exe" goto init
|
||||
|
||||
echo.
|
||||
echo Error: JAVA_HOME is set to an invalid directory. >&2
|
||||
echo JAVA_HOME = "%JAVA_HOME%" >&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the >&2
|
||||
echo location of your Java installation. >&2
|
||||
echo.
|
||||
goto error
|
||||
|
||||
@REM ==== END VALIDATION ====
|
||||
|
||||
:init
|
||||
|
||||
@REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
|
||||
@REM Fallback to current working directory if not found.
|
||||
|
||||
set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
|
||||
IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
|
||||
|
||||
set EXEC_DIR=%CD%
|
||||
set WDIR=%EXEC_DIR%
|
||||
:findBaseDir
|
||||
IF EXIST "%WDIR%"\.mvn goto baseDirFound
|
||||
cd ..
|
||||
IF "%WDIR%"=="%CD%" goto baseDirNotFound
|
||||
set WDIR=%CD%
|
||||
goto findBaseDir
|
||||
|
||||
:baseDirFound
|
||||
set MAVEN_PROJECTBASEDIR=%WDIR%
|
||||
cd "%EXEC_DIR%"
|
||||
goto endDetectBaseDir
|
||||
|
||||
:baseDirNotFound
|
||||
set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
|
||||
cd "%EXEC_DIR%"
|
||||
|
||||
:endDetectBaseDir
|
||||
|
||||
IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
|
||||
|
||||
@setlocal EnableExtensions EnableDelayedExpansion
|
||||
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
|
||||
@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
|
||||
|
||||
:endReadAdditionalConfig
|
||||
|
||||
SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
|
||||
set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
|
||||
set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
|
||||
|
||||
set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
|
||||
|
||||
FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
|
||||
IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
|
||||
)
|
||||
|
||||
@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
|
||||
@REM This allows using the maven wrapper in projects that prohibit checking in binary data.
|
||||
if exist %WRAPPER_JAR% (
|
||||
if "%MVNW_VERBOSE%" == "true" (
|
||||
echo Found %WRAPPER_JAR%
|
||||
)
|
||||
) else (
|
||||
if not "%MVNW_REPOURL%" == "" (
|
||||
SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
|
||||
)
|
||||
if "%MVNW_VERBOSE%" == "true" (
|
||||
echo Couldn't find %WRAPPER_JAR%, downloading it ...
|
||||
echo Downloading from: %DOWNLOAD_URL%
|
||||
)
|
||||
|
||||
powershell -Command "&{"^
|
||||
"$webclient = new-object System.Net.WebClient;"^
|
||||
"if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
|
||||
"$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
|
||||
"}"^
|
||||
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^
|
||||
"}"
|
||||
if "%MVNW_VERBOSE%" == "true" (
|
||||
echo Finished downloading %WRAPPER_JAR%
|
||||
)
|
||||
)
|
||||
@REM End of extension
|
||||
|
||||
@REM Provide a "standardized" way to retrieve the CLI args that will
|
||||
@REM work with both Windows and non-Windows executions.
|
||||
set MAVEN_CMD_LINE_ARGS=%*
|
||||
|
||||
%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
|
||||
if ERRORLEVEL 1 goto error
|
||||
goto end
|
||||
|
||||
:error
|
||||
set ERROR_CODE=1
|
||||
|
||||
:end
|
||||
@endlocal & set ERROR_CODE=%ERROR_CODE%
|
||||
|
||||
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
|
||||
@REM check for post script, once with legacy .bat ending and once with .cmd ending
|
||||
if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
|
||||
if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
|
||||
:skipRcPost
|
||||
|
||||
@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
|
||||
if "%MAVEN_BATCH_PAUSE%" == "on" pause
|
||||
|
||||
if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
|
||||
|
||||
exit /B %ERROR_CODE%
|
||||
@@ -0,0 +1,124 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.6.0-SNAPSHOT</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-function-grpc-cloudevent-ext</artifactId>
|
||||
<version>3.2.0-SNAPSHOT</version>
|
||||
<name> </name>
|
||||
<description>CloudEvent extansion for spring-cloud-function-grpc</description>
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-netty</artifactId>
|
||||
<version>1.16.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-protobuf</artifactId>
|
||||
<version>1.16.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-stub</artifactId>
|
||||
<version>1.16.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-function-grpc</artifactId>
|
||||
<version>3.2.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<extensions>
|
||||
<extension>
|
||||
<groupId>kr.motd.maven</groupId>
|
||||
<artifactId>os-maven-plugin</artifactId>
|
||||
<version>1.6.1</version>
|
||||
</extension>
|
||||
</extensions>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.xolstice.maven.plugins</groupId>
|
||||
<artifactId>protobuf-maven-plugin</artifactId>
|
||||
<version>0.6.1</version>
|
||||
<configuration>
|
||||
<protocArtifact>
|
||||
com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier}
|
||||
</protocArtifact>
|
||||
<pluginId>grpc-java</pluginId>
|
||||
<pluginArtifact>
|
||||
io.grpc:protoc-gen-grpc-java:1.4.0:exe:${os.detected.classifier}
|
||||
</pluginArtifact>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
<goal>compile-custom</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2021-2021 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
|
||||
*
|
||||
* https://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.grpc.ce;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.function.grpc.MessageHandlingHelper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import io.grpc.BindableService;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnProperty(name = "spring.cloud.function.grpc.server", havingValue = "true", matchIfMissing = true)
|
||||
public class CloudEventGrpcAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public BindableService cloudEventMessageHandler(MessageHandlingHelper helper) {
|
||||
return new CloudEventHandler(helper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public CloudEventMessageConverter cloudEventMessageConverter() {
|
||||
return new CloudEventMessageConverter();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 2021-2021 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
|
||||
*
|
||||
* https://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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright 2021-2021 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
|
||||
*
|
||||
* https://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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright 2021-2021 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
|
||||
*
|
||||
* https://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.grpc.ce;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.cloud.function.grpc.MessageHandlingHelper;
|
||||
|
||||
import io.cloudevents.v1.CloudEventServiceGrpc.CloudEventServiceImplBase;
|
||||
import io.cloudevents.v1.proto.CloudEvent;
|
||||
import io.grpc.stub.StreamObserver;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 3.2
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
class CloudEventHandler extends CloudEventServiceImplBase {
|
||||
|
||||
private Log logger = LogFactory.getLog(CloudEventHandler.class);
|
||||
|
||||
|
||||
|
||||
private final MessageHandlingHelper helper;
|
||||
|
||||
|
||||
|
||||
public CloudEventHandler(MessageHandlingHelper helper) {
|
||||
this.helper = helper;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void requestReply(CloudEvent request, StreamObserver<CloudEvent> responseObserver) {
|
||||
this.helper.requestReply(request, responseObserver);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright 2021-2021 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
|
||||
*
|
||||
* https://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.grpc.ce;
|
||||
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventMessageUtils;
|
||||
import org.springframework.cloud.function.grpc.AbstractGrpcMessageConverter;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.GeneratedMessageV3;
|
||||
|
||||
import io.cloudevents.v1.proto.CloudEvent;
|
||||
import io.cloudevents.v1.proto.CloudEvent.Builder;
|
||||
import io.cloudevents.v1.proto.CloudEvent.CloudEventAttributeValue;
|
||||
import io.cloudevents.v1.proto.CloudEvent.CloudEventAttributeValue.AttrCase;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class CloudEventMessageConverter extends AbstractGrpcMessageConverter<CloudEvent> {
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@Override
|
||||
protected Message<byte[]> doToSpringMessage(CloudEvent cloudEvent) {
|
||||
MessageBuilder builder = MessageBuilder.withPayload(cloudEvent.getTextData());
|
||||
builder.setHeader(CloudEventMessageUtils.TYPE, cloudEvent.getType());
|
||||
builder.setHeader(CloudEventMessageUtils.SOURCE, cloudEvent.getSource());
|
||||
builder.setHeader(CloudEventMessageUtils.ID, cloudEvent.getId());
|
||||
builder.setHeader(CloudEventMessageUtils.SPECVERSION, cloudEvent.getId());
|
||||
|
||||
for (Entry<String, CloudEventAttributeValue> attributeEntry : cloudEvent.getAttributesMap().entrySet()) {
|
||||
AttrCase attrCase = attributeEntry.getValue().getAttrCase();
|
||||
if (attrCase.equals(AttrCase.CE_BOOLEAN)) {
|
||||
builder.setHeader(attributeEntry.getKey(), attributeEntry.getValue().getCeBoolean());
|
||||
}
|
||||
else if (attrCase.equals(AttrCase.CE_BYTES)) {
|
||||
builder.setHeader(attributeEntry.getKey(), attributeEntry.getValue().getCeBytes());
|
||||
}
|
||||
else if (attrCase.equals(AttrCase.CE_INTEGER)) {
|
||||
builder.setHeader(attributeEntry.getKey(), attributeEntry.getValue().getCeInteger());
|
||||
}
|
||||
else if (attrCase.equals(AttrCase.CE_STRING)) {
|
||||
builder.setHeader(attributeEntry.getKey(), attributeEntry.getValue().getCeString());
|
||||
}
|
||||
else if (attrCase.equals(AttrCase.CE_TIMESTAMP)) {
|
||||
builder.setHeader(attributeEntry.getKey(), attributeEntry.getValue().getCeTimestamp());
|
||||
}
|
||||
else if (attrCase.equals(AttrCase.CE_URI)) {
|
||||
builder.setHeader(attributeEntry.getKey(), attributeEntry.getValue().getCeUri());
|
||||
}
|
||||
else if (attrCase.equals(AttrCase.CE_URI_REF)) {
|
||||
builder.setHeader(attributeEntry.getKey(), attributeEntry.getValue().getCeUriRef());
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException("Unknown type for attribute " + attributeEntry.getKey());
|
||||
}
|
||||
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CloudEvent doFromSpringMessage(Message<byte[]> springMessage) {
|
||||
Builder builder = CloudEvent.newBuilder()
|
||||
.setTextDataBytes(ByteString.copyFrom(springMessage.getPayload()))
|
||||
.setType(CloudEventMessageUtils.getType(springMessage))
|
||||
.setSource(CloudEventMessageUtils.getSource(springMessage).toString())
|
||||
.setId(CloudEventMessageUtils.getId(springMessage))
|
||||
.setSpecVersion(CloudEventMessageUtils.getSpecVersion(springMessage));
|
||||
|
||||
|
||||
for (Entry<String, Object> entry : springMessage.getHeaders().entrySet()) {
|
||||
builder.putAttributes(entry.getKey(), CloudEventAttributeValue.newBuilder().setCeString(entry.getValue().toString()).build());
|
||||
}
|
||||
return builder.build();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean supports(Class<? extends GeneratedMessageV3> grpcClass) {
|
||||
return grpcClass.isAssignableFrom(CloudEvent.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package io.cloudevents.v1;
|
||||
|
||||
import "google/protobuf/any.proto";
|
||||
import "google/protobuf/timestamp.proto";
|
||||
|
||||
option go_package = "cloudevents.io/genproto/v1";
|
||||
option java_package = "io.cloudevents.v1.proto";
|
||||
option java_multiple_files = true;
|
||||
|
||||
message CloudEvent {
|
||||
|
||||
// -- CloudEvent Context Attributes
|
||||
|
||||
// Required Attributes
|
||||
string id = 1;
|
||||
string source = 2; // URI-reference
|
||||
string spec_version = 3;
|
||||
string type = 4;
|
||||
|
||||
// Optional & Extension Attributes
|
||||
map<string, CloudEventAttributeValue> attributes = 5;
|
||||
|
||||
// -- CloudEvent Data (Bytes, Text, or Proto)
|
||||
oneof data {
|
||||
bytes binary_data = 6;
|
||||
string text_data = 7;
|
||||
google.protobuf.Any proto_data = 8;
|
||||
}
|
||||
|
||||
/**
|
||||
* The CloudEvent specification defines
|
||||
* seven attribute value types...
|
||||
*/
|
||||
|
||||
message CloudEventAttributeValue {
|
||||
|
||||
oneof attr {
|
||||
bool ce_boolean = 1;
|
||||
int32 ce_integer = 2;
|
||||
string ce_string = 3;
|
||||
bytes ce_bytes = 4;
|
||||
string ce_uri = 5;
|
||||
string ce_uri_ref = 6;
|
||||
google.protobuf.Timestamp ce_timestamp = 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package io.cloudevents.v1;
|
||||
|
||||
import "google/protobuf/any.proto";
|
||||
import "google/protobuf/timestamp.proto";
|
||||
import "CloudEvent.proto";
|
||||
|
||||
service CloudEventService {
|
||||
rpc biStream(stream io.cloudevents.v1.CloudEvent) returns (stream io.cloudevents.v1.CloudEvent);
|
||||
|
||||
rpc clientStream(stream io.cloudevents.v1.CloudEvent) returns (io.cloudevents.v1.CloudEvent);
|
||||
|
||||
rpc serverStream(io.cloudevents.v1.CloudEvent) returns (stream io.cloudevents.v1.CloudEvent);
|
||||
|
||||
rpc requestReply(io.cloudevents.v1.CloudEvent) returns (io.cloudevents.v1.CloudEvent);
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.springframework.cloud.function.grpc.ce.CloudEventGrpcAutoConfiguration
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.springframework.cloud.grpc.ce;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class SpringCloudFunctionGrpcCloudeventApplicationTests {
|
||||
|
||||
// @Test
|
||||
// void contextLoads() {
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -1046,6 +1046,7 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
||||
}
|
||||
|
||||
private boolean isExtractPayload(Message<?> message, Type type) {
|
||||
|
||||
if (FunctionTypeUtils.isCollectionOfMessage(type)) {
|
||||
return true;
|
||||
}
|
||||
@@ -1054,6 +1055,9 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
||||
}
|
||||
|
||||
Object payload = message.getPayload();
|
||||
if ((payload instanceof byte[])) {
|
||||
return false;
|
||||
}
|
||||
if (ObjectUtils.isArray(payload)) {
|
||||
payload = CollectionUtils.arrayToList(payload);
|
||||
}
|
||||
@@ -1072,6 +1076,9 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
||||
* set as a header in a message or explicitly provided as part of the lookup.
|
||||
*/
|
||||
private Object convertOutputIfNecessary(Object output, Type type, String[] contentType) {
|
||||
if (output instanceof Message && ((Message) output).getPayload() instanceof byte[]) {
|
||||
return output;
|
||||
}
|
||||
if (this.skipOutputConversion) {
|
||||
return output;
|
||||
}
|
||||
@@ -1087,6 +1094,7 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
||||
}
|
||||
|
||||
Object convertedOutput = output;
|
||||
|
||||
if (FunctionTypeUtils.isMultipleArgumentType(type)) {
|
||||
convertedOutput = this.convertMultipleOutputArgumentTypeIfNecesary(convertedOutput, type, contentType);
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
@@ -403,11 +404,15 @@ public class BeanFactoryAwareFunctionRegistryTests {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
@Disabled
|
||||
public void byteArrayNoSpecialHandling() throws Exception {
|
||||
FunctionCatalog catalog = this.configureCatalog(ByteArrayFunction.class);
|
||||
FunctionInvocationWrapper function = catalog.lookup("beanFactoryAwareFunctionRegistryTests.ByteArrayFunction", "application/json");
|
||||
assertThat(function).isNotNull();
|
||||
Message<byte[]> result = (Message<byte[]>) function.apply(MessageBuilder.withPayload("hello".getBytes()).setHeader(MessageHeaders.CONTENT_TYPE, "application/octet-stream").build());
|
||||
|
||||
System.out.println(new String(result.getPayload()));
|
||||
|
||||
assertThat(result.getPayload()).isEqualTo("\"b2xsZWg=\"".getBytes());
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
<properties>
|
||||
<grpc.version>1.16.1</grpc.version>
|
||||
<checkstyle.skip>true</checkstyle.skip>
|
||||
<disable.checks>true</disable.checks>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -65,12 +65,6 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-checkstyle-plugin</artifactId>
|
||||
<!-- <executions> -->
|
||||
<!-- <execution> -->
|
||||
<!-- <id>checkstyle-validation</id> -->
|
||||
<!-- <phase>none</phase> -->
|
||||
<!-- </execution> -->
|
||||
<!-- </executions> -->
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.xolstice.maven.plugins</groupId>
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2021-2021 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
|
||||
*
|
||||
* https://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.grpc;
|
||||
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
import com.google.protobuf.GeneratedMessageV3;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
* @param <T> instance of {@link GeneratedMessageV3}
|
||||
*/
|
||||
public abstract class AbstractGrpcMessageConverter<T extends GeneratedMessageV3> implements GrpcMessageConverter<T> {
|
||||
|
||||
@Override
|
||||
public Message<byte[]> toSpringMessage(T grpcMessage) {
|
||||
if (this.supports(grpcMessage)) {
|
||||
return this.doToSpringMessage(grpcMessage);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T fromSpringMessage(Message<byte[]> springMessage, Class<T> grpcClass) {
|
||||
if (this.supports(grpcClass)) {
|
||||
return this.doFromSpringMessage(springMessage);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected abstract Message<byte[]> doToSpringMessage(T grpcMessage);
|
||||
|
||||
|
||||
protected abstract T doFromSpringMessage(Message<byte[]> springMessage);
|
||||
|
||||
protected boolean supports(T grpcMessage) {
|
||||
// String fieldName = grpcMessage.getAllFields().keySet().iterator().next().getFullName();
|
||||
// fieldName = fieldName.substring(0, fieldName.lastIndexOf("."));
|
||||
// System.out.println(grpcMessage.getClass().getName());
|
||||
// return fieldName.contains(grpcMessage.getClass().getSimpleName());
|
||||
return this.supports(grpcMessage.getClass());
|
||||
}
|
||||
|
||||
protected abstract boolean supports(Class<? extends GeneratedMessageV3> grpcClass);
|
||||
}
|
||||
@@ -16,13 +16,19 @@
|
||||
|
||||
package org.springframework.cloud.function.grpc;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.function.context.FunctionCatalog;
|
||||
import org.springframework.cloud.function.context.FunctionProperties;
|
||||
import org.springframework.cloud.function.grpc.MessagingServiceGrpc.MessagingServiceImplBase;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.google.protobuf.GeneratedMessageV3;
|
||||
|
||||
import io.grpc.BindableService;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -35,13 +41,25 @@ import org.springframework.context.annotation.Configuration;
|
||||
class GrpcAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public GrpcServer grpcServer(FunctionGrpcProperties grpcProperties, MessagingServiceImplBase grpcMessagingService) {
|
||||
return new GrpcServer(grpcProperties, grpcMessagingService);
|
||||
public GrpcServer grpcServer(FunctionGrpcProperties grpcProperties, BindableService[] grpcMessagingServices) {
|
||||
Assert.notEmpty(grpcMessagingServices, "'grpcMessagingServices' must not be null or empty");
|
||||
return new GrpcServer(grpcProperties, grpcMessagingServices);
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public GrpcServerMessageHandler grpcMessageService(FunctionProperties funcProperties, FunctionCatalog functionCatalog) {
|
||||
return new GrpcServerMessageHandler(funcProperties, functionCatalog);
|
||||
public BindableService grpcSpringMessageHandler(MessageHandlingHelper helper) {
|
||||
return new GrpcServerMessageHandler(helper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MessageHandlingHelper grpcMessageHandlingHelper(List<GrpcMessageConverter<?>> grpcConverters,
|
||||
FunctionProperties funcProperties, FunctionCatalog functionCatalog) {
|
||||
return new MessageHandlingHelper(grpcConverters, functionCatalog, funcProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GrpcSpringMessageConverter grpcSpringMessageConverter() {
|
||||
return new GrpcSpringMessageConverter();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2021-2021 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
|
||||
*
|
||||
* https://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.grpc;
|
||||
|
||||
import com.google.protobuf.GeneratedMessageV3;
|
||||
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
* @param <T> instance of {@link GeneratedMessageV3}
|
||||
*/
|
||||
public interface GrpcMessageConverter<T extends GeneratedMessageV3> {
|
||||
|
||||
Message<byte[]> toSpringMessage(T grpcMessage);
|
||||
|
||||
T fromSpringMessage(Message<byte[]> springMessage, Class<T> grpcClass);
|
||||
}
|
||||
@@ -19,38 +19,46 @@ package org.springframework.cloud.function.grpc;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import io.grpc.BindableService;
|
||||
import io.grpc.Server;
|
||||
import io.grpc.ServerBuilder;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.cloud.function.grpc.MessagingServiceGrpc.MessagingServiceImplBase;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
class GrpcServer implements SmartLifecycle {
|
||||
|
||||
private Log logger = LogFactory.getLog(GrpcServer.class);
|
||||
|
||||
private final FunctionGrpcProperties grpcProperties;
|
||||
|
||||
private final MessagingServiceImplBase grpcMessageService;
|
||||
private final BindableService[] grpcMessageServices;
|
||||
|
||||
private final ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
|
||||
private Server server;
|
||||
|
||||
GrpcServer(FunctionGrpcProperties grpcProperties, MessagingServiceImplBase grpcMessageService) {
|
||||
GrpcServer(FunctionGrpcProperties grpcProperties, BindableService[] grpcMessageServices) {
|
||||
this.grpcProperties = grpcProperties;
|
||||
this.grpcMessageService = grpcMessageService;
|
||||
this.grpcMessageServices = grpcMessageServices;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
this.executor.execute(() -> {
|
||||
try {
|
||||
this.server = ServerBuilder.forPort(this.grpcProperties.getPort())
|
||||
.addService(this.grpcMessageService)
|
||||
.build();
|
||||
ServerBuilder<?> serverBuilder = ServerBuilder.forPort(this.grpcProperties.getPort());
|
||||
for (int i = 0; i < this.grpcMessageServices.length; i++) {
|
||||
BindableService bindableService = this.grpcMessageServices[i];
|
||||
serverBuilder.addService(bindableService);
|
||||
}
|
||||
this.server = serverBuilder.build();
|
||||
|
||||
logger.info("Starting gRPC server");
|
||||
this.server.start();
|
||||
|
||||
@@ -32,14 +32,13 @@
|
||||
|
||||
package org.springframework.cloud.function.grpc;
|
||||
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
//
|
||||
import io.grpc.Status;
|
||||
import io.grpc.stub.ServerCallStreamObserver;
|
||||
import io.grpc.stub.StreamObserver;
|
||||
@@ -49,7 +48,7 @@ import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
import reactor.core.publisher.Sinks.Many;
|
||||
|
||||
//
|
||||
import org.springframework.cloud.function.context.FunctionCatalog;
|
||||
import org.springframework.cloud.function.context.FunctionProperties;
|
||||
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
|
||||
@@ -59,291 +58,53 @@ import org.springframework.messaging.Message;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import com.google.protobuf.GeneratedMessageV3;
|
||||
//
|
||||
//import com.google.protobuf.GeneratedMessage;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 3.2
|
||||
*
|
||||
*/
|
||||
class GrpcServerMessageHandler extends MessagingServiceImplBase implements SmartLifecycle {
|
||||
@SuppressWarnings("rawtypes")
|
||||
class GrpcServerMessageHandler extends MessagingServiceImplBase {
|
||||
|
||||
private Log logger = LogFactory.getLog(GrpcServerMessageHandler.class);
|
||||
|
||||
private final ExecutorService executor;
|
||||
|
||||
private final FunctionProperties funcProperties;
|
||||
|
||||
private final FunctionCatalog functionCatalog;
|
||||
private final MessageHandlingHelper helper;
|
||||
|
||||
private boolean running;
|
||||
|
||||
|
||||
GrpcServerMessageHandler(FunctionProperties funcProperties, FunctionCatalog functionCatalog) {
|
||||
this.functionCatalog = functionCatalog;
|
||||
this.funcProperties = funcProperties;
|
||||
this.executor = Executors.newCachedThreadPool();
|
||||
GrpcServerMessageHandler(MessageHandlingHelper<GeneratedMessageV3> helper) {
|
||||
this.helper = helper;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void requestReply(GrpcMessage request, StreamObserver<GrpcMessage> responseObserver) {
|
||||
Message<byte[]> message = GrpcUtils.fromGrpcMessage(request);
|
||||
FunctionInvocationWrapper function = this.resolveFunction(message.getHeaders());
|
||||
public void requestReply(GrpcSpringMessage request, StreamObserver<GrpcSpringMessage> responseObserver) {
|
||||
this.helper.requestReply(request, responseObserver);
|
||||
}
|
||||
|
||||
Message<byte[]> replyMessage = (Message<byte[]>) function.apply(message);
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void serverStream(GrpcSpringMessage request, StreamObserver<GrpcSpringMessage> responseObserver) {
|
||||
this.helper.serverStream(request, responseObserver);
|
||||
}
|
||||
|
||||
GrpcMessage reply = GrpcUtils.toGrpcMessage(replyMessage);
|
||||
|
||||
responseObserver.onNext(reply);
|
||||
responseObserver.onCompleted();
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public StreamObserver<GrpcSpringMessage> clientStream(StreamObserver<GrpcSpringMessage> responseObserver) {
|
||||
return this.helper.clientStream(responseObserver, GrpcSpringMessage.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void serverStream(GrpcMessage request, StreamObserver<GrpcMessage> responseObserver) {
|
||||
Message<byte[]> message = GrpcUtils.fromGrpcMessage(request);
|
||||
FunctionInvocationWrapper function = this.resolveFunction(message.getHeaders());
|
||||
Publisher<Message<byte[]>> replyStream = (Publisher<Message<byte[]>>) function.apply(message);
|
||||
Flux.from(replyStream).doOnNext(replyMessage -> {
|
||||
responseObserver.onNext(GrpcUtils.toGrpcMessage(replyMessage));
|
||||
})
|
||||
.doOnComplete(() -> responseObserver.onCompleted())
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public StreamObserver<GrpcMessage> clientStream(StreamObserver<GrpcMessage> responseObserver) {
|
||||
ServerCallStreamObserver<GrpcMessage> serverCallStreamObserver = (ServerCallStreamObserver<GrpcMessage>) responseObserver;
|
||||
serverCallStreamObserver.disableAutoInboundFlowControl();
|
||||
|
||||
FunctionInvocationWrapper function = this.resolveFunction(null);
|
||||
|
||||
AtomicBoolean wasReady = new AtomicBoolean(false);
|
||||
serverCallStreamObserver.setOnReadyHandler(() -> {
|
||||
if (serverCallStreamObserver.isReady() && !wasReady.get()) {
|
||||
wasReady.set(true);
|
||||
logger.info("gRPC Server receiving stream is ready.");
|
||||
serverCallStreamObserver.request(1);
|
||||
}
|
||||
});
|
||||
|
||||
if (!function.isInputTypePublisher()) {
|
||||
throw new UnsupportedOperationException("The client streaming is "
|
||||
+ "not supported for functions that accept non-Publisher: "
|
||||
+ function);
|
||||
}
|
||||
else if (function.isOutputTypePublisher()) {
|
||||
throw new UnsupportedOperationException("The client streaming is "
|
||||
+ "not supported for functions that return Publisher: "
|
||||
+ function);
|
||||
}
|
||||
else {
|
||||
Many<Message<byte[]>> inputStream = Sinks.many().unicast().onBackpressureBuffer();
|
||||
Flux<Message<byte[]>> inputStreamFlux = inputStream.asFlux();
|
||||
|
||||
LinkedBlockingQueue<Message<byte[]>> resultRef = new LinkedBlockingQueue<>(1);
|
||||
this.executor.execute(() -> {
|
||||
Message<byte[]> replyMessage = (Message<byte[]>) function.apply(inputStreamFlux);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Function invocation reply: " + replyMessage);
|
||||
}
|
||||
resultRef.offer(replyMessage);
|
||||
});
|
||||
|
||||
return new StreamObserver<GrpcMessage>() {
|
||||
|
||||
@Override
|
||||
public void onNext(GrpcMessage inputMessage) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("gRPC Server receiving: " + inputMessage);
|
||||
}
|
||||
|
||||
inputStream.tryEmitNext(GrpcUtils.fromGrpcMessage(inputMessage));
|
||||
serverCallStreamObserver.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCompleted() {
|
||||
logger.info("gRPC Server has finished receiving data.");
|
||||
inputStream.tryEmitComplete();
|
||||
try {
|
||||
responseObserver.onNext(GrpcUtils.toGrpcMessage(resultRef.poll(Integer.MAX_VALUE, TimeUnit.MILLISECONDS)));
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public StreamObserver<GrpcMessage> biStream(StreamObserver<GrpcMessage> responseObserver) {
|
||||
ServerCallStreamObserver<GrpcMessage> serverCallStreamObserver = (ServerCallStreamObserver<GrpcMessage>) responseObserver;
|
||||
serverCallStreamObserver.disableAutoInboundFlowControl();
|
||||
|
||||
FunctionInvocationWrapper function = this.resolveFunction(null);
|
||||
|
||||
AtomicBoolean wasReady = new AtomicBoolean(false);
|
||||
serverCallStreamObserver.setOnReadyHandler(() -> {
|
||||
if (serverCallStreamObserver.isReady() && !wasReady.get()) {
|
||||
wasReady.set(true);
|
||||
logger.info("gRPC Server receiving stream is ready.");
|
||||
serverCallStreamObserver.request(1);
|
||||
}
|
||||
});
|
||||
|
||||
if (function.isInputTypePublisher()) {
|
||||
if (function.isOutputTypePublisher()) {
|
||||
return this.biStreamReactive(responseObserver, serverCallStreamObserver);
|
||||
}
|
||||
throw new UnsupportedOperationException("The bi-directional streaming is "
|
||||
+ "not supported for functions that accept Publisher but return non-Publisher: "
|
||||
+ function);
|
||||
}
|
||||
else {
|
||||
if (!function.isOutputTypePublisher()) {
|
||||
return this.biStreamImperative(responseObserver, serverCallStreamObserver, wasReady);
|
||||
}
|
||||
throw new UnsupportedOperationException("The bidirection streaming is "
|
||||
+ "not supported for functions that accept non-Publisher but return Publisher: "
|
||||
+ function);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private StreamObserver<GrpcMessage> biStreamImperative(StreamObserver<GrpcMessage> responseObserver,
|
||||
ServerCallStreamObserver<GrpcMessage> serverCallStreamObserver, AtomicBoolean wasReady) {
|
||||
return new StreamObserver<GrpcMessage>() {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void onNext(GrpcMessage request) {
|
||||
try {
|
||||
Message<byte[]> message = GrpcUtils.fromGrpcMessage(request);
|
||||
FunctionInvocationWrapper function = resolveFunction(message.getHeaders());
|
||||
|
||||
Message<byte[]> replyMessage = (Message<byte[]>) function.apply(message);
|
||||
|
||||
GrpcMessage reply = GrpcUtils.toGrpcMessage(replyMessage);
|
||||
|
||||
responseObserver.onNext(reply);
|
||||
|
||||
// Check the provided ServerCallStreamObserver to see if it is still
|
||||
// ready to accept more messages.
|
||||
if (serverCallStreamObserver.isReady()) {
|
||||
serverCallStreamObserver.request(1);
|
||||
}
|
||||
else {
|
||||
wasReady.set(false);
|
||||
}
|
||||
}
|
||||
catch (Throwable throwable) {
|
||||
throwable.printStackTrace();
|
||||
responseObserver.onError(
|
||||
Status.UNKNOWN.withDescription("Error handling request").withCause(throwable).asException());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCompleted() {
|
||||
logger.info("gRPC Server has finished receiving data.");
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
this.running = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
this.executor.shutdown();
|
||||
try {
|
||||
Assert.isTrue(this.executor.awaitTermination(5000, TimeUnit.MILLISECONDS), "gRPC Server executor timed out while stopping, "
|
||||
+ "since there are currently executing tasks");
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
this.running = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return this.running;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private StreamObserver<GrpcMessage> biStreamReactive(StreamObserver<GrpcMessage> responseObserver,
|
||||
ServerCallStreamObserver<GrpcMessage> serverCallStreamObserver) {
|
||||
Many<Message<byte[]>> inputStream = Sinks.many().unicast().onBackpressureBuffer();
|
||||
Flux<Message<byte[]>> inputStreamFlux = inputStream.asFlux();
|
||||
|
||||
FunctionInvocationWrapper function = this.resolveFunction(null);
|
||||
|
||||
Publisher<Message<byte[]>> outputPublisher = (Publisher<Message<byte[]>>) function.apply(inputStreamFlux);
|
||||
|
||||
Flux.from(outputPublisher).subscribe(functionResult -> {
|
||||
GrpcMessage outputMessage = GrpcUtils.toGrpcMessage(functionResult);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("gRPC Server replying: " + outputMessage);
|
||||
}
|
||||
responseObserver.onNext(outputMessage);
|
||||
});
|
||||
|
||||
return new StreamObserver<GrpcMessage>() {
|
||||
|
||||
@Override
|
||||
public void onNext(GrpcMessage inputMessage) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("gRPC Server receiving: " + inputMessage);
|
||||
}
|
||||
|
||||
inputStream.tryEmitNext(GrpcUtils.fromGrpcMessage(inputMessage));
|
||||
serverCallStreamObserver.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCompleted() {
|
||||
logger.info("gRPC Server has finished receiving data.");
|
||||
inputStream.tryEmitComplete();
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private FunctionInvocationWrapper resolveFunction(Map<String, Object> headers) {
|
||||
String functionDefinition = funcProperties.getDefinition();
|
||||
if (!CollectionUtils.isEmpty(headers) && headers.containsKey(FunctionProperties.FUNCTION_DEFINITION)) {
|
||||
functionDefinition = (String) headers.get(FunctionProperties.FUNCTION_DEFINITION);
|
||||
}
|
||||
FunctionInvocationWrapper function = this.functionCatalog.lookup(functionDefinition, "application/json");
|
||||
Assert.notNull(function, "Failed to lookup function " + funcProperties.getDefinition());
|
||||
return function;
|
||||
public StreamObserver<GrpcSpringMessage> biStream(StreamObserver<GrpcSpringMessage> responseObserver) {
|
||||
return this.helper.biStream(responseObserver, GrpcSpringMessage.class);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2021-2021 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
|
||||
*
|
||||
* https://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.grpc;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.GeneratedMessageV3;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class GrpcSpringMessageConverter extends AbstractGrpcMessageConverter<GrpcSpringMessage> {
|
||||
|
||||
@Override
|
||||
protected Message<byte[]> doToSpringMessage(GrpcSpringMessage grpcMessage) {
|
||||
return MessageBuilder.withPayload(grpcMessage.getPayload().toByteArray())
|
||||
.copyHeaders(grpcMessage.getHeadersMap())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GrpcSpringMessage doFromSpringMessage(Message<byte[]> springMessage) {
|
||||
Map<String, String> stringHeaders = new HashMap<>();
|
||||
springMessage.getHeaders().forEach((k, v) -> {
|
||||
stringHeaders.put(k, v.toString());
|
||||
});
|
||||
return GrpcSpringMessage.newBuilder()
|
||||
.setPayload(ByteString.copyFrom(springMessage.getPayload()))
|
||||
.putAllHeaders(stringHeaders)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean supports(Class<? extends GeneratedMessageV3> grpcClass) {
|
||||
return grpcClass.isAssignableFrom(GrpcSpringMessage.class);
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.function.grpc;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
@@ -24,6 +25,7 @@ import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import io.grpc.ManagedChannel;
|
||||
import io.grpc.ManagedChannelBuilder;
|
||||
@@ -53,22 +55,22 @@ final class GrpcUtils {
|
||||
|
||||
}
|
||||
|
||||
public static GrpcMessage toGrpcMessage(byte[] payload, Map<String, String> headers) {
|
||||
return GrpcMessage.newBuilder()
|
||||
public static GrpcSpringMessage toGrpcSpringMessage(byte[] payload, Map<String, String> headers) {
|
||||
return GrpcSpringMessage.newBuilder()
|
||||
.setPayload(ByteString.copyFrom(payload))
|
||||
.putAllHeaders(headers)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static GrpcMessage toGrpcMessage(Message<byte[]> message) {
|
||||
public static GrpcSpringMessage toGrpcSpringMessage(Message<byte[]> message) {
|
||||
Map<String, String> stringHeaders = new HashMap<>();
|
||||
message.getHeaders().forEach((k, v) -> {
|
||||
stringHeaders.put(k, v.toString());
|
||||
});
|
||||
return toGrpcMessage(message.getPayload(), stringHeaders);
|
||||
return toGrpcSpringMessage(message.getPayload(), stringHeaders);
|
||||
}
|
||||
|
||||
public static Message<byte[]> fromGrpcMessage(GrpcMessage message) {
|
||||
public static Message<byte[]> fromGrpcSpringMessage(GrpcSpringMessage message) {
|
||||
return MessageBuilder.withPayload(message.getPayload().toByteArray())
|
||||
.copyHeaders(message.getHeadersMap())
|
||||
.build();
|
||||
@@ -84,9 +86,9 @@ final class GrpcUtils {
|
||||
MessagingServiceGrpc.MessagingServiceBlockingStub stub = MessagingServiceGrpc
|
||||
.newBlockingStub(channel);
|
||||
|
||||
GrpcMessage response = stub.requestReply(toGrpcMessage(inputMessage));
|
||||
GrpcSpringMessage response = stub.requestReply(toGrpcSpringMessage(inputMessage));
|
||||
channel.shutdown();
|
||||
return fromGrpcMessage(response);
|
||||
return fromGrpcSpringMessage(response);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -121,7 +123,7 @@ final class GrpcUtils {
|
||||
.newStub(channel);
|
||||
Many<Message<byte[]>> sink = Sinks.many().unicast().onBackpressureBuffer();
|
||||
|
||||
ClientResponseObserver<GrpcMessage, GrpcMessage> clientResponseObserver = clientResponseObserver(inputStream, sink);
|
||||
ClientResponseObserver<GrpcSpringMessage, GrpcSpringMessage> clientResponseObserver = clientResponseObserver(inputStream, sink);
|
||||
|
||||
stub.biStream(clientResponseObserver);
|
||||
|
||||
@@ -137,14 +139,14 @@ final class GrpcUtils {
|
||||
MessagingServiceGrpc.MessagingServiceBlockingStub stub = MessagingServiceGrpc
|
||||
.newBlockingStub(channel);
|
||||
|
||||
Iterator<GrpcMessage> serverStream = stub.serverStream(toGrpcMessage(inputMessage));
|
||||
Iterator<GrpcSpringMessage> serverStream = stub.serverStream(toGrpcSpringMessage(inputMessage));
|
||||
|
||||
Many<Message<byte[]>> sink = Sinks.many().unicast().onBackpressureBuffer();
|
||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
executor.execute(() -> {
|
||||
while (serverStream.hasNext()) {
|
||||
GrpcMessage grpcMessage = serverStream.next();
|
||||
sink.tryEmitNext(GrpcUtils.fromGrpcMessage(grpcMessage));
|
||||
GrpcSpringMessage grpcMessage = serverStream.next();
|
||||
sink.tryEmitNext(GrpcUtils.fromGrpcSpringMessage(grpcMessage));
|
||||
}
|
||||
sink.tryEmitComplete();
|
||||
});
|
||||
@@ -182,13 +184,13 @@ final class GrpcUtils {
|
||||
.usePlaintext().build();
|
||||
|
||||
LinkedBlockingQueue<Message<byte[]>> resultRef = new LinkedBlockingQueue<>(1);
|
||||
StreamObserver<GrpcMessage> responseObserver = new StreamObserver<GrpcMessage>() {
|
||||
StreamObserver<GrpcSpringMessage> responseObserver = new StreamObserver<GrpcSpringMessage>() {
|
||||
@Override
|
||||
public void onNext(GrpcMessage result) {
|
||||
public void onNext(GrpcSpringMessage result) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Client received reply: " + result);
|
||||
}
|
||||
resultRef.offer(GrpcUtils.fromGrpcMessage(result));
|
||||
resultRef.offer(GrpcUtils.fromGrpcSpringMessage(result));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -204,14 +206,14 @@ final class GrpcUtils {
|
||||
|
||||
MessagingServiceGrpc.MessagingServiceStub asyncStub = MessagingServiceGrpc.newStub(channel);
|
||||
|
||||
StreamObserver<GrpcMessage> requestObserver = asyncStub.clientStream(responseObserver);
|
||||
StreamObserver<GrpcSpringMessage> requestObserver = asyncStub.clientStream(responseObserver);
|
||||
|
||||
inputStream.doOnNext(message -> {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Client sending: " + message);
|
||||
}
|
||||
try {
|
||||
requestObserver.onNext(GrpcUtils.toGrpcMessage(message));
|
||||
requestObserver.onNext(GrpcUtils.toGrpcSpringMessage(message));
|
||||
}
|
||||
catch (Exception e) {
|
||||
requestObserver.onError(e);
|
||||
@@ -229,13 +231,13 @@ final class GrpcUtils {
|
||||
}
|
||||
}
|
||||
|
||||
private static ClientResponseObserver<GrpcMessage, GrpcMessage> clientResponseObserver(Flux<Message<byte[]>> inputStream, Many<Message<byte[]>> sink) {
|
||||
return new ClientResponseObserver<GrpcMessage, GrpcMessage>() {
|
||||
private static ClientResponseObserver<GrpcSpringMessage, GrpcSpringMessage> clientResponseObserver(Flux<Message<byte[]>> inputStream, Many<Message<byte[]>> sink) {
|
||||
return new ClientResponseObserver<GrpcSpringMessage, GrpcSpringMessage>() {
|
||||
|
||||
ClientCallStreamObserver<GrpcMessage> requestStreamObserver;
|
||||
ClientCallStreamObserver<GrpcSpringMessage> requestStreamObserver;
|
||||
|
||||
@Override
|
||||
public void beforeStart(ClientCallStreamObserver<GrpcMessage> requestStreamObserver) {
|
||||
public void beforeStart(ClientCallStreamObserver<GrpcSpringMessage> requestStreamObserver) {
|
||||
this.requestStreamObserver = requestStreamObserver;
|
||||
requestStreamObserver.disableAutoInboundFlowControl();
|
||||
|
||||
@@ -247,7 +249,7 @@ final class GrpcUtils {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Streaming message to function: " + request);
|
||||
}
|
||||
requestStreamObserver.onNext(GrpcUtils.toGrpcMessage(request));
|
||||
requestStreamObserver.onNext(GrpcUtils.toGrpcSpringMessage(request));
|
||||
})
|
||||
.doOnComplete(() -> {
|
||||
requestStreamObserver.onCompleted();
|
||||
@@ -258,11 +260,11 @@ final class GrpcUtils {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(GrpcMessage message) {
|
||||
public void onNext(GrpcSpringMessage message) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Streaming message from function: " + message);
|
||||
}
|
||||
sink.tryEmitNext(fromGrpcMessage(message));
|
||||
sink.tryEmitNext(fromGrpcSpringMessage(message));
|
||||
requestStreamObserver.request(1);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,352 @@
|
||||
/*
|
||||
* Copyright 2021-2021 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
|
||||
*
|
||||
* https://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.grpc;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import com.google.protobuf.GeneratedMessageV3;
|
||||
|
||||
import io.grpc.Status;
|
||||
import io.grpc.stub.ServerCallStreamObserver;
|
||||
import io.grpc.stub.StreamObserver;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
import reactor.core.publisher.Sinks.Many;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.springframework.cloud.function.context.FunctionCatalog;
|
||||
import org.springframework.cloud.function.context.FunctionProperties;
|
||||
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class MessageHandlingHelper<T extends GeneratedMessageV3> implements SmartLifecycle {
|
||||
|
||||
private Log logger = LogFactory.getLog(MessageHandlingHelper.class);
|
||||
|
||||
private final List<GrpcMessageConverter<?>> grpcConverters;
|
||||
|
||||
private final FunctionProperties funcProperties;
|
||||
|
||||
private final FunctionCatalog functionCatalog;
|
||||
|
||||
private final ExecutorService executor;
|
||||
|
||||
private boolean running;
|
||||
|
||||
public MessageHandlingHelper(List<GrpcMessageConverter<?>> grpcConverters,
|
||||
FunctionCatalog functionCatalog, FunctionProperties funcProperties) {
|
||||
this.grpcConverters = grpcConverters;
|
||||
this.funcProperties = funcProperties;
|
||||
this.functionCatalog = functionCatalog;
|
||||
this.executor = Executors.newCachedThreadPool();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void requestReply(T request, StreamObserver<T> responseObserver) {
|
||||
Message<byte[]> message = this.toSpringMessage(request);
|
||||
FunctionInvocationWrapper function = this.resolveFunction(message.getHeaders());
|
||||
|
||||
Message<byte[]> replyMessage = (Message<byte[]>) function.apply(message);
|
||||
GeneratedMessageV3 reply = this.toGrpcMessage(replyMessage, (Class<T>) request.getClass());
|
||||
|
||||
responseObserver.onNext((T) reply);
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void serverStream(T request, StreamObserver<T> responseObserver) {
|
||||
Message<byte[]> message = this.toSpringMessage(request);
|
||||
FunctionInvocationWrapper function = this.resolveFunction(message.getHeaders());
|
||||
Publisher<Message<byte[]>> replyStream = (Publisher<Message<byte[]>>) function.apply(message);
|
||||
Flux.from(replyStream).doOnNext(replyMessage -> {
|
||||
responseObserver.onNext(this.toGrpcMessage(replyMessage, (Class<T>) request.getClass()));
|
||||
})
|
||||
.doOnComplete(() -> responseObserver.onCompleted())
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public StreamObserver<T> clientStream(StreamObserver<T> responseObserver, Class<T> grpcMessageType) {
|
||||
ServerCallStreamObserver<T> serverCallStreamObserver = (ServerCallStreamObserver<T>) responseObserver;
|
||||
serverCallStreamObserver.disableAutoInboundFlowControl();
|
||||
|
||||
FunctionInvocationWrapper function = this.resolveFunction(null);
|
||||
|
||||
AtomicBoolean wasReady = new AtomicBoolean(false);
|
||||
serverCallStreamObserver.setOnReadyHandler(() -> {
|
||||
if (serverCallStreamObserver.isReady() && !wasReady.get()) {
|
||||
wasReady.set(true);
|
||||
logger.info("gRPC Server receiving stream is ready.");
|
||||
serverCallStreamObserver.request(1);
|
||||
}
|
||||
});
|
||||
|
||||
if (!function.isInputTypePublisher()) {
|
||||
throw new UnsupportedOperationException("The client streaming is "
|
||||
+ "not supported for functions that accept non-Publisher: "
|
||||
+ function);
|
||||
}
|
||||
else if (function.isOutputTypePublisher()) {
|
||||
throw new UnsupportedOperationException("The client streaming is "
|
||||
+ "not supported for functions that return Publisher: "
|
||||
+ function);
|
||||
}
|
||||
else {
|
||||
Many<Message<byte[]>> inputStream = Sinks.many().unicast().onBackpressureBuffer();
|
||||
Flux<Message<byte[]>> inputStreamFlux = inputStream.asFlux();
|
||||
|
||||
LinkedBlockingQueue<Message<byte[]>> resultRef = new LinkedBlockingQueue<>(1);
|
||||
this.executor.execute(() -> {
|
||||
Message<byte[]> replyMessage = (Message<byte[]>) function.apply(inputStreamFlux);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Function invocation reply: " + replyMessage);
|
||||
}
|
||||
resultRef.offer(replyMessage);
|
||||
});
|
||||
|
||||
return new StreamObserver<T>() {
|
||||
@Override
|
||||
public void onNext(T inputMessage) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("gRPC Server receiving: " + inputMessage);
|
||||
}
|
||||
inputStream.tryEmitNext(toSpringMessage(inputMessage));
|
||||
serverCallStreamObserver.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCompleted() {
|
||||
logger.info("gRPC Server has finished receiving data.");
|
||||
inputStream.tryEmitComplete();
|
||||
try {
|
||||
responseObserver.onNext(toGrpcMessage(resultRef.poll(Integer.MAX_VALUE, TimeUnit.MILLISECONDS), grpcMessageType));
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public StreamObserver<T> biStream(StreamObserver<T> responseObserver, Class<T> grpcMessageType) {
|
||||
ServerCallStreamObserver<T> serverCallStreamObserver = (ServerCallStreamObserver<T>) responseObserver;
|
||||
serverCallStreamObserver.disableAutoInboundFlowControl();
|
||||
|
||||
FunctionInvocationWrapper function = this.resolveFunction(null);
|
||||
|
||||
AtomicBoolean wasReady = new AtomicBoolean(false);
|
||||
serverCallStreamObserver.setOnReadyHandler(() -> {
|
||||
if (serverCallStreamObserver.isReady() && !wasReady.get()) {
|
||||
wasReady.set(true);
|
||||
logger.info("gRPC Server receiving stream is ready.");
|
||||
serverCallStreamObserver.request(1);
|
||||
}
|
||||
});
|
||||
|
||||
if (function.isInputTypePublisher()) {
|
||||
if (function.isOutputTypePublisher()) {
|
||||
return this.biStreamReactive(responseObserver, serverCallStreamObserver, grpcMessageType);
|
||||
}
|
||||
throw new UnsupportedOperationException("The bi-directional streaming is "
|
||||
+ "not supported for functions that accept Publisher but return non-Publisher: "
|
||||
+ function);
|
||||
}
|
||||
else {
|
||||
if (!function.isOutputTypePublisher()) {
|
||||
return this.biStreamImperative(responseObserver, serverCallStreamObserver, wasReady);
|
||||
}
|
||||
throw new UnsupportedOperationException("The bidirection streaming is "
|
||||
+ "not supported for functions that accept non-Publisher but return Publisher: "
|
||||
+ function);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private StreamObserver<T> biStreamReactive(StreamObserver<T> responseObserver,
|
||||
ServerCallStreamObserver<T> serverCallStreamObserver, Class<T> grpcMessageType) {
|
||||
Many<Message<byte[]>> inputStream = Sinks.many().unicast().onBackpressureBuffer();
|
||||
Flux<Message<byte[]>> inputStreamFlux = inputStream.asFlux();
|
||||
|
||||
FunctionInvocationWrapper function = this.resolveFunction(null);
|
||||
|
||||
Publisher<Message<byte[]>> outputPublisher = (Publisher<Message<byte[]>>) function.apply(inputStreamFlux);
|
||||
|
||||
Flux.from(outputPublisher).subscribe(functionResult -> {
|
||||
T outputMessage = toGrpcMessage(functionResult, grpcMessageType);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("gRPC Server replying: " + outputMessage);
|
||||
}
|
||||
responseObserver.onNext(outputMessage);
|
||||
});
|
||||
|
||||
return new StreamObserver<T>() {
|
||||
@Override
|
||||
public void onNext(T inputMessage) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("gRPC Server receiving: " + inputMessage);
|
||||
}
|
||||
//GRPC_MESSAGE_TYPE = (Class<T>) inputMessage.getClass();
|
||||
inputStream.tryEmitNext(toSpringMessage(inputMessage));
|
||||
serverCallStreamObserver.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCompleted() {
|
||||
logger.info("gRPC Server has finished receiving data.");
|
||||
inputStream.tryEmitComplete();
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private StreamObserver<T> biStreamImperative(StreamObserver<T> responseObserver,
|
||||
ServerCallStreamObserver<T> serverCallStreamObserver,
|
||||
AtomicBoolean wasReady) {
|
||||
return new StreamObserver<T>() {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void onNext(T request) {
|
||||
try {
|
||||
Message<byte[]> message = toSpringMessage(request);
|
||||
FunctionInvocationWrapper function = resolveFunction(
|
||||
message.getHeaders());
|
||||
|
||||
Message<byte[]> replyMessage = (Message<byte[]>) function
|
||||
.apply(message);
|
||||
|
||||
T reply = toGrpcMessage(replyMessage, (Class<T>) request.getClass());
|
||||
|
||||
responseObserver.onNext(reply);
|
||||
|
||||
// Check the provided ServerCallStreamObserver to see if it is still
|
||||
// ready to accept more messages.
|
||||
if (serverCallStreamObserver.isReady()) {
|
||||
serverCallStreamObserver.request(1);
|
||||
}
|
||||
else {
|
||||
wasReady.set(false);
|
||||
}
|
||||
}
|
||||
catch (Throwable throwable) {
|
||||
throwable.printStackTrace();
|
||||
responseObserver.onError(
|
||||
Status.UNKNOWN.withDescription("Error handling request")
|
||||
.withCause(throwable).asException());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCompleted() {
|
||||
logger.info("gRPC Server has finished receiving data.");
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
private T toGrpcMessage(Message<byte[]> request, Class<T> grpcClass) {
|
||||
for (GrpcMessageConverter converter : this.grpcConverters) {
|
||||
GeneratedMessageV3 grpcMessage = converter.fromSpringMessage(request, grpcClass);
|
||||
if (grpcMessage != null) {
|
||||
return (T) grpcMessage;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("Failed to convert Grpc Message to Spring Message: " + request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
this.running = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
this.executor.shutdown();
|
||||
try {
|
||||
Assert.isTrue(this.executor.awaitTermination(5000, TimeUnit.MILLISECONDS), "gRPC Server executor timed out while stopping, "
|
||||
+ "since there are currently executing tasks");
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
this.running = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return this.running;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
private Message<byte[]> toSpringMessage(GeneratedMessageV3 request) {
|
||||
for (GrpcMessageConverter converter : this.grpcConverters) {
|
||||
Message<byte[]> springMessage = converter.toSpringMessage(request);
|
||||
if (springMessage != null) {
|
||||
return springMessage;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("Failed to convert Grpc Message to Spring Message: " + request);
|
||||
}
|
||||
|
||||
private FunctionInvocationWrapper resolveFunction(Map<String, Object> headers) {
|
||||
String functionDefinition = funcProperties.getDefinition();
|
||||
if (!CollectionUtils.isEmpty(headers) && headers.containsKey(FunctionProperties.FUNCTION_DEFINITION)) {
|
||||
functionDefinition = (String) headers.get(FunctionProperties.FUNCTION_DEFINITION);
|
||||
}
|
||||
FunctionInvocationWrapper function = this.functionCatalog.lookup(functionDefinition, "application/json");
|
||||
Assert.notNull(function, "Failed to lookup function " + funcProperties.getDefinition());
|
||||
return function;
|
||||
}
|
||||
}
|
||||
@@ -2,17 +2,17 @@ syntax = "proto3";
|
||||
option java_multiple_files = true;
|
||||
package org.springframework.cloud.function.grpc;
|
||||
|
||||
message GrpcMessage {
|
||||
message GrpcSpringMessage {
|
||||
bytes payload = 1;
|
||||
map<string, string> headers = 2;
|
||||
}
|
||||
|
||||
service MessagingService {
|
||||
rpc biStream(stream GrpcMessage) returns (stream GrpcMessage);
|
||||
rpc biStream(stream GrpcSpringMessage) returns (stream GrpcSpringMessage);
|
||||
|
||||
rpc clientStream(stream GrpcMessage) returns (GrpcMessage);
|
||||
rpc clientStream(stream GrpcSpringMessage) returns (GrpcSpringMessage);
|
||||
|
||||
rpc serverStream(GrpcMessage) returns (stream GrpcMessage);
|
||||
rpc serverStream(GrpcSpringMessage) returns (stream GrpcSpringMessage);
|
||||
|
||||
rpc requestReply(GrpcMessage) returns (GrpcMessage);
|
||||
rpc requestReply(GrpcSpringMessage) returns (GrpcSpringMessage);
|
||||
}
|
||||
33
spring-cloud-function-samples/function-sample-grpc-cloudevent/.gitignore
vendored
Normal file
33
spring-cloud-function-samples/function-sample-grpc-cloudevent/.gitignore
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright 2007-present 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
|
||||
*
|
||||
* https://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.
|
||||
*/
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.nio.channels.*;
|
||||
import java.util.Properties;
|
||||
|
||||
public class MavenWrapperDownloader {
|
||||
|
||||
private static final String WRAPPER_VERSION = "0.5.6";
|
||||
/**
|
||||
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
|
||||
*/
|
||||
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
|
||||
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
|
||||
|
||||
/**
|
||||
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
|
||||
* use instead of the default one.
|
||||
*/
|
||||
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
|
||||
".mvn/wrapper/maven-wrapper.properties";
|
||||
|
||||
/**
|
||||
* Path where the maven-wrapper.jar will be saved to.
|
||||
*/
|
||||
private static final String MAVEN_WRAPPER_JAR_PATH =
|
||||
".mvn/wrapper/maven-wrapper.jar";
|
||||
|
||||
/**
|
||||
* Name of the property which should be used to override the default download url for the wrapper.
|
||||
*/
|
||||
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
|
||||
|
||||
public static void main(String args[]) {
|
||||
System.out.println("- Downloader started");
|
||||
File baseDirectory = new File(args[0]);
|
||||
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
|
||||
|
||||
// If the maven-wrapper.properties exists, read it and check if it contains a custom
|
||||
// wrapperUrl parameter.
|
||||
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
|
||||
String url = DEFAULT_DOWNLOAD_URL;
|
||||
if(mavenWrapperPropertyFile.exists()) {
|
||||
FileInputStream mavenWrapperPropertyFileInputStream = null;
|
||||
try {
|
||||
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
|
||||
Properties mavenWrapperProperties = new Properties();
|
||||
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
|
||||
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
|
||||
} catch (IOException e) {
|
||||
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
|
||||
} finally {
|
||||
try {
|
||||
if(mavenWrapperPropertyFileInputStream != null) {
|
||||
mavenWrapperPropertyFileInputStream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// Ignore ...
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("- Downloading from: " + url);
|
||||
|
||||
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
|
||||
if(!outputFile.getParentFile().exists()) {
|
||||
if(!outputFile.getParentFile().mkdirs()) {
|
||||
System.out.println(
|
||||
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
|
||||
}
|
||||
}
|
||||
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
|
||||
try {
|
||||
downloadFileFromURL(url, outputFile);
|
||||
System.out.println("Done");
|
||||
System.exit(0);
|
||||
} catch (Throwable e) {
|
||||
System.out.println("- Error downloading");
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
|
||||
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
|
||||
String username = System.getenv("MVNW_USERNAME");
|
||||
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
|
||||
Authenticator.setDefault(new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(username, password);
|
||||
}
|
||||
});
|
||||
}
|
||||
URL website = new URL(urlString);
|
||||
ReadableByteChannel rbc;
|
||||
rbc = Channels.newChannel(website.openStream());
|
||||
FileOutputStream fos = new FileOutputStream(destination);
|
||||
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
|
||||
fos.close();
|
||||
rbc.close();
|
||||
}
|
||||
|
||||
}
|
||||
BIN
spring-cloud-function-samples/function-sample-grpc-cloudevent/.mvn/wrapper/maven-wrapper.jar
vendored
Normal file
BIN
spring-cloud-function-samples/function-sample-grpc-cloudevent/.mvn/wrapper/maven-wrapper.jar
vendored
Normal file
Binary file not shown.
@@ -0,0 +1,2 @@
|
||||
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.2/apache-maven-3.8.2-bin.zip
|
||||
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
|
||||
310
spring-cloud-function-samples/function-sample-grpc-cloudevent/mvnw
vendored
Executable file
310
spring-cloud-function-samples/function-sample-grpc-cloudevent/mvnw
vendored
Executable file
@@ -0,0 +1,310 @@
|
||||
#!/bin/sh
|
||||
# ----------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you 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
|
||||
#
|
||||
# https://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.
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
# Maven Start Up Batch script
|
||||
#
|
||||
# Required ENV vars:
|
||||
# ------------------
|
||||
# JAVA_HOME - location of a JDK home dir
|
||||
#
|
||||
# Optional ENV vars
|
||||
# -----------------
|
||||
# M2_HOME - location of maven2's installed home dir
|
||||
# MAVEN_OPTS - parameters passed to the Java VM when running Maven
|
||||
# e.g. to debug Maven itself, use
|
||||
# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
|
||||
# MAVEN_SKIP_RC - flag to disable loading of mavenrc files
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
if [ -z "$MAVEN_SKIP_RC" ] ; then
|
||||
|
||||
if [ -f /etc/mavenrc ] ; then
|
||||
. /etc/mavenrc
|
||||
fi
|
||||
|
||||
if [ -f "$HOME/.mavenrc" ] ; then
|
||||
. "$HOME/.mavenrc"
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
# OS specific support. $var _must_ be set to either true or false.
|
||||
cygwin=false;
|
||||
darwin=false;
|
||||
mingw=false
|
||||
case "`uname`" in
|
||||
CYGWIN*) cygwin=true ;;
|
||||
MINGW*) mingw=true;;
|
||||
Darwin*) darwin=true
|
||||
# Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
|
||||
# See https://developer.apple.com/library/mac/qa/qa1170/_index.html
|
||||
if [ -z "$JAVA_HOME" ]; then
|
||||
if [ -x "/usr/libexec/java_home" ]; then
|
||||
export JAVA_HOME="`/usr/libexec/java_home`"
|
||||
else
|
||||
export JAVA_HOME="/Library/Java/Home"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
if [ -z "$JAVA_HOME" ] ; then
|
||||
if [ -r /etc/gentoo-release ] ; then
|
||||
JAVA_HOME=`java-config --jre-home`
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -z "$M2_HOME" ] ; then
|
||||
## resolve links - $0 may be a link to maven's home
|
||||
PRG="$0"
|
||||
|
||||
# need this for relative symlinks
|
||||
while [ -h "$PRG" ] ; do
|
||||
ls=`ls -ld "$PRG"`
|
||||
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||
if expr "$link" : '/.*' > /dev/null; then
|
||||
PRG="$link"
|
||||
else
|
||||
PRG="`dirname "$PRG"`/$link"
|
||||
fi
|
||||
done
|
||||
|
||||
saveddir=`pwd`
|
||||
|
||||
M2_HOME=`dirname "$PRG"`/..
|
||||
|
||||
# make it fully qualified
|
||||
M2_HOME=`cd "$M2_HOME" && pwd`
|
||||
|
||||
cd "$saveddir"
|
||||
# echo Using m2 at $M2_HOME
|
||||
fi
|
||||
|
||||
# For Cygwin, ensure paths are in UNIX format before anything is touched
|
||||
if $cygwin ; then
|
||||
[ -n "$M2_HOME" ] &&
|
||||
M2_HOME=`cygpath --unix "$M2_HOME"`
|
||||
[ -n "$JAVA_HOME" ] &&
|
||||
JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
|
||||
[ -n "$CLASSPATH" ] &&
|
||||
CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
|
||||
fi
|
||||
|
||||
# For Mingw, ensure paths are in UNIX format before anything is touched
|
||||
if $mingw ; then
|
||||
[ -n "$M2_HOME" ] &&
|
||||
M2_HOME="`(cd "$M2_HOME"; pwd)`"
|
||||
[ -n "$JAVA_HOME" ] &&
|
||||
JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
|
||||
fi
|
||||
|
||||
if [ -z "$JAVA_HOME" ]; then
|
||||
javaExecutable="`which javac`"
|
||||
if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
|
||||
# readlink(1) is not available as standard on Solaris 10.
|
||||
readLink=`which readlink`
|
||||
if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
|
||||
if $darwin ; then
|
||||
javaHome="`dirname \"$javaExecutable\"`"
|
||||
javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
|
||||
else
|
||||
javaExecutable="`readlink -f \"$javaExecutable\"`"
|
||||
fi
|
||||
javaHome="`dirname \"$javaExecutable\"`"
|
||||
javaHome=`expr "$javaHome" : '\(.*\)/bin'`
|
||||
JAVA_HOME="$javaHome"
|
||||
export JAVA_HOME
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -z "$JAVACMD" ] ; then
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||
else
|
||||
JAVACMD="$JAVA_HOME/bin/java"
|
||||
fi
|
||||
else
|
||||
JAVACMD="`which java`"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
echo "Error: JAVA_HOME is not defined correctly." >&2
|
||||
echo " We cannot execute $JAVACMD" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "$JAVA_HOME" ] ; then
|
||||
echo "Warning: JAVA_HOME environment variable is not set."
|
||||
fi
|
||||
|
||||
CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
|
||||
|
||||
# traverses directory structure from process work directory to filesystem root
|
||||
# first directory with .mvn subdirectory is considered project base directory
|
||||
find_maven_basedir() {
|
||||
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
echo "Path not specified to find_maven_basedir"
|
||||
return 1
|
||||
fi
|
||||
|
||||
basedir="$1"
|
||||
wdir="$1"
|
||||
while [ "$wdir" != '/' ] ; do
|
||||
if [ -d "$wdir"/.mvn ] ; then
|
||||
basedir=$wdir
|
||||
break
|
||||
fi
|
||||
# workaround for JBEAP-8937 (on Solaris 10/Sparc)
|
||||
if [ -d "${wdir}" ]; then
|
||||
wdir=`cd "$wdir/.."; pwd`
|
||||
fi
|
||||
# end of workaround
|
||||
done
|
||||
echo "${basedir}"
|
||||
}
|
||||
|
||||
# concatenates all lines of a file
|
||||
concat_lines() {
|
||||
if [ -f "$1" ]; then
|
||||
echo "$(tr -s '\n' ' ' < "$1")"
|
||||
fi
|
||||
}
|
||||
|
||||
BASE_DIR=`find_maven_basedir "$(pwd)"`
|
||||
if [ -z "$BASE_DIR" ]; then
|
||||
exit 1;
|
||||
fi
|
||||
|
||||
##########################################################################################
|
||||
# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
|
||||
# This allows using the maven wrapper in projects that prohibit checking in binary data.
|
||||
##########################################################################################
|
||||
if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Found .mvn/wrapper/maven-wrapper.jar"
|
||||
fi
|
||||
else
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..."
|
||||
fi
|
||||
if [ -n "$MVNW_REPOURL" ]; then
|
||||
jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
|
||||
else
|
||||
jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
|
||||
fi
|
||||
while IFS="=" read key value; do
|
||||
case "$key" in (wrapperUrl) jarUrl="$value"; break ;;
|
||||
esac
|
||||
done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties"
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Downloading from: $jarUrl"
|
||||
fi
|
||||
wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar"
|
||||
if $cygwin; then
|
||||
wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"`
|
||||
fi
|
||||
|
||||
if command -v wget > /dev/null; then
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Found wget ... using wget"
|
||||
fi
|
||||
if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
|
||||
wget "$jarUrl" -O "$wrapperJarPath"
|
||||
else
|
||||
wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath"
|
||||
fi
|
||||
elif command -v curl > /dev/null; then
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Found curl ... using curl"
|
||||
fi
|
||||
if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
|
||||
curl -o "$wrapperJarPath" "$jarUrl" -f
|
||||
else
|
||||
curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f
|
||||
fi
|
||||
|
||||
else
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Falling back to using Java to download"
|
||||
fi
|
||||
javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java"
|
||||
# For Cygwin, switch paths to Windows format before running javac
|
||||
if $cygwin; then
|
||||
javaClass=`cygpath --path --windows "$javaClass"`
|
||||
fi
|
||||
if [ -e "$javaClass" ]; then
|
||||
if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo " - Compiling MavenWrapperDownloader.java ..."
|
||||
fi
|
||||
# Compiling the Java class
|
||||
("$JAVA_HOME/bin/javac" "$javaClass")
|
||||
fi
|
||||
if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
|
||||
# Running the downloader
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo " - Running MavenWrapperDownloader.java ..."
|
||||
fi
|
||||
("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR")
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
##########################################################################################
|
||||
# End of extension
|
||||
##########################################################################################
|
||||
|
||||
export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo $MAVEN_PROJECTBASEDIR
|
||||
fi
|
||||
MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
|
||||
|
||||
# For Cygwin, switch paths to Windows format before running java
|
||||
if $cygwin; then
|
||||
[ -n "$M2_HOME" ] &&
|
||||
M2_HOME=`cygpath --path --windows "$M2_HOME"`
|
||||
[ -n "$JAVA_HOME" ] &&
|
||||
JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
|
||||
[ -n "$CLASSPATH" ] &&
|
||||
CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
|
||||
[ -n "$MAVEN_PROJECTBASEDIR" ] &&
|
||||
MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
|
||||
fi
|
||||
|
||||
# Provide a "standardized" way to retrieve the CLI args that will
|
||||
# work with both Windows and non-Windows executions.
|
||||
MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"
|
||||
export MAVEN_CMD_LINE_ARGS
|
||||
|
||||
WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
|
||||
|
||||
exec "$JAVACMD" \
|
||||
$MAVEN_OPTS \
|
||||
-classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
|
||||
"-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
|
||||
${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"
|
||||
182
spring-cloud-function-samples/function-sample-grpc-cloudevent/mvnw.cmd
vendored
Normal file
182
spring-cloud-function-samples/function-sample-grpc-cloudevent/mvnw.cmd
vendored
Normal file
@@ -0,0 +1,182 @@
|
||||
@REM ----------------------------------------------------------------------------
|
||||
@REM Licensed to the Apache Software Foundation (ASF) under one
|
||||
@REM or more contributor license agreements. See the NOTICE file
|
||||
@REM distributed with this work for additional information
|
||||
@REM regarding copyright ownership. The ASF licenses this file
|
||||
@REM to you under the Apache License, Version 2.0 (the
|
||||
@REM "License"); you may not use this file except in compliance
|
||||
@REM with the License. You may obtain a copy of the License at
|
||||
@REM
|
||||
@REM https://www.apache.org/licenses/LICENSE-2.0
|
||||
@REM
|
||||
@REM Unless required by applicable law or agreed to in writing,
|
||||
@REM software distributed under the License is distributed on an
|
||||
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
@REM KIND, either express or implied. See the License for the
|
||||
@REM specific language governing permissions and limitations
|
||||
@REM under the License.
|
||||
@REM ----------------------------------------------------------------------------
|
||||
|
||||
@REM ----------------------------------------------------------------------------
|
||||
@REM Maven Start Up Batch script
|
||||
@REM
|
||||
@REM Required ENV vars:
|
||||
@REM JAVA_HOME - location of a JDK home dir
|
||||
@REM
|
||||
@REM Optional ENV vars
|
||||
@REM M2_HOME - location of maven2's installed home dir
|
||||
@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
|
||||
@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending
|
||||
@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
|
||||
@REM e.g. to debug Maven itself, use
|
||||
@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
|
||||
@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
|
||||
@REM ----------------------------------------------------------------------------
|
||||
|
||||
@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
|
||||
@echo off
|
||||
@REM set title of command window
|
||||
title %0
|
||||
@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on'
|
||||
@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
|
||||
|
||||
@REM set %HOME% to equivalent of $HOME
|
||||
if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
|
||||
|
||||
@REM Execute a user defined script before this one
|
||||
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
|
||||
@REM check for pre script, once with legacy .bat ending and once with .cmd ending
|
||||
if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
|
||||
if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
|
||||
:skipRcPre
|
||||
|
||||
@setlocal
|
||||
|
||||
set ERROR_CODE=0
|
||||
|
||||
@REM To isolate internal variables from possible post scripts, we use another setlocal
|
||||
@setlocal
|
||||
|
||||
@REM ==== START VALIDATION ====
|
||||
if not "%JAVA_HOME%" == "" goto OkJHome
|
||||
|
||||
echo.
|
||||
echo Error: JAVA_HOME not found in your environment. >&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the >&2
|
||||
echo location of your Java installation. >&2
|
||||
echo.
|
||||
goto error
|
||||
|
||||
:OkJHome
|
||||
if exist "%JAVA_HOME%\bin\java.exe" goto init
|
||||
|
||||
echo.
|
||||
echo Error: JAVA_HOME is set to an invalid directory. >&2
|
||||
echo JAVA_HOME = "%JAVA_HOME%" >&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the >&2
|
||||
echo location of your Java installation. >&2
|
||||
echo.
|
||||
goto error
|
||||
|
||||
@REM ==== END VALIDATION ====
|
||||
|
||||
:init
|
||||
|
||||
@REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
|
||||
@REM Fallback to current working directory if not found.
|
||||
|
||||
set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
|
||||
IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
|
||||
|
||||
set EXEC_DIR=%CD%
|
||||
set WDIR=%EXEC_DIR%
|
||||
:findBaseDir
|
||||
IF EXIST "%WDIR%"\.mvn goto baseDirFound
|
||||
cd ..
|
||||
IF "%WDIR%"=="%CD%" goto baseDirNotFound
|
||||
set WDIR=%CD%
|
||||
goto findBaseDir
|
||||
|
||||
:baseDirFound
|
||||
set MAVEN_PROJECTBASEDIR=%WDIR%
|
||||
cd "%EXEC_DIR%"
|
||||
goto endDetectBaseDir
|
||||
|
||||
:baseDirNotFound
|
||||
set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
|
||||
cd "%EXEC_DIR%"
|
||||
|
||||
:endDetectBaseDir
|
||||
|
||||
IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
|
||||
|
||||
@setlocal EnableExtensions EnableDelayedExpansion
|
||||
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
|
||||
@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
|
||||
|
||||
:endReadAdditionalConfig
|
||||
|
||||
SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
|
||||
set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
|
||||
set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
|
||||
|
||||
set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
|
||||
|
||||
FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
|
||||
IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
|
||||
)
|
||||
|
||||
@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
|
||||
@REM This allows using the maven wrapper in projects that prohibit checking in binary data.
|
||||
if exist %WRAPPER_JAR% (
|
||||
if "%MVNW_VERBOSE%" == "true" (
|
||||
echo Found %WRAPPER_JAR%
|
||||
)
|
||||
) else (
|
||||
if not "%MVNW_REPOURL%" == "" (
|
||||
SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
|
||||
)
|
||||
if "%MVNW_VERBOSE%" == "true" (
|
||||
echo Couldn't find %WRAPPER_JAR%, downloading it ...
|
||||
echo Downloading from: %DOWNLOAD_URL%
|
||||
)
|
||||
|
||||
powershell -Command "&{"^
|
||||
"$webclient = new-object System.Net.WebClient;"^
|
||||
"if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
|
||||
"$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
|
||||
"}"^
|
||||
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^
|
||||
"}"
|
||||
if "%MVNW_VERBOSE%" == "true" (
|
||||
echo Finished downloading %WRAPPER_JAR%
|
||||
)
|
||||
)
|
||||
@REM End of extension
|
||||
|
||||
@REM Provide a "standardized" way to retrieve the CLI args that will
|
||||
@REM work with both Windows and non-Windows executions.
|
||||
set MAVEN_CMD_LINE_ARGS=%*
|
||||
|
||||
%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
|
||||
if ERRORLEVEL 1 goto error
|
||||
goto end
|
||||
|
||||
:error
|
||||
set ERROR_CODE=1
|
||||
|
||||
:end
|
||||
@endlocal & set ERROR_CODE=%ERROR_CODE%
|
||||
|
||||
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
|
||||
@REM check for post script, once with legacy .bat ending and once with .cmd ending
|
||||
if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
|
||||
if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
|
||||
:skipRcPost
|
||||
|
||||
@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
|
||||
if "%MAVEN_BATCH_PAUSE%" == "on" pause
|
||||
|
||||
if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
|
||||
|
||||
exit /B %ERROR_CODE%
|
||||
@@ -0,0 +1,128 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.6.0-M3</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.example.grpc</groupId>
|
||||
<artifactId>function-sample-grpc-cloudevent</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>function-sample-grpc-cloudevent</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-function-grpc-cloudevent-ext</artifactId>
|
||||
<version>3.2.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-netty</artifactId>
|
||||
<version>1.16.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-protobuf</artifactId>
|
||||
<version>1.16.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-stub</artifactId>
|
||||
<version>1.16.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<extensions>
|
||||
<extension>
|
||||
<groupId>kr.motd.maven</groupId>
|
||||
<artifactId>os-maven-plugin</artifactId>
|
||||
<version>1.6.1</version>
|
||||
</extension>
|
||||
</extensions>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.xolstice.maven.plugins</groupId>
|
||||
<artifactId>protobuf-maven-plugin</artifactId>
|
||||
<version>0.6.1</version>
|
||||
<configuration>
|
||||
<protocArtifact>
|
||||
com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier}
|
||||
</protocArtifact>
|
||||
<pluginId>grpc-java</pluginId>
|
||||
<pluginArtifact>
|
||||
io.grpc:protoc-gen-grpc-java:1.4.0:exe:${os.detected.classifier}
|
||||
</pluginArtifact>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
<goal>compile-custom</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.example.grpc.demo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.function.grpc.MessagingServiceGrpc;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import com.google.protobuf.DescriptorProtos.FileDescriptorProto;
|
||||
import com.google.protobuf.DescriptorProtos.FileDescriptorSet;
|
||||
import com.google.protobuf.Descriptors.FileDescriptor;
|
||||
import com.google.protobuf.ProtocolStringList;
|
||||
|
||||
import io.cloudevents.v1.CloudEventServiceGrpc;
|
||||
import io.cloudevents.v1.proto.CloudEvent;
|
||||
import io.cloudevents.v1.proto.CloudEvent.CloudEventAttributeValue;
|
||||
import io.grpc.ManagedChannel;
|
||||
import io.grpc.ManagedChannelBuilder;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoGrpcApplication {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
SpringApplication.run(DemoGrpcApplication.class, args);
|
||||
|
||||
CloudEvent cloudEvent = CloudEvent.newBuilder()
|
||||
.setTextData("{\"event_name\":\"SCF supports CloudEvent gRPC\"}")
|
||||
.setSource("http://springsource.com")
|
||||
.setId("12345")
|
||||
.setSpecVersion("1.0")
|
||||
.setType("org.springframework")
|
||||
.putAttributes("name", CloudEventAttributeValue.newBuilder().setCeString("oleg").build())
|
||||
.putAttributes("fluent_in_french", CloudEventAttributeValue.newBuilder().setCeBoolean(false).build())
|
||||
.build();
|
||||
|
||||
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 6048)
|
||||
.usePlaintext().build();
|
||||
|
||||
CloudEventServiceGrpc.CloudEventServiceBlockingStub stub = CloudEventServiceGrpc.newBlockingStub(channel);
|
||||
CloudEvent reply = stub.requestReply(cloudEvent);
|
||||
System.out.println(reply);
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<Message<String>, Message<String>> uppercase() {
|
||||
return message -> {
|
||||
return MessageBuilder.withPayload(message.getPayload().toUpperCase())
|
||||
.copyHeaders(message.getHeaders())
|
||||
.setHeader("uppercased", "true")
|
||||
.build();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package io.cloudevents.v1;
|
||||
|
||||
import "google/protobuf/any.proto";
|
||||
import "google/protobuf/timestamp.proto";
|
||||
|
||||
option go_package = "cloudevents.io/genproto/v1";
|
||||
option java_package = "io.cloudevents.v1.proto";
|
||||
option java_multiple_files = true;
|
||||
|
||||
message CloudEvent {
|
||||
|
||||
// -- CloudEvent Context Attributes
|
||||
|
||||
// Required Attributes
|
||||
string id = 1;
|
||||
string source = 2; // URI-reference
|
||||
string spec_version = 3;
|
||||
string type = 4;
|
||||
|
||||
// Optional & Extension Attributes
|
||||
map<string, CloudEventAttributeValue> attributes = 5;
|
||||
|
||||
// -- CloudEvent Data (Bytes, Text, or Proto)
|
||||
oneof data {
|
||||
bytes binary_data = 6;
|
||||
string text_data = 7;
|
||||
google.protobuf.Any proto_data = 8;
|
||||
}
|
||||
|
||||
/**
|
||||
* The CloudEvent specification defines
|
||||
* seven attribute value types...
|
||||
*/
|
||||
|
||||
message CloudEventAttributeValue {
|
||||
|
||||
oneof attr {
|
||||
bool ce_boolean = 1;
|
||||
int32 ce_integer = 2;
|
||||
string ce_string = 3;
|
||||
bytes ce_bytes = 4;
|
||||
string ce_uri = 5;
|
||||
string ce_uri_ref = 6;
|
||||
google.protobuf.Timestamp ce_timestamp = 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package io.cloudevents.v1;
|
||||
|
||||
import "google/protobuf/any.proto";
|
||||
import "google/protobuf/timestamp.proto";
|
||||
import "CloudEvent.proto";
|
||||
|
||||
service CloudEventService {
|
||||
rpc biStream(stream io.cloudevents.v1.CloudEvent) returns (stream io.cloudevents.v1.CloudEvent);
|
||||
|
||||
rpc clientStream(stream io.cloudevents.v1.CloudEvent) returns (io.cloudevents.v1.CloudEvent);
|
||||
|
||||
rpc serverStream(io.cloudevents.v1.CloudEvent) returns (stream io.cloudevents.v1.CloudEvent);
|
||||
|
||||
rpc requestReply(io.cloudevents.v1.CloudEvent) returns (io.cloudevents.v1.CloudEvent);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.grpc.demo;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class DemoGrpcApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user