Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-15958
This commit is contained in:
commit
9867e44b7d
@ -30,6 +30,19 @@
|
|||||||
<artifactId>guava</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
<version>${guava.version}</version>
|
<version>${guava.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>pl.pragmatists</groupId>
|
||||||
|
<artifactId>JUnitParams</artifactId>
|
||||||
|
<version>1.1.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.baeldung.algorithms.printtriangles;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
public class PrintTriangleExamples {
|
||||||
|
|
||||||
|
public static String printARightAngledTriangle(int N) {
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
for (int r = 1; r <= N; r++) {
|
||||||
|
for (int j = 1; j <= r; j++) {
|
||||||
|
result.append("*");
|
||||||
|
}
|
||||||
|
result.append(System.lineSeparator());
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String printAnIsoscelesTriangle(int N) {
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
for (int r = 1; r <= N; r++) {
|
||||||
|
for (int sp = 1; sp <= N - r; sp++) {
|
||||||
|
result.append(" ");
|
||||||
|
}
|
||||||
|
for (int c = 1; c <= (r * 2) - 1; c++) {
|
||||||
|
result.append("*");
|
||||||
|
}
|
||||||
|
result.append(System.lineSeparator());
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String printAnIsoscelesTriangleUsingSubstring(int N) {
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
String helperString = StringUtils.repeat(' ', N - 1) + StringUtils.repeat('*', N * 2 - 1);
|
||||||
|
|
||||||
|
for (int r = 0; r < N; r++) {
|
||||||
|
result.append(helperString.substring(r, N + 2 * r));
|
||||||
|
result.append(System.lineSeparator());
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(printARightAngledTriangle(5));
|
||||||
|
System.out.println(printAnIsoscelesTriangle(5));
|
||||||
|
System.out.println(printAnIsoscelesTriangleUsingSubstring(5));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,93 @@
|
|||||||
|
package com.baeldung.algorithms.printtriangles;
|
||||||
|
|
||||||
|
import junitparams.JUnitParamsRunner;
|
||||||
|
import junitparams.Parameters;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
@RunWith(JUnitParamsRunner.class)
|
||||||
|
public class PrintTriangleExamplesUnitTest {
|
||||||
|
|
||||||
|
private static Object[][] rightAngledTriangles() {
|
||||||
|
String expected0 = "";
|
||||||
|
|
||||||
|
String expected2 = "*" + System.lineSeparator()
|
||||||
|
+ "**" + System.lineSeparator();
|
||||||
|
|
||||||
|
String expected5 = "*" + System.lineSeparator()
|
||||||
|
+ "**" + System.lineSeparator()
|
||||||
|
+ "***" + System.lineSeparator()
|
||||||
|
+ "****" + System.lineSeparator()
|
||||||
|
+ "*****" + System.lineSeparator();
|
||||||
|
|
||||||
|
String expected7 = "*" + System.lineSeparator()
|
||||||
|
+ "**" + System.lineSeparator()
|
||||||
|
+ "***" + System.lineSeparator()
|
||||||
|
+ "****" + System.lineSeparator()
|
||||||
|
+ "*****" + System.lineSeparator()
|
||||||
|
+ "******" + System.lineSeparator()
|
||||||
|
+ "*******" + System.lineSeparator();
|
||||||
|
|
||||||
|
return new Object[][] {
|
||||||
|
{ 0, expected0 },
|
||||||
|
{ 2, expected2 },
|
||||||
|
{ 5, expected5 },
|
||||||
|
{ 7, expected7 }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Parameters(method = "rightAngledTriangles")
|
||||||
|
public void whenPrintARightAngledTriangleIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) {
|
||||||
|
String actual = PrintTriangleExamples.printARightAngledTriangle(nrOfRows);
|
||||||
|
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Object[][] isoscelesTriangles() {
|
||||||
|
String expected0 = "";
|
||||||
|
|
||||||
|
String expected2 = " *" + System.lineSeparator()
|
||||||
|
+ "***" + System.lineSeparator();
|
||||||
|
|
||||||
|
String expected5 = " *" + System.lineSeparator()
|
||||||
|
+ " ***" + System.lineSeparator()
|
||||||
|
+ " *****" + System.lineSeparator()
|
||||||
|
+ " *******" + System.lineSeparator()
|
||||||
|
+ "*********" + System.lineSeparator();
|
||||||
|
|
||||||
|
String expected7 = " *" + System.lineSeparator()
|
||||||
|
+ " ***" + System.lineSeparator()
|
||||||
|
+ " *****" + System.lineSeparator()
|
||||||
|
+ " *******" + System.lineSeparator()
|
||||||
|
+ " *********" + System.lineSeparator()
|
||||||
|
+ " ***********" + System.lineSeparator()
|
||||||
|
+ "*************" + System.lineSeparator();
|
||||||
|
|
||||||
|
return new Object[][] {
|
||||||
|
{ 0, expected0 },
|
||||||
|
{ 2, expected2 },
|
||||||
|
{ 5, expected5 },
|
||||||
|
{ 7, expected7 }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Parameters(method = "isoscelesTriangles")
|
||||||
|
public void whenPrintAnIsoscelesTriangleIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) {
|
||||||
|
String actual = PrintTriangleExamples.printAnIsoscelesTriangle(nrOfRows);
|
||||||
|
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Parameters(method = "isoscelesTriangles")
|
||||||
|
public void whenPrintAnIsoscelesTriangleUsingSubstringIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) {
|
||||||
|
String actual = PrintTriangleExamples.printAnIsoscelesTriangleUsingSubstring(nrOfRows);
|
||||||
|
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
3
java-strings-3/README.md
Normal file
3
java-strings-3/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
## Relevant Articles:
|
||||||
|
|
||||||
|
- [Converting Java String to Double](https://www.baeldung.com/java-string-to-double)
|
155
java-strings-3/pom.xml
Normal file
155
java-strings-3/pom.xml
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>java-strings-3</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>java-strings-3</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-java</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../parent-java</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-core</artifactId>
|
||||||
|
<version>${jmh-core.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-generator-annprocess</artifactId>
|
||||||
|
<version>${jmh-core.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ibm.icu</groupId>
|
||||||
|
<artifactId>icu4j</artifactId>
|
||||||
|
<version>${icu4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava</artifactId>
|
||||||
|
<version>${guava.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${commons-lang3.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-io</groupId>
|
||||||
|
<artifactId>commons-io</artifactId>
|
||||||
|
<version>${commons-io.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-codec</groupId>
|
||||||
|
<artifactId>commons-codec</artifactId>
|
||||||
|
<version>${commons-codec.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-library</artifactId>
|
||||||
|
<version>${org.hamcrest.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Added for password generation -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.passay</groupId>
|
||||||
|
<artifactId>passay</artifactId>
|
||||||
|
<version>${passay.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-text</artifactId>
|
||||||
|
<version>${commons-text.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.vdurmont</groupId>
|
||||||
|
<artifactId>emoji-java</artifactId>
|
||||||
|
<version>${emoji-java.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.ahocorasick</groupId>
|
||||||
|
<artifactId>ahocorasick</artifactId>
|
||||||
|
<version>${ahocorasick.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.validation</groupId>
|
||||||
|
<artifactId>validation-api</artifactId>
|
||||||
|
<version>${validation-api.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate.validator</groupId>
|
||||||
|
<artifactId>hibernate-validator</artifactId>
|
||||||
|
<version>${hibernate-validator.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.el</groupId>
|
||||||
|
<artifactId>javax.el-api</artifactId>
|
||||||
|
<version>${javax.el-api.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish.web</groupId>
|
||||||
|
<artifactId>javax.el</artifactId>
|
||||||
|
<version>${javax.el.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>java-strings-3</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<source>${java.version}</source>
|
||||||
|
<target>${java.version}</target>
|
||||||
|
<compilerArgument>-parameters</compilerArgument>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<commons-lang3.version>3.8.1</commons-lang3.version>
|
||||||
|
<commons-codec.version>1.10</commons-codec.version>
|
||||||
|
<passay.version>1.3.1</passay.version>
|
||||||
|
<!-- testing -->
|
||||||
|
<assertj.version>3.6.1</assertj.version>
|
||||||
|
<emoji-java.version>4.0.0</emoji-java.version>
|
||||||
|
<ahocorasick.version>0.4.0</ahocorasick.version>
|
||||||
|
<icu4j.version>61.1</icu4j.version>
|
||||||
|
<guava.version>28.0-jre</guava.version>
|
||||||
|
<commons-text.version>1.4</commons-text.version>
|
||||||
|
<validation-api.version>2.0.0.Final</validation-api.version>
|
||||||
|
<hibernate-validator.version>6.0.2.Final</hibernate-validator.version>
|
||||||
|
<javax.el-api.version>3.0.0</javax.el-api.version>
|
||||||
|
<javax.el.version>2.2.6</javax.el.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,3 @@
|
|||||||
|
This file exists to ensure this empty directory is committed in Git.
|
||||||
|
|
||||||
|
Please remove this file when this directory is populated.
|
13
java-strings-3/src/main/resources/.gitignore
vendored
Normal file
13
java-strings-3/src/main/resources/.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
*.class
|
||||||
|
|
||||||
|
#folders#
|
||||||
|
/target
|
||||||
|
/neoDb*
|
||||||
|
/data
|
||||||
|
/src/main/webapp/WEB-INF/classes
|
||||||
|
*/META-INF/*
|
||||||
|
|
||||||
|
# Packaged files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.string.equalsIgnoreCase;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class StringEqualsIgnoreCaseUnitTest {
|
||||||
|
private String string1 = "equals ignore case";
|
||||||
|
private String string2 = "EQUALS IGNORE CASE";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEqualStringsWithDifferentCase_whenUsingEqualsIgnoreCase_ThenTheyAreEqual() {
|
||||||
|
assertThat(string1.equalsIgnoreCase(string2)).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEqualStringsWithDifferentCase_whenUsingApacheCommonsEqualsIgnoreCase_ThenTheyAreEqual() {
|
||||||
|
assertThat(StringUtils.equalsIgnoreCase(string1, string2)).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAStringAndNullValue_whenUsingApacheCommonsEqualsIgnoreCase_ThenTheyAreNotEqual() {
|
||||||
|
assertThat(StringUtils.equalsIgnoreCase(string1, null)).isFalse();
|
||||||
|
}
|
||||||
|
}
|
@ -1,56 +1,56 @@
|
|||||||
package com.baeldung.string.todouble;
|
package com.baeldung.string.todouble;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class StringToDoubleConversionUnitTest {
|
public class StringToDoubleConversionUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenValidString_WhenParseDouble_ThenResultIsPrimitiveDouble() {
|
public void givenValidString_WhenParseDouble_ThenResultIsPrimitiveDouble() {
|
||||||
assertEquals(1.23, Double.parseDouble("1.23"), 0.000001);
|
assertEquals(1.23, Double.parseDouble("1.23"), 0.000001);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = NullPointerException.class)
|
@Test(expected = NullPointerException.class)
|
||||||
public void givenNullString_WhenParseDouble_ThenNullPointerExceptionIsThrown() {
|
public void givenNullString_WhenParseDouble_ThenNullPointerExceptionIsThrown() {
|
||||||
Double.parseDouble(null);
|
Double.parseDouble(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = NumberFormatException.class)
|
@Test(expected = NumberFormatException.class)
|
||||||
public void givenInalidString_WhenParseDouble_ThenNumberFormatExceptionIsThrown() {
|
public void givenInalidString_WhenParseDouble_ThenNumberFormatExceptionIsThrown() {
|
||||||
Double.parseDouble("&");
|
Double.parseDouble("&");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenValidString_WhenValueOf_ThenResultIsPrimitiveDouble() {
|
public void givenValidString_WhenValueOf_ThenResultIsPrimitiveDouble() {
|
||||||
assertEquals(1.23, Double.valueOf("1.23"), 0.000001);
|
assertEquals(1.23, Double.valueOf("1.23"), 0.000001);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = NullPointerException.class)
|
@Test(expected = NullPointerException.class)
|
||||||
public void givenNullString_WhenValueOf_ThenNullPointerExceptionIsThrown() {
|
public void givenNullString_WhenValueOf_ThenNullPointerExceptionIsThrown() {
|
||||||
Double.valueOf(null);
|
Double.valueOf(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = NumberFormatException.class)
|
@Test(expected = NumberFormatException.class)
|
||||||
public void givenInalidString_WhenValueOf_ThenNumberFormatExceptionIsThrown() {
|
public void givenInalidString_WhenValueOf_ThenNumberFormatExceptionIsThrown() {
|
||||||
Double.valueOf("&");
|
Double.valueOf("&");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenValidString_WhenDecimalFormat_ThenResultIsValidDouble() throws ParseException {
|
public void givenValidString_WhenDecimalFormat_ThenResultIsValidDouble() throws ParseException {
|
||||||
assertEquals(1.23, new DecimalFormat("#").parse("1.23").doubleValue(), 0.000001);
|
assertEquals(1.23, new DecimalFormat("#").parse("1.23").doubleValue(), 0.000001);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = NullPointerException.class)
|
@Test(expected = NullPointerException.class)
|
||||||
public void givenNullString_WhenDecimalFormat_ThenNullPointerExceptionIsThrown() throws ParseException {
|
public void givenNullString_WhenDecimalFormat_ThenNullPointerExceptionIsThrown() throws ParseException {
|
||||||
new DecimalFormat("#").parse(null);
|
new DecimalFormat("#").parse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ParseException.class)
|
@Test(expected = ParseException.class)
|
||||||
public void givenInvalidString_WhenDecimalFormat_ThenParseExceptionIsThrown() throws ParseException {
|
public void givenInvalidString_WhenDecimalFormat_ThenParseExceptionIsThrown() throws ParseException {
|
||||||
new DecimalFormat("#").parse("&");
|
new DecimalFormat("#").parse("&");
|
||||||
}
|
}
|
||||||
}
|
}
|
13
java-strings-3/src/test/resources/.gitignore
vendored
Normal file
13
java-strings-3/src/test/resources/.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
*.class
|
||||||
|
|
||||||
|
#folders#
|
||||||
|
/target
|
||||||
|
/neoDb*
|
||||||
|
/data
|
||||||
|
/src/main/webapp/WEB-INF/classes
|
||||||
|
*/META-INF/*
|
||||||
|
|
||||||
|
# Packaged files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
20
pom.xml
20
pom.xml
@ -469,6 +469,7 @@
|
|||||||
<!-- <module>java-streams-2</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
|
<!-- <module>java-streams-2</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
|
||||||
<module>java-strings</module>
|
<module>java-strings</module>
|
||||||
<module>java-strings-2</module>
|
<module>java-strings-2</module>
|
||||||
|
<module>java-strings-3</module>
|
||||||
<module>java-strings-ops</module>
|
<module>java-strings-ops</module>
|
||||||
<module>java-vavr-stream</module>
|
<module>java-vavr-stream</module>
|
||||||
<module>java-websocket</module>
|
<module>java-websocket</module>
|
||||||
@ -625,7 +626,6 @@
|
|||||||
<module>spring-akka</module>
|
<module>spring-akka</module>
|
||||||
<module>spring-all</module>
|
<module>spring-all</module>
|
||||||
<module>spring-amqp</module>
|
<module>spring-amqp</module>
|
||||||
<module>spring-amqp-simple</module>
|
|
||||||
<module>spring-aop</module>
|
<module>spring-aop</module>
|
||||||
<module>spring-apache-camel</module>
|
<module>spring-apache-camel</module>
|
||||||
<module>spring-batch</module>
|
<module>spring-batch</module>
|
||||||
@ -664,23 +664,23 @@
|
|||||||
<module>spring-boot-libraries</module>
|
<module>spring-boot-libraries</module>
|
||||||
|
|
||||||
|
|
||||||
<!-- <module>spring-cloud</module> --> <!-- BAEL-14304 -->
|
<module>spring-cloud</module>
|
||||||
<!-- <module>spring-cloud-bus</module> --> <!-- BAEL-14304 -->
|
<module>spring-cloud-bus</module>
|
||||||
<!-- <module>spring-cloud-cli</module> --> <!-- Not a maven project -->
|
<!-- <module>spring-cloud-cli</module> --> <!-- Not a maven project -->
|
||||||
<!-- <module>spring-cloud-data-flow</module> --> <!-- BAEL-14304 -->
|
<module>spring-cloud-data-flow</module>
|
||||||
|
|
||||||
<module>spring-core</module>
|
<module>spring-core</module>
|
||||||
<module>spring-core-2</module>
|
<module>spring-core-2</module>
|
||||||
<module>spring-cucumber</module>
|
<module>spring-cucumber</module>
|
||||||
|
|
||||||
<module>spring-data-rest</module>
|
<module>spring-data-rest</module>
|
||||||
<!-- <module>spring-data-rest-querydsl</module> --> <!-- BAEL-14304 -->
|
<module>spring-data-rest-querydsl</module>
|
||||||
<module>spring-dispatcher-servlet</module>
|
<module>spring-dispatcher-servlet</module>
|
||||||
<module>spring-drools</module>
|
<module>spring-drools</module>
|
||||||
<module>spring-di</module>
|
<module>spring-di</module>
|
||||||
|
|
||||||
<module>spring-ehcache</module>
|
<module>spring-ehcache</module>
|
||||||
<!-- <module>spring-ejb</module> BAEL-14304 -->
|
<module>spring-ejb</module>
|
||||||
<module>spring-exceptions</module>
|
<module>spring-exceptions</module>
|
||||||
|
|
||||||
<module>spring-freemarker</module>
|
<module>spring-freemarker</module>
|
||||||
@ -752,7 +752,7 @@
|
|||||||
<module>spring-security-stormpath</module>
|
<module>spring-security-stormpath</module>
|
||||||
<module>spring-security-thymeleaf</module>
|
<module>spring-security-thymeleaf</module>
|
||||||
<module>spring-security-x509</module>
|
<module>spring-security-x509</module>
|
||||||
<!-- <module>spring-session</module> BAEL-14304 -->
|
<module>spring-session</module>
|
||||||
<module>spring-sleuth</module>
|
<module>spring-sleuth</module>
|
||||||
<module>spring-soap</module>
|
<module>spring-soap</module>
|
||||||
<module>spring-social-login</module>
|
<module>spring-social-login</module>
|
||||||
@ -763,10 +763,10 @@
|
|||||||
|
|
||||||
<module>spring-thymeleaf</module>
|
<module>spring-thymeleaf</module>
|
||||||
|
|
||||||
<!-- <module>spring-vault</module> BAEL-14304 -->
|
<module>spring-vault</module>
|
||||||
<module>spring-vertx</module>
|
<module>spring-vertx</module>
|
||||||
|
|
||||||
<!-- <module>spring-webflux-amqp</module> BAEL-14304 --> <!-- long -->
|
<module>spring-webflux-amqp</module> <!-- long -->
|
||||||
|
|
||||||
<module>spring-zuul</module>
|
<module>spring-zuul</module>
|
||||||
|
|
||||||
@ -1165,6 +1165,7 @@
|
|||||||
<!-- <module>java-streams-2</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
|
<!-- <module>java-streams-2</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
|
||||||
<module>java-strings</module>
|
<module>java-strings</module>
|
||||||
<module>java-strings-2</module>
|
<module>java-strings-2</module>
|
||||||
|
<module>java-strings-3</module>
|
||||||
<module>java-strings-ops</module>
|
<module>java-strings-ops</module>
|
||||||
<module>java-vavr-stream</module>
|
<module>java-vavr-stream</module>
|
||||||
<module>java-websocket</module>
|
<module>java-websocket</module>
|
||||||
@ -1303,7 +1304,6 @@
|
|||||||
<module>spring-akka</module>
|
<module>spring-akka</module>
|
||||||
<module>spring-all</module>
|
<module>spring-all</module>
|
||||||
<module>spring-amqp</module>
|
<module>spring-amqp</module>
|
||||||
<module>spring-amqp-simple</module>
|
|
||||||
<module>spring-aop</module>
|
<module>spring-aop</module>
|
||||||
<module>spring-apache-camel</module>
|
<module>spring-apache-camel</module>
|
||||||
<module>spring-batch</module>
|
<module>spring-batch</module>
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
### Relevant Articles:
|
|
||||||
- [RabbitMQ Message Dispatching with Spring AMQP](http://www.baeldung.com/rabbitmq-spring-amqp)
|
|
@ -1,28 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<groupId>com.baeldung</groupId>
|
|
||||||
<artifactId>spring-amqp-simple</artifactId>
|
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
|
||||||
<name>spring-amqp-simple</name>
|
|
||||||
|
|
||||||
<parent>
|
|
||||||
<artifactId>parent-boot-2</artifactId>
|
|
||||||
<groupId>com.baeldung</groupId>
|
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
|
||||||
<relativePath>../parent-boot-2</relativePath>
|
|
||||||
</parent>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
</project>
|
|
@ -1,17 +0,0 @@
|
|||||||
package com.baeldung.springamqpsimple;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class MessageConsumer {
|
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(MessageConsumer.class);
|
|
||||||
|
|
||||||
@RabbitListener(queues = {SpringAmqpConfig.queueName})
|
|
||||||
public void receiveMessage(String message) {
|
|
||||||
logger.info("Received Message: " + message);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
package com.baeldung.springamqpsimple;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.stereotype.Controller;
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
||||||
|
|
||||||
@Controller
|
|
||||||
public class MessageController {
|
|
||||||
|
|
||||||
private final MessageProducer messageProducer;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public MessageController(MessageProducer messageProducer) {
|
|
||||||
this.messageProducer = messageProducer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value="/messages", method= RequestMethod.POST)
|
|
||||||
@ResponseStatus(value= HttpStatus.CREATED)
|
|
||||||
public void sendMessage(@RequestBody String message) {
|
|
||||||
messageProducer.sendMessage(message);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
package com.baeldung.springamqpsimple;
|
|
||||||
|
|
||||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class MessageProducer {
|
|
||||||
|
|
||||||
private final RabbitTemplate rabbitTemplate;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public MessageProducer(RabbitTemplate rabbitTemplate) {
|
|
||||||
this.rabbitTemplate = rabbitTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendMessage(String message) {
|
|
||||||
rabbitTemplate.convertAndSend(SpringAmqpConfig.queueName, message);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
package com.baeldung.springamqpsimple;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
public class SpringAmqpApplication {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws InterruptedException {
|
|
||||||
SpringApplication.run(SpringAmqpApplication.class, args);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,51 +0,0 @@
|
|||||||
package com.baeldung.springamqpsimple;
|
|
||||||
|
|
||||||
import org.springframework.amqp.core.Binding;
|
|
||||||
import org.springframework.amqp.core.BindingBuilder;
|
|
||||||
import org.springframework.amqp.core.DirectExchange;
|
|
||||||
import org.springframework.amqp.core.Exchange;
|
|
||||||
import org.springframework.amqp.core.Queue;
|
|
||||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
|
||||||
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
|
|
||||||
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.Profile;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@Profile("!test")
|
|
||||||
public class SpringAmqpConfig {
|
|
||||||
|
|
||||||
public final static String queueName = "com.baeldung.spring-amqp-simple.queue";
|
|
||||||
public final static String exchangeName = "com.baeldung.spring-amqp-simple.exchange";
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
Queue queue() {
|
|
||||||
return new Queue(queueName, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
Exchange exchange() {
|
|
||||||
return new DirectExchange(exchangeName);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
Binding binding(Queue queue, DirectExchange exchange) {
|
|
||||||
return BindingBuilder.bind(queue).to(exchange).with(queueName);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
SimpleMessageListenerContainer springAmqpContainer(ConnectionFactory connectionFactory,
|
|
||||||
MessageListenerAdapter listenerAdapter) {
|
|
||||||
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
|
|
||||||
container.setConnectionFactory(connectionFactory);
|
|
||||||
container.setQueueNames(queueName);
|
|
||||||
container.setMessageListener(listenerAdapter);
|
|
||||||
return container;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
MessageListenerAdapter listenerAdapter(MessageConsumer messageReceiver) {
|
|
||||||
return new MessageListenerAdapter(messageReceiver, "receiveMessage");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,70 +0,0 @@
|
|||||||
package com.baeldung.springamqpsimple.broadcast;
|
|
||||||
|
|
||||||
import org.springframework.amqp.core.BindingBuilder;
|
|
||||||
import org.springframework.amqp.core.Declarable;
|
|
||||||
import org.springframework.amqp.core.DirectExchange;
|
|
||||||
import org.springframework.amqp.core.FanoutExchange;
|
|
||||||
import org.springframework.amqp.core.Queue;
|
|
||||||
import org.springframework.amqp.core.TopicExchange;
|
|
||||||
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
|
|
||||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
|
||||||
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.Profile;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@Profile("!test")
|
|
||||||
public class BroadcastConfig {
|
|
||||||
|
|
||||||
public final static String fanoutQueue1Name = "com.baeldung.spring-amqp-simple.fanout.queue1";
|
|
||||||
public final static String fanoutQueue2Name = "com.baeldung.spring-amqp-simple.fanout.queue2";
|
|
||||||
public final static String fanoutExchangeName = "com.baeldung.spring-amqp-simple.fanout.exchange";
|
|
||||||
|
|
||||||
public final static String topicQueue1Name = "com.baeldung.spring-amqp-simple.topic.queue1";
|
|
||||||
public final static String topicQueue2Name = "com.baeldung.spring-amqp-simple.topic.queue2";
|
|
||||||
public final static String topicExchangeName = "com.baeldung.spring-amql-simple.topic.exchange";
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public List<Declarable> topicBindings() {
|
|
||||||
Queue topicQueue1 = new Queue(topicQueue1Name, false);
|
|
||||||
Queue topicQueue2 = new Queue(topicQueue2Name, false);
|
|
||||||
|
|
||||||
TopicExchange topicExchange = new TopicExchange(topicExchangeName);
|
|
||||||
|
|
||||||
return Arrays.asList(
|
|
||||||
topicQueue1,
|
|
||||||
topicQueue2,
|
|
||||||
topicExchange,
|
|
||||||
BindingBuilder.bind(topicQueue1).to(topicExchange).with("*.important.*"),
|
|
||||||
BindingBuilder.bind(topicQueue2).to(topicExchange).with("user.#")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public List<Declarable> fanoutBindings() {
|
|
||||||
Queue fanoutQueue1 = new Queue(fanoutQueue1Name, false);
|
|
||||||
Queue fanoutQueue2 = new Queue(fanoutQueue2Name, false);
|
|
||||||
|
|
||||||
FanoutExchange fanoutExchange = new FanoutExchange(fanoutExchangeName);
|
|
||||||
|
|
||||||
return Arrays.asList(
|
|
||||||
fanoutQueue1,
|
|
||||||
fanoutQueue2,
|
|
||||||
fanoutExchange,
|
|
||||||
BindingBuilder.bind(fanoutQueue1).to(fanoutExchange),
|
|
||||||
BindingBuilder.bind(fanoutQueue2).to(fanoutExchange)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public SimpleRabbitListenerContainerFactory broadcastContainer(ConnectionFactory connectionFactory, SimpleRabbitListenerContainerFactoryConfigurer configurer) {
|
|
||||||
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
|
|
||||||
configurer.configure(factory, connectionFactory);
|
|
||||||
return factory;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
package com.baeldung.springamqpsimple.broadcast;
|
|
||||||
|
|
||||||
import com.baeldung.springamqpsimple.MessageConsumer;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class BroadcastMessageConsumers {
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(MessageConsumer.class);
|
|
||||||
|
|
||||||
@RabbitListener(queues = {BroadcastConfig.fanoutQueue1Name})
|
|
||||||
public void receiveMessageFromFanout1(String message) {
|
|
||||||
logger.info("Received fanout 1 message: " + message);
|
|
||||||
}
|
|
||||||
|
|
||||||
@RabbitListener(queues = {BroadcastConfig.fanoutQueue2Name})
|
|
||||||
public void receiveMessageFromFanout2(String message) {
|
|
||||||
logger.info("Received fanout 2 message: " + message);
|
|
||||||
}
|
|
||||||
|
|
||||||
@RabbitListener(queues = {BroadcastConfig.topicQueue1Name})
|
|
||||||
public void receiveMessageFromTopic1(String message) {
|
|
||||||
logger.info("Received topic 1 message: " + message);
|
|
||||||
}
|
|
||||||
|
|
||||||
@RabbitListener(queues = {BroadcastConfig.topicQueue2Name})
|
|
||||||
public void receiveMessageFromTopic2(String message) {
|
|
||||||
logger.info("Received topic 2 message: " + message);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
package com.baeldung.springamqpsimple.broadcast;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.stereotype.Controller;
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
||||||
|
|
||||||
@Controller
|
|
||||||
public class BroadcastMessageController {
|
|
||||||
|
|
||||||
private final BroadcastMessageProducer messageProducer;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public BroadcastMessageController(BroadcastMessageProducer messageProducer) {
|
|
||||||
this.messageProducer = messageProducer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value="/broadcast", method= RequestMethod.POST)
|
|
||||||
@ResponseStatus(value= HttpStatus.CREATED)
|
|
||||||
public void sendMessage(@RequestBody String message) {
|
|
||||||
messageProducer.sendMessages(message);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
package com.baeldung.springamqpsimple.broadcast;
|
|
||||||
|
|
||||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class BroadcastMessageProducer {
|
|
||||||
|
|
||||||
private final RabbitTemplate rabbitTemplate;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public BroadcastMessageProducer(RabbitTemplate rabbitTemplate) {
|
|
||||||
this.rabbitTemplate = rabbitTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendMessages(String message) {
|
|
||||||
rabbitTemplate.convertAndSend(BroadcastConfig.fanoutExchangeName, "", message);
|
|
||||||
rabbitTemplate.convertAndSend(BroadcastConfig.topicExchangeName, "user.not-important.info", message);
|
|
||||||
rabbitTemplate.convertAndSend(BroadcastConfig.topicExchangeName, "user.important.error", message);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
spring:
|
|
||||||
rabbitmq:
|
|
||||||
username: guest
|
|
||||||
password: guest
|
|
||||||
host: 10.10.10.105
|
|
@ -1,13 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<configuration>
|
|
||||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
|
||||||
<encoder>
|
|
||||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
|
||||||
</pattern>
|
|
||||||
</encoder>
|
|
||||||
</appender>
|
|
||||||
|
|
||||||
<root level="INFO">
|
|
||||||
<appender-ref ref="STDOUT" />
|
|
||||||
</root>
|
|
||||||
</configuration>
|
|
@ -1,17 +0,0 @@
|
|||||||
package org.baeldung;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
import com.baeldung.springamqpsimple.SpringAmqpApplication;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = SpringAmqpApplication.class)
|
|
||||||
public class SpringContextManualTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
spring:
|
|
||||||
rabbitmq:
|
|
||||||
username: guest
|
|
||||||
password: guest
|
|
||||||
host: localhost
|
|
@ -1,3 +1,4 @@
|
|||||||
## Relevant articles:
|
## Relevant articles:
|
||||||
|
|
||||||
- [Messaging With Spring AMQP](http://www.baeldung.com/spring-amqp)
|
- [Messaging With Spring AMQP](https://www.baeldung.com/spring-amqp)
|
||||||
|
- [RabbitMQ Message Dispatching with Spring AMQP](https://www.baeldung.com/rabbitmq-spring-amqp)
|
@ -3,37 +3,26 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>spring-amqp</artifactId>
|
<artifactId>spring-amqp</artifactId>
|
||||||
<version>0.1-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
<name>spring-amqp</name>
|
<name>spring-amqp</name>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<description>Introduction to Spring-AMQP</description>
|
<description>Introduction to Spring-AMQP</description>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
|
<artifactId>parent-boot-2</artifactId>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<relativePath>../parent-boot-2</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.amqp</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-rabbit</artifactId>
|
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||||
<version>${spring-rabbit}</version>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>commons-logging</artifactId>
|
|
||||||
<groupId>commons-logging</groupId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
|
||||||
<finalName>spring-amqp</finalName>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<spring-rabbit>1.6.6.RELEASE</spring-rabbit>
|
<start-class>com.baeldung.springamqp.simple.HelloWorldMessageApp</start-class>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -0,0 +1,52 @@
|
|||||||
|
package com.baeldung.springamqp.broadcast;
|
||||||
|
|
||||||
|
import org.springframework.amqp.core.*;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class BroadcastConfig {
|
||||||
|
|
||||||
|
private static final boolean NON_DURABLE = false;
|
||||||
|
|
||||||
|
public final static String FANOUT_QUEUE_1_NAME = "com.baeldung.spring-amqp-simple.fanout.queue1";
|
||||||
|
public final static String FANOUT_QUEUE_2_NAME = "com.baeldung.spring-amqp-simple.fanout.queue2";
|
||||||
|
public final static String FANOUT_EXCHANGE_NAME = "com.baeldung.spring-amqp-simple.fanout.exchange";
|
||||||
|
|
||||||
|
public final static String TOPIC_QUEUE_1_NAME = "com.baeldung.spring-amqp-simple.topic.queue1";
|
||||||
|
public final static String TOPIC_QUEUE_2_NAME = "com.baeldung.spring-amqp-simple.topic.queue2";
|
||||||
|
public final static String TOPIC_EXCHANGE_NAME = "com.baeldung.spring-amqp-simple.topic.exchange";
|
||||||
|
public static final String BINDING_PATTERN_IMPORTANT = "*.important.*";
|
||||||
|
public static final String BINDING_PATTERN_ERROR = "#.error";
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Declarables topicBindings() {
|
||||||
|
Queue topicQueue1 = new Queue(TOPIC_QUEUE_1_NAME, NON_DURABLE);
|
||||||
|
Queue topicQueue2 = new Queue(TOPIC_QUEUE_2_NAME, NON_DURABLE);
|
||||||
|
|
||||||
|
TopicExchange topicExchange = new TopicExchange(TOPIC_EXCHANGE_NAME, NON_DURABLE, false);
|
||||||
|
|
||||||
|
return new Declarables(topicQueue1, topicQueue2, topicExchange, BindingBuilder
|
||||||
|
.bind(topicQueue1)
|
||||||
|
.to(topicExchange)
|
||||||
|
.with(BINDING_PATTERN_IMPORTANT), BindingBuilder
|
||||||
|
.bind(topicQueue2)
|
||||||
|
.to(topicExchange)
|
||||||
|
.with(BINDING_PATTERN_ERROR));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Declarables fanoutBindings() {
|
||||||
|
Queue fanoutQueue1 = new Queue(FANOUT_QUEUE_1_NAME, NON_DURABLE);
|
||||||
|
Queue fanoutQueue2 = new Queue(FANOUT_QUEUE_2_NAME, NON_DURABLE);
|
||||||
|
|
||||||
|
FanoutExchange fanoutExchange = new FanoutExchange(FANOUT_EXCHANGE_NAME, NON_DURABLE, false);
|
||||||
|
|
||||||
|
return new Declarables(fanoutQueue1, fanoutQueue2, fanoutExchange, BindingBuilder
|
||||||
|
.bind(fanoutQueue1)
|
||||||
|
.to(fanoutExchange), BindingBuilder
|
||||||
|
.bind(fanoutQueue2)
|
||||||
|
.to(fanoutExchange));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.baeldung.springamqp.broadcast;
|
||||||
|
|
||||||
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.boot.ApplicationRunner;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
import static com.baeldung.springamqp.broadcast.BroadcastConfig.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple test application to send messages to rabbitMQ.
|
||||||
|
*
|
||||||
|
*<p>To run this particular application with mvn you use the following command:</p>
|
||||||
|
* {@code
|
||||||
|
* mvn spring-boot:run -Dstart-class=com.baeldung.springamqp.broadcast.BroadcastMessageApp
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
public class BroadcastMessageApp {
|
||||||
|
|
||||||
|
private static String ROUTING_KEY_USER_IMPORTANT_WARN = "user.important.warn";
|
||||||
|
private static String ROUTING_KEY_USER_IMPORTANT_ERROR = "user.important.error";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(BroadcastMessageApp.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ApplicationRunner runner(RabbitTemplate rabbitTemplate) {
|
||||||
|
String message = " payload is broadcast";
|
||||||
|
return args -> {
|
||||||
|
rabbitTemplate.convertAndSend(BroadcastConfig.FANOUT_EXCHANGE_NAME, "", "fanout" + message);
|
||||||
|
rabbitTemplate.convertAndSend(BroadcastConfig.TOPIC_EXCHANGE_NAME, ROUTING_KEY_USER_IMPORTANT_WARN, "topic important warn" + message);
|
||||||
|
rabbitTemplate.convertAndSend(BroadcastConfig.TOPIC_EXCHANGE_NAME, ROUTING_KEY_USER_IMPORTANT_ERROR, "topic important error" + message);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@RabbitListener(queues = { FANOUT_QUEUE_1_NAME })
|
||||||
|
public void receiveMessageFromFanout1(String message) {
|
||||||
|
System.out.println("Received fanout 1 message: " + message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RabbitListener(queues = { FANOUT_QUEUE_2_NAME })
|
||||||
|
public void receiveMessageFromFanout2(String message) {
|
||||||
|
System.out.println("Received fanout 2 message: " + message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RabbitListener(queues = { TOPIC_QUEUE_1_NAME })
|
||||||
|
public void receiveMessageFromTopic1(String message) {
|
||||||
|
System.out.println("Received topic 1 (" + BINDING_PATTERN_IMPORTANT + ") message: " + message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RabbitListener(queues = { TOPIC_QUEUE_2_NAME })
|
||||||
|
public void receiveMessageFromTopic2(String message) {
|
||||||
|
System.out.println("Received topic 2 (" + BINDING_PATTERN_ERROR + ") message: " + message);
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +0,0 @@
|
|||||||
package com.baeldung.springamqp.consumer;
|
|
||||||
|
|
||||||
public class Consumer {
|
|
||||||
public void listen(String foo) {
|
|
||||||
System.out.println(foo);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
package com.baeldung.springamqp.producer;
|
|
||||||
|
|
||||||
import org.springframework.amqp.core.AmqpTemplate;
|
|
||||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
||||||
import org.springframework.context.support.AbstractApplicationContext;
|
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
|
||||||
|
|
||||||
public class Producer {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws InterruptedException {
|
|
||||||
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
|
|
||||||
AmqpTemplate template = ctx.getBean(RabbitTemplate.class);
|
|
||||||
template.convertAndSend("Hello, world!");
|
|
||||||
Thread.sleep(1000);
|
|
||||||
ctx.destroy();
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.springamqp.simple;
|
||||||
|
|
||||||
|
import org.springframework.amqp.core.Queue;
|
||||||
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.boot.ApplicationRunner;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class HelloWorldMessageApp {
|
||||||
|
|
||||||
|
private static final boolean NON_DURABLE = false;
|
||||||
|
private static final String MY_QUEUE_NAME = "myQueue";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(HelloWorldMessageApp.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ApplicationRunner runner(RabbitTemplate template) {
|
||||||
|
return args -> {
|
||||||
|
template.convertAndSend("myQueue", "Hello, world!");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Queue myQueue() {
|
||||||
|
return new Queue(MY_QUEUE_NAME, NON_DURABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RabbitListener(queues = MY_QUEUE_NAME)
|
||||||
|
public void listen(String in) {
|
||||||
|
System.out.println("Message read from myQueue : " + in);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,35 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
|
||||||
xmlns:p="http://www.springframework.org/schema/p" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
|
|
||||||
xmlns:c="http://www.springframework.org/schema/c" xmlns:util="http://www.springframework.org/schema/util"
|
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
|
||||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
|
|
||||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
|
|
||||||
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd">
|
|
||||||
|
|
||||||
<rabbit:connection-factory id="connectionFactory" host="localhost" username="guest" password="guest" />
|
|
||||||
|
|
||||||
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
|
|
||||||
exchange="myExchange" routing-key="foo.bar" />
|
|
||||||
|
|
||||||
<rabbit:admin connection-factory="connectionFactory" />
|
|
||||||
|
|
||||||
<rabbit:queue name="myQueue" />
|
|
||||||
|
|
||||||
<rabbit:topic-exchange name="myExchange">
|
|
||||||
<rabbit:bindings>
|
|
||||||
<rabbit:binding queue="myQueue" pattern="foo.*" />
|
|
||||||
</rabbit:bindings>
|
|
||||||
</rabbit:topic-exchange>
|
|
||||||
|
|
||||||
|
|
||||||
<rabbit:listener-container
|
|
||||||
connection-factory="connectionFactory">
|
|
||||||
<rabbit:listener ref="consumer" method="listen"
|
|
||||||
queue-names="myQueue" />
|
|
||||||
</rabbit:listener-container>
|
|
||||||
|
|
||||||
<bean id="consumer" class="com.baeldung.springamqp.consumer.Consumer" />
|
|
||||||
|
|
||||||
</beans>
|
|
@ -1,13 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<configuration>
|
|
||||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
|
||||||
<encoder>
|
|
||||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
|
||||||
</pattern>
|
|
||||||
</encoder>
|
|
||||||
</appender>
|
|
||||||
|
|
||||||
<root level="INFO">
|
|
||||||
<appender-ref ref="STDOUT" />
|
|
||||||
</root>
|
|
||||||
</configuration>
|
|
@ -9,7 +9,7 @@ import com.baeldung.SpringCloudConfigClientApplication;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = SpringCloudConfigClientApplication.class)
|
@SpringBootTest(classes = SpringCloudConfigClientApplication.class)
|
||||||
public class SpringContextTest {
|
public class SpringContextLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void contextLoads() {
|
public void contextLoads() {
|
@ -13,7 +13,7 @@
|
|||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>spring-cloud-data-flow-stream</module>
|
<module>spring-cloud-data-flow-stream-processing</module>
|
||||||
<module>spring-cloud-data-flow-etl</module>
|
<module>spring-cloud-data-flow-etl</module>
|
||||||
<module>batch-job</module>
|
<module>batch-job</module>
|
||||||
<module>apache-spark-job</module>
|
<module>apache-spark-job</module>
|
||||||
|
@ -8,7 +8,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = DataFlowServerApplication.class)
|
@SpringBootTest(classes = DataFlowServerApplication.class)
|
||||||
public class SpringContextTest {
|
public class SpringContextLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
@ -8,7 +8,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = DataFlowShellApplication.class)
|
@SpringBootTest(classes = DataFlowShellApplication.class)
|
||||||
public class SpringContextTest {
|
public class SpringContextLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void contextLoads() {
|
public void contextLoads() {
|
@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class SpringContextTest {
|
public class SpringContextLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = VaultSampleApplication.class)
|
@SpringBootTest(classes = VaultSampleApplication.class)
|
||||||
public class SpringContextTest {
|
public class SpringContextLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
@ -9,7 +9,7 @@ import com.baeldung.Application;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = Application.class)
|
@SpringBootTest(classes = Application.class)
|
||||||
public class SpringContextTest {
|
public class SpringContextLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
@ -9,7 +9,7 @@ import com.baeldung.springejbclient.SpringEjbClientApplication;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = SpringEjbClientApplication.class)
|
@SpringBootTest(classes = SpringEjbClientApplication.class)
|
||||||
public class SpringContextTest {
|
public class SpringContextLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
@ -8,7 +8,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = SpringSessionMongoDBApplication.class)
|
@SpringBootTest(classes = SpringSessionMongoDBApplication.class)
|
||||||
public class SpringContextTest {
|
public class SpringContextLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
@ -9,7 +9,7 @@ import com.baeldung.spring.session.SessionWebApplication;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = SessionWebApplication.class)
|
@SpringBootTest(classes = SessionWebApplication.class)
|
||||||
public class SpringContextTest {
|
public class SpringContextLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
@ -8,7 +8,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = SpringVaultApplication.class)
|
@SpringBootTest(classes = SpringVaultApplication.class)
|
||||||
public class SpringContextTest {
|
public class SpringContextLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
@ -8,7 +8,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = SpringWebfluxAmqpApplication.class)
|
@SpringBootTest(classes = SpringWebfluxAmqpApplication.class)
|
||||||
public class SpringContextTest {
|
public class SpringContextLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
Loading…
x
Reference in New Issue
Block a user