Skip to content

Commit 044a048

Browse files
author
Kostromin, Maksim
committed
Initial commit
0 parents commit 044a048

File tree

28 files changed

+1584
-0
lines changed

28 files changed

+1584
-0
lines changed

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
**/src/main/gen/**
2+
3+
target/
4+
!.mvn/wrapper/maven-wrapper.jar
5+
!**/src/main/**/target/
6+
!**/src/test/**/target/
7+
8+
### STS ###
9+
.apt_generated
10+
.classpath
11+
.factorypath
12+
.project
13+
.settings
14+
.springBeans
15+
.sts4-cache
16+
17+
### IntelliJ IDEA ###
18+
.idea
19+
*.iws
20+
*.iml
21+
*.ipr
22+
23+
### NetBeans ###
24+
/nbproject/private/
25+
/nbbuild/
26+
/dist/
27+
/nbdist/
28+
/.nb-gradle/
29+
build/
30+
!**/src/main/**/build/
31+
!**/src/test/**/build/
32+
33+
### VS Code ###
34+
.vscode/
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2007-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import java.net.*;
18+
import java.io.*;
19+
import java.nio.channels.*;
20+
import java.util.Properties;
21+
22+
public class MavenWrapperDownloader {
23+
24+
private static final String WRAPPER_VERSION = "0.5.6";
25+
/**
26+
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
27+
*/
28+
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
29+
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
30+
31+
/**
32+
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
33+
* use instead of the default one.
34+
*/
35+
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
36+
".mvn/wrapper/maven-wrapper.properties";
37+
38+
/**
39+
* Path where the maven-wrapper.jar will be saved to.
40+
*/
41+
private static final String MAVEN_WRAPPER_JAR_PATH =
42+
".mvn/wrapper/maven-wrapper.jar";
43+
44+
/**
45+
* Name of the property which should be used to override the default download url for the wrapper.
46+
*/
47+
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
48+
49+
public static void main(String args[]) {
50+
System.out.println("- Downloader started");
51+
File baseDirectory = new File(args[0]);
52+
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
53+
54+
// If the maven-wrapper.properties exists, read it and check if it contains a custom
55+
// wrapperUrl parameter.
56+
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
57+
String url = DEFAULT_DOWNLOAD_URL;
58+
if (mavenWrapperPropertyFile.exists()) {
59+
FileInputStream mavenWrapperPropertyFileInputStream = null;
60+
try {
61+
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
62+
Properties mavenWrapperProperties = new Properties();
63+
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
64+
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
65+
} catch (IOException e) {
66+
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
67+
} finally {
68+
try {
69+
if (mavenWrapperPropertyFileInputStream != null) {
70+
mavenWrapperPropertyFileInputStream.close();
71+
}
72+
} catch (IOException e) {
73+
// Ignore ...
74+
}
75+
}
76+
}
77+
System.out.println("- Downloading from: " + url);
78+
79+
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
80+
if (!outputFile.getParentFile().exists()) {
81+
if (!outputFile.getParentFile().mkdirs()) {
82+
System.out.println(
83+
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
84+
}
85+
}
86+
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
87+
try {
88+
downloadFileFromURL(url, outputFile);
89+
System.out.println("Done");
90+
System.exit(0);
91+
} catch (Throwable e) {
92+
System.out.println("- Error downloading");
93+
e.printStackTrace();
94+
System.exit(1);
95+
}
96+
}
97+
98+
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
99+
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
100+
String username = System.getenv("MVNW_USERNAME");
101+
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
102+
Authenticator.setDefault(new Authenticator() {
103+
@Override
104+
protected PasswordAuthentication getPasswordAuthentication() {
105+
return new PasswordAuthentication(username, password);
106+
}
107+
});
108+
}
109+
URL website = new URL(urlString);
110+
ReadableByteChannel rbc;
111+
rbc = Channels.newChannel(website.openStream());
112+
FileOutputStream fos = new FileOutputStream(destination);
113+
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
114+
fos.close();
115+
rbc.close();
116+
}
117+
118+
}

.mvn/wrapper/maven-wrapper.jar

49.5 KB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.2/apache-maven-3.8.2-bin.zip
2+
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Spring cloud stream + RabbitMQ + Protobuf
2+
This repository contains RabbitMQ Protobuf starters with its usage
3+
samples for `spring-rabbit` and
4+
`spring-cloud-starter-stream-rabbit` modules
5+
6+
## Quickstart
7+
8+
```bash
9+
git clone --depth=0 https://github.com/daggerok/spring-cloud-stream-protobuf-rabbitmq.git my-app && cd $_
10+
```
11+
12+
```bash
13+
./mvnw -f rabbitmq docker:start
14+
./mvnw clean test
15+
./mvnw -f rabbitmq docker:stop docker:remove
16+
```
17+
18+
## Integration testing
19+
20+
```bash
21+
rm -rf ~/.m2/repository/com/github/daggerok
22+
./mvnw -f rabbitmq docker:start
23+
./mvnw clean install -DskipTests
24+
./mvnw -f consumer spring-boot:run # to create durable, then CTRL+C
25+
./mvnw -f producer spring-boot:run # then in a separate terminal:
26+
http :8080 message="Hello, World" # and press CTRL+C for producer
27+
./mvnw -f consumer spring-boot:run # check message and press CTRL+C
28+
./mvnw -f rabbitmq docker:stop docker:remove
29+
```
30+
31+
<!--
32+
33+
# Getting Started
34+
35+
### Reference Documentation
36+
37+
For further reference, please consider the following sections:
38+
39+
* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html)
40+
* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.5.5/maven-plugin/reference/html/)
41+
* [Create an OCI image](https://docs.spring.io/spring-boot/docs/2.5.5/maven-plugin/reference/html/#build-image)
42+
* [Spring Web](https://docs.spring.io/spring-boot/docs/2.5.5/reference/htmlsingle/#boot-features-developing-web-applications)
43+
44+
### Guides
45+
46+
The following guides illustrate how to use some features concretely:
47+
48+
* [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/)
49+
* [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/)
50+
* [Building REST services with Spring](https://spring.io/guides/tutorials/bookmarks/)
51+
52+
-->

api/pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>com.github.daggerok.protorabbit</groupId>
7+
<artifactId>spring-cloud-stream-protobuf-rabbitmq</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<packaging>jar</packaging>
12+
<modelVersion>4.0.0</modelVersion>
13+
<artifactId>api</artifactId>
14+
<name>${project.parent.artifactId}:${project.artifactId}</name>
15+
<description>Spring Cloud Stream Protobuf RabbitMQ api</description>
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.google.protobuf</groupId>
19+
<artifactId>protobuf-java</artifactId>
20+
<version>${protobuf-java.version}</version>
21+
</dependency>
22+
</dependencies>
23+
<build>
24+
<plugins>
25+
<!-- cleanup src/main/gen on maven clean goal -->
26+
<plugin>
27+
<artifactId>maven-clean-plugin</artifactId>
28+
</plugin>
29+
<!-- add src/main/gen as an additional java maven sources folder -->
30+
<plugin>
31+
<groupId>org.codehaus.mojo</groupId>
32+
<artifactId>build-helper-maven-plugin</artifactId>
33+
</plugin>
34+
<!-- src/main/proto/**/*.proto -> src/main/gen/**/*.java -->
35+
<plugin>
36+
<groupId>com.github.os72</groupId>
37+
<artifactId>protoc-jar-maven-plugin</artifactId>
38+
</plugin>
39+
</plugins>
40+
</build>
41+
</project>

api/src/main/proto/api.proto

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
syntax = "proto2";
2+
3+
package com.github.daggerok.protorabbit;
4+
5+
option java_package = "com.github.daggerok.protorabbit";
6+
option java_outer_classname = "ApiProtos";
7+
8+
message Greeting {
9+
optional string message = 1;
10+
}

consumer/pom.xml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>com.github.daggerok.protorabbit</groupId>
7+
<artifactId>spring-cloud-stream-protobuf-rabbitmq</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<packaging>jar</packaging>
12+
<modelVersion>4.0.0</modelVersion>
13+
<artifactId>consumer</artifactId>
14+
<name>${project.parent.artifactId}:${project.artifactId}</name>
15+
<description>Spring Cloud Stream Protobuf RabbitMQ consumer app</description>
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.github.daggerok.protorabbit</groupId>
19+
<artifactId>spring-cloud-stream-rabbit-protobuf-starter</artifactId>
20+
<version>${project.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
<!---->
27+
<dependency>
28+
<groupId>com.github.daggerok.protorabbit</groupId>
29+
<artifactId>api</artifactId>
30+
<version>${project.version}</version>
31+
</dependency>
32+
<!---->
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.cloud</groupId>
40+
<artifactId>spring-cloud-stream</artifactId>
41+
<scope>test</scope>
42+
<classifier>test-binder</classifier>
43+
<type>test-jar</type>
44+
</dependency>
45+
</dependencies>
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-maven-plugin</artifactId>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.github.daggerok.spring_cloud_stream_rabbitmq_protobuf
2+
3+
import com.github.daggerok.protorabbit.ApiProtos.Greeting
4+
import java.util.function.Consumer
5+
import org.apache.logging.log4j.kotlin.logger
6+
import org.springframework.boot.autoconfigure.SpringBootApplication
7+
import org.springframework.boot.runApplication
8+
import org.springframework.context.annotation.Bean
9+
import org.springframework.context.annotation.Configuration
10+
11+
@Configuration
12+
class ConsumerConfig {
13+
14+
@Bean
15+
fun consumingFunction() = Consumer<Greeting> {
16+
log.info { "Consuming: $it" }
17+
}
18+
19+
companion object {
20+
val log = logger()
21+
}
22+
}
23+
24+
@SpringBootApplication
25+
class ConsumerApplication
26+
27+
fun main(args: Array<String>) {
28+
runApplication<ConsumerApplication>(*args)
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
spring:
2+
application:
3+
name: consumer
4+
output:
5+
ansi:
6+
enabled: always
7+
rabbitmq:
8+
host: 127.0.0.1
9+
port: 5672
10+
virtual-host: /
11+
username: guest
12+
password: guest
13+
cloud:
14+
function:
15+
definition: consumingFunction
16+
stream:
17+
bindings:
18+
consumingFunction-in-0:
19+
destination: myDestination
20+
group: consumingFunctionGroup@${spring.application.name}
21+
content-type: application/x-protobuf
22+
default-binder: rabbit
23+
rabbit:
24+
bindings:
25+
consumingFunction-in-0:
26+
consumer:
27+
durableSubscription: true
28+
missingQueuesFatal: true
29+
maxConcurrency: 1
30+
batchSize: 1
31+
prefetch: 1
32+
server:
33+
port: 0

0 commit comments

Comments
 (0)