├── .gitignore ├── README.md ├── demo-go-sink ├── consumer.go ├── main.go └── model.go ├── demo-java-source ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ ├── MessageDto.java │ │ │ ├── SampleSource.java │ │ │ └── StreamApplication.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── DemoApplicationTests.java └── demo-kotlin-sink ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src └── main ├── kotlin └── com │ └── example │ ├── SampleSink.kt │ └── SinkApplication.kt └── resources └── application.properties /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Gradle template 3 | .gradle 4 | /build/ 5 | 6 | # Ignore Gradle GUI config 7 | gradle-app.setting 8 | 9 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 10 | !gradle-wrapper.jar 11 | 12 | # Cache of project 13 | .gradletasknamecache 14 | 15 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 16 | # gradle/wrapper/gradle-wrapper.properties 17 | ### JetBrains template 18 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 19 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 20 | 21 | # User-specific stuff: 22 | .idea/workspace.xml 23 | .idea/tasks.xml 24 | .idea/* 25 | 26 | # Sensitive or high-churn files: 27 | .idea/dataSources/ 28 | .idea/dataSources.ids 29 | .idea/dataSources.xml 30 | .idea/dataSources.local.xml 31 | .idea/sqlDataSources.xml 32 | .idea/dynamic.xml 33 | .idea/uiDesigner.xml 34 | *idea* 35 | 36 | # Gradle: 37 | .idea/gradle.xml 38 | .idea/libraries 39 | build 40 | target 41 | lib 42 | 43 | # Mongo Explorer plugin: 44 | .idea/mongoSettings.xml 45 | 46 | ## File-based project format: 47 | *.iws 48 | 49 | ## Plugin-specific files: 50 | 51 | # IntelliJ 52 | /out/ 53 | 54 | # mpeltonen/sbt-idea plugin 55 | .idea_modules/ 56 | 57 | # JIRA plugin 58 | atlassian-ide-plugin.xml 59 | 60 | # Crashlytics plugin (for Android Studio and IntelliJ) 61 | com_crashlytics_export_strings.xml 62 | crashlytics.properties 63 | crashlytics-build.properties 64 | fabric.properties 65 | 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # About 2 | 'Hello world' Kafka integration of Java, Kotlin and Go apps 3 | 4 | - demo-java-source with Spring Cloud Stream - localhost:8080 5 | - demo-kotlin-sink with Spring Cloud Stream - localhost:8081 (Consumer) 6 | - demo-go-sink with Shopify/sarama (Kafka Client) - localhost:8082 (Consumer) 7 | - Apache Kafka as Message Broker 8 | - Go Http/2 Push example (just for fun) 9 | 10 | Running Kafka from docker: 11 | ``` 12 | docker run --name spotifykafka -p 2181:2181 -p 9092:9092 --env ADVERTISED_HOST=localhost --env ADVERTISED_PORT=9092 spotify/kafka 13 | ``` 14 | 15 | to get Shopify/sarama lib: 16 | ``` 17 | go get github.com/Shopify/sarama 18 | ``` 19 | 20 | # How to run 21 | 1. Run docker spotify/kafka 22 | 1. Run Java/Kotlin/Go app from IDE (Prefer IDEA & Gogland) 23 | 1. Send request to Java Source App - POST http://localhost:8080/message {"name": "Krzysztof"} 24 | 1. Both Go and Kotlin app will print the messages on their consoles, something like this: 25 | ``` 26 | INFO 22360 --- [afka-listener-1] com.example.SampleSink Kotlin received: Krzysztof 27 | ``` 28 | 29 | ``` 30 | Golang received: Krzysztof 31 | 32 | ``` 33 | # Http/2 Go Push 34 | 1. Go 1.8beta2 required 35 | 1. Run crypto/tls/generate_cert.go to generate cert.pem and key.pem. and put them to the project 36 | 1. Go to https://localhost:8082/ (yes https) 37 | 1. Resources will be pushed to browser, check Network (devtools) 38 | 39 | 40 | # References 41 | 1. https://medium.com/@Oskarr3/implementing-cqrs-using-kafka-and-sarama-library-in-golang-da7efa3b77fe#.razrnz8eh 42 | 1. https://blog.codecentric.de/en/2016/04/event-driven-microservices-spring-cloud-stream/ 43 | 1. [Go 1.8 Http/2 Push](https://gist.github.com/rakyll/eec415977f85d50a493ca8472ba97b68) 44 | 45 | # TODO 46 | 1. Make it more interesting... because printing to console is not cool enough ;) 47 | 1. Add SSE/ Websockets ? 48 | 49 | 50 | -------------------------------------------------------------------------------- /demo-go-sink/consumer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "encoding/json" 7 | 8 | "github.com/Shopify/sarama" 9 | ) 10 | 11 | var brokers = []string{"localhost:9092"} 12 | 13 | type Message struct { 14 | Name string `json:"name"` 15 | } 16 | 17 | func subscribe(topic string, consumer sarama.Consumer) { 18 | partitionList, err := consumer.Partitions(topic) //get all partitions on the given topic 19 | if err != nil { 20 | fmt.Println("Error retrieving partitionList ", err) 21 | } 22 | initialOffset := sarama.OffsetNewest //get offset for the newest message on the topic 23 | 24 | for _, partition := range partitionList { 25 | pc, _ := consumer.ConsumePartition(topic, partition, initialOffset) 26 | 27 | go func(pc sarama.PartitionConsumer) { 28 | for message := range pc.Messages() { 29 | messageReceived(message) 30 | } 31 | }(pc) 32 | } 33 | } 34 | 35 | func messageReceived(message *sarama.ConsumerMessage) { 36 | var m Message 37 | 38 | err := json.Unmarshal(message.Value, &m) 39 | if err != nil { 40 | println("JSON parsing error ", err) 41 | } 42 | println("Golang received: " + m.Name) 43 | 44 | saveMessage(m.Name) 45 | } 46 | -------------------------------------------------------------------------------- /demo-go-sink/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net/http" 7 | 8 | "github.com/Shopify/sarama" 9 | ) 10 | 11 | const ( 12 | topic = "messages" 13 | contentType = "Content-Type" 14 | applicationJSON = "application/json" 15 | ) 16 | 17 | func html(text string) string { 18 | html := ` 19 | 20 | Hello 21 | 22 | 23 | Received: ` + text + 24 | ` 25 | 26 | ` 27 | return html 28 | 29 | } 30 | 31 | func main() { 32 | http.HandleFunc("/main.js", func(w http.ResponseWriter, r *http.Request) { 33 | 34 | mainJS := `alert("` + getMessage() + `");` 35 | fmt.Fprintf(w, mainJS) 36 | }) 37 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 38 | if r.URL.Path != "/" { 39 | http.NotFound(w, r) 40 | return 41 | } 42 | pusher, ok := w.(http.Pusher) 43 | if ok { 44 | if err := pusher.Push("/main.js", nil); err != nil { 45 | log.Printf("Failed to push: %v", err) 46 | } 47 | } 48 | fmt.Fprintf(w, html(getMessage())) 49 | }) 50 | 51 | config := sarama.NewConfig() 52 | config.Consumer.Return.Errors = true 53 | consumer, err := sarama.NewConsumer(brokers, config) 54 | if err != nil { 55 | fmt.Println("Could not create consumer: ", err) 56 | } 57 | subscribe(topic, consumer) 58 | 59 | log.Fatal(http.ListenAndServeTLS(":8082", "cert.pem", "key.pem", nil)) 60 | } 61 | -------------------------------------------------------------------------------- /demo-go-sink/model.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | var message string 4 | 5 | func saveMessage(text string) { message = text } 6 | 7 | func getMessage() string { return message } 8 | -------------------------------------------------------------------------------- /demo-java-source/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '1.4.3.RELEASE' 4 | } 5 | repositories { 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 | } 11 | } 12 | 13 | apply plugin: 'java' 14 | apply plugin: 'eclipse' 15 | apply plugin: 'org.springframework.boot' 16 | 17 | jar { 18 | baseName = 'demo-java-source' 19 | version = '0.0.1-SNAPSHOT' 20 | } 21 | 22 | sourceCompatibility = 1.8 23 | 24 | repositories { 25 | mavenCentral() 26 | } 27 | 28 | 29 | dependencies { 30 | compile('org.springframework.cloud:spring-cloud-starter-stream-kafka') 31 | testCompile('org.springframework.boot:spring-boot-starter-test') 32 | } 33 | 34 | dependencyManagement { 35 | imports { 36 | mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR3" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /demo-java-source/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kgoralski/go-kotlin-java-kafka/342ba9d8ddea7cece2b9f155fca9522327c2fe68/demo-java-source/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /demo-java-source/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | zipStoreBase=GRADLE_USER_HOME 4 | zipStorePath=wrapper/dists 5 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip 6 | -------------------------------------------------------------------------------- /demo-java-source/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn ( ) { 37 | echo "$*" 38 | } 39 | 40 | die ( ) { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /demo-java-source/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /demo-java-source/src/main/java/com/example/MessageDto.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | class MessageDto { 4 | 5 | 6 | String name; 7 | 8 | public String getName() { 9 | return name; 10 | } 11 | 12 | public void setName(String name) { 13 | this.name = name; 14 | } 15 | } -------------------------------------------------------------------------------- /demo-java-source/src/main/java/com/example/SampleSource.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.cloud.stream.annotation.EnableBinding; 5 | import org.springframework.cloud.stream.messaging.Source; 6 | import org.springframework.http.MediaType; 7 | import org.springframework.messaging.MessageChannel; 8 | import org.springframework.messaging.support.GenericMessage; 9 | import org.springframework.web.bind.annotation.PostMapping; 10 | import org.springframework.web.bind.annotation.RequestBody; 11 | import org.springframework.web.bind.annotation.ResponseBody; 12 | import org.springframework.web.bind.annotation.RestController; 13 | 14 | import com.fasterxml.jackson.core.JsonProcessingException; 15 | import com.fasterxml.jackson.databind.ObjectMapper; 16 | 17 | @RestController 18 | @EnableBinding(Source.class) 19 | public class SampleSource { 20 | 21 | private final MessageChannel output; 22 | 23 | @Autowired 24 | public SampleSource(MessageChannel output) { 25 | this.output = output; 26 | } 27 | 28 | private ObjectMapper objectMapper = new ObjectMapper(); 29 | 30 | @PostMapping(path = "/message", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) 31 | public @ResponseBody void sendMessage(@RequestBody MessageDto message) throws JsonProcessingException { 32 | byte[] bytes = objectMapper.writeValueAsBytes(message); 33 | 34 | // normally you can use something like this instead of bytes : 35 | // MessageBuilder.withPayload(name).build() 36 | output.send(new GenericMessage<>(bytes)); 37 | } 38 | } -------------------------------------------------------------------------------- /demo-java-source/src/main/java/com/example/StreamApplication.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.stream.annotation.EnableBinding; 6 | import org.springframework.cloud.stream.messaging.Source; 7 | 8 | @SpringBootApplication 9 | @EnableBinding(Source.class) 10 | public class StreamApplication { 11 | 12 | public static void main(String[] args) { 13 | SpringApplication.run(StreamApplication.class, args); 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /demo-java-source/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.cloud.stream.bindings.output.destination=messages 2 | #spring.cloud.stream.bindings.output.content-type=application/json 3 | 4 | # for integrating with non spring apps 5 | spring.cloud.stream.bindings.output.producer.headerMode=raw 6 | spring.cloud.stream.kafka.binder.zkNodes=localhost 7 | spring.cloud.stream.kafka.binder.brokers=localhost 8 | 9 | server.port=8080 -------------------------------------------------------------------------------- /demo-java-source/src/test/java/com/example/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /demo-kotlin-sink/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.0.6' 3 | ext { 4 | springBootVersion = '1.4.3.RELEASE' 5 | } 6 | repositories { 7 | mavenCentral() 8 | } 9 | dependencies { 10 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | apply plugin: 'eclipse' 18 | apply plugin: 'org.springframework.boot' 19 | 20 | jar { 21 | baseName = 'demo-kotlin-sink' 22 | version = '0.0.1-SNAPSHOT' 23 | } 24 | 25 | sourceCompatibility = 1.8 26 | 27 | repositories { 28 | mavenCentral() 29 | } 30 | 31 | 32 | 33 | 34 | dependencies { 35 | compile('org.springframework.cloud:spring-cloud-starter-stream-kafka') 36 | compile("com.fasterxml.jackson.module:jackson-module-kotlin:2.8.4") 37 | testCompile('org.springframework.boot:spring-boot-starter-test') 38 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 39 | } 40 | 41 | dependencyManagement { 42 | imports { 43 | mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR3" 44 | } 45 | } 46 | sourceSets { 47 | main.java.srcDirs += 'src/main/kotlin' 48 | } 49 | -------------------------------------------------------------------------------- /demo-kotlin-sink/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kgoralski/go-kotlin-java-kafka/342ba9d8ddea7cece2b9f155fca9522327c2fe68/demo-kotlin-sink/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /demo-kotlin-sink/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Jan 07 17:05:33 CET 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-bin.zip 7 | -------------------------------------------------------------------------------- /demo-kotlin-sink/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn ( ) { 37 | echo "$*" 38 | } 39 | 40 | die ( ) { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 165 | if [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]]; then 166 | cd "$(dirname "$0")" 167 | fi 168 | 169 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 170 | -------------------------------------------------------------------------------- /demo-kotlin-sink/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /demo-kotlin-sink/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'demo-kotlin-sink' 2 | 3 | -------------------------------------------------------------------------------- /demo-kotlin-sink/src/main/kotlin/com/example/SampleSink.kt: -------------------------------------------------------------------------------- 1 | package com.example 2 | 3 | import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper 4 | import org.slf4j.LoggerFactory 5 | import org.springframework.cloud.stream.annotation.EnableBinding 6 | import org.springframework.cloud.stream.annotation.StreamListener 7 | import org.springframework.cloud.stream.messaging.Sink 8 | import org.springframework.stereotype.Component 9 | 10 | @Component 11 | @EnableBinding(Sink::class) 12 | open class SampleSink { 13 | 14 | // jackson kotlin module 15 | val JSON = jacksonObjectMapper() 16 | 17 | data class Message(var name: String) 18 | 19 | private val logger = LoggerFactory.getLogger(SampleSink::class.java) 20 | @StreamListener(Sink.INPUT) 21 | fun receive(message: ByteArray) { 22 | val parsedMessage = JSON.readValue(message, Message::class.java) 23 | logger.info("Kotlin received: " + parsedMessage.name) 24 | } 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /demo-kotlin-sink/src/main/kotlin/com/example/SinkApplication.kt: -------------------------------------------------------------------------------- 1 | package com.example 2 | 3 | 4 | import org.springframework.boot.SpringApplication 5 | import org.springframework.boot.autoconfigure.SpringBootApplication 6 | 7 | @SpringBootApplication 8 | open class SinkApplication { 9 | 10 | 11 | companion object { 12 | 13 | @JvmStatic fun main(args: Array) { 14 | SpringApplication.run(SinkApplication::class.java, *args) 15 | } 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /demo-kotlin-sink/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.cloud.stream.bindings.input.destination=messages 2 | #spring.cloud.stream.bindings.input.content-type=application/json 3 | 4 | # for integrating with non spring apps 5 | spring.cloud.stream.bindings.input.consumer.headerMode=raw 6 | spring.cloud.stream.bindings.input.group=messageGroup 7 | spring.cloud.stream.kafka.bindings.input.consumer.resetOffsets=true 8 | spring.cloud.stream.kafka.binder.zkNodes=localhost 9 | spring.cloud.stream.kafka.binder.brokers=localhost 10 | 11 | server.port=8081 --------------------------------------------------------------------------------