Merge branch 'master' into BAEL-16646

# Conflicts:
#	core-java-modules/core-java-datetime/README.md
This commit is contained in:
Alessio Stalla
2019-10-19 15:57:18 +02:00
82 changed files with 886 additions and 208 deletions

View File

@@ -1,3 +1,7 @@
## Relevant articles:
## Akka HTTP
This module contains articles about Akka HTTP.
### Relevant articles:
- [Introduction to Akka HTTP](https://www.baeldung.com/akka-http)

View File

@@ -1,3 +1,7 @@
## Akka Streams
This module contains articles about Akka Streams.
### Relevant articles
- [Guide to Akka Streams](https://www.baeldung.com/akka-streams)

View File

@@ -1,4 +1,8 @@
## Relevant articles:
## Genetic Algorithms
This module contains articles about genetic algorithms.
### Relevant articles:
- [Introduction to Jenetics Library](https://www.baeldung.com/jenetics)
- [Ant Colony Optimization](https://www.baeldung.com/java-ant-colony-optimization)

View File

@@ -1,4 +1,9 @@
## Relevant articles:
## Algorithms - Miscellaneous
This module contains articles about algorithms. Some classes of algorithms, e.g., [sorting](/../algorithms-sorting) and
[genetic algorithms](/../algorithms-genetic), have their own dedicated modules.
### Relevant articles:
- [Validating Input With Finite Automata in Java](https://www.baeldung.com/java-finite-automata)
- [Example of Hill Climbing Algorithm](https://www.baeldung.com/java-hill-climbing-algorithm)
@@ -6,4 +11,5 @@
- [Binary Search Algorithm in Java](https://www.baeldung.com/java-binary-search)
- [Introduction to Minimax Algorithm](https://www.baeldung.com/java-minimax-algorithm)
- [How to Calculate Levenshtein Distance in Java?](https://www.baeldung.com/java-levenshtein-distance)
- [How to Find the Kth Largest Element in Java](https://www.baeldung.com/java-kth-largest-element)
- [How to Find the Kth Largest Element in Java](https://www.baeldung.com/java-kth-largest-element)
- More articles: [[next -->]](/../algorithms-miscellaneous-2)

View File

@@ -1,4 +1,9 @@
## Relevant articles:
## Algorithms - Miscellaneous
This module contains articles about algorithms. Some classes of algorithms, e.g., [sorting](/../algorithms-sorting) and
[genetic algorithms](/../algorithms-genetic), have their own dedicated modules.
### Relevant articles:
- [Dijkstra Shortest Path Algorithm in Java](https://www.baeldung.com/java-dijkstra)
- [Introduction to Cobertura](https://www.baeldung.com/cobertura)
@@ -8,3 +13,4 @@
- [Create a Sudoku Solver in Java](https://www.baeldung.com/java-sudoku)
- [Displaying Money Amounts in Words](https://www.baeldung.com/java-money-into-words)
- [A Collaborative Filtering Recommendation System in Java](https://www.baeldung.com/java-collaborative-filtering-recommendations)
- More articles: [[<-- prev]](/../algorithms-miscellaneous-1) [[next -->]](/../algorithms-miscellaneous-3)

View File

@@ -1,4 +1,9 @@
## Relevant articles:
## Algorithms - Miscellaneous
This module contains articles about algorithms. Some classes of algorithms, e.g., [sorting](/../algorithms-sorting) and
[genetic algorithms](/../algorithms-genetic), have their own dedicated modules.
### Relevant articles:
- [Multi-Swarm Optimization Algorithm in Java](https://www.baeldung.com/java-multi-swarm-algorithm)
- [String Search Algorithms for Large Texts](https://www.baeldung.com/java-full-text-search-algorithms)
@@ -6,4 +11,5 @@
- [Find the Middle Element of a Linked List](https://www.baeldung.com/java-linked-list-middle-element)
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
- [Find the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
- More articles: [[<-- prev]](/../algorithms-miscellaneous-3) [[next -->]](/../algorithms-miscellaneous-5)

View File

@@ -1,5 +1,11 @@
## Relevant articles:
## Algorithms - Miscellaneous
This module contains articles about algorithms. Some classes of algorithms, e.g., [sorting](/../algorithms-sorting) and
[genetic algorithms](/../algorithms-genetic), have their own dedicated modules.
### Relevant articles:
- [Converting Between Byte Arrays and Hexadecimal Strings in Java](https://www.baeldung.com/java-byte-arrays-hex-strings)
- [Reversing a Binary Tree in Java](https://www.baeldung.com/java-reversing-a-binary-tree)
- [Find If Two Numbers Are Relatively Prime in Java](https://www.baeldung.com/java-two-relatively-prime-numbers)
- [Find If Two Numbers Are Relatively Prime in Java](https://www.baeldung.com/java-two-relatively-prime-numbers)
- More articles: [[<-- prev]](/../algorithms-miscellaneous-4)

View File

@@ -16,3 +16,4 @@ This module contains articles about sorting algorithms.
- [Selection Sort in Java](https://www.baeldung.com/java-selection-sort)
- [Sorting Strings by Contained Numbers in Java](https://www.baeldung.com/java-sort-strings-contained-numbers)
- [Radix Sort in Java](https://www.baeldung.com/java-radix-sort)
- [Sorting a String Alphabetically in Java](https://www.baeldung.com/java-sort-string-alphabetically)

View File

@@ -28,6 +28,12 @@
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter-api.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
@@ -52,6 +58,7 @@
<commons-math3.version>3.6.1</commons-math3.version>
<org.assertj.core.version>3.9.0</org.assertj.core.version>
<commons-codec.version>1.11</commons-codec.version>
<junit-jupiter-api.version>5.3.1</junit-jupiter-api.version>
</properties>
</project>

View File

@@ -1,4 +1,4 @@
package com.baeldung.string.sorting;
package com.baeldung.algorithms.stringsort;
import java.util.Arrays;

View File

@@ -1,12 +1,10 @@
package com.baeldung.string.sorting;
package com.baeldung.algorithms.stringsort;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.jupiter.api.Test;
import com.baeldung.string.sorting.AnagramValidator;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class AnagramValidatorUnitTest {

View File

@@ -1,9 +1,9 @@
package com.baeldung.string.sorting;
import java.util.Arrays;
package com.baeldung.algorithms.stringsort;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
class SortStringUnitTest {

View File

@@ -3,7 +3,6 @@
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.consumermodule</groupId>
<artifactId>consumermodule</artifactId>
<version>1.0</version>

View File

@@ -3,16 +3,16 @@
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.servicemodule</groupId>
<artifactId>servicemodule</artifactId>
<version>1.0</version>
<parent>
<artifactId>>com.baeldung.decoupling-pattern2</artifactId>
<groupId>decoupling-pattern2</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.baeldung.servicemodule</groupId>
<artifactId>servicemodule</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>

View File

@@ -28,12 +28,6 @@
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<!-- testing -->
<assertj-core.version>3.10.0</assertj-core.version>
<equalsverifier.version>3.0.3</equalsverifier.version>
</properties>
<build>
<finalName>core-java-lang-oop-2</finalName>
@@ -45,4 +39,10 @@
</resources>
</build>
<properties>
<!-- testing -->
<assertj-core.version>3.10.0</assertj-core.version>
<equalsverifier.version>3.0.3</equalsverifier.version>
</properties>
</project>

View File

@@ -54,7 +54,7 @@
</resources>
<plugins>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>

View File

@@ -0,0 +1,12 @@
## Java String APIs
This module contains articles about string APIs.
### Relevant Articles:
- [Java 8 StringJoiner](https://www.baeldung.com/java-string-joiner)
- [Quick Guide to the Java StringTokenizer](https://www.baeldung.com/java-stringtokenizer)
- [Guide to java.util.Formatter](https://www.baeldung.com/java-string-formatter)
- [Guide to StreamTokenizer](https://www.baeldung.com/java-streamtokenizer)
- [CharSequence vs. String in Java](https://www.baeldung.com/java-char-sequence-string)
- [StringBuilder and StringBuffer in Java](https://www.baeldung.com/java-string-builder-string-buffer)
- [Generate a Secure Random Password in Java](https://www.baeldung.com/java-generate-secure-password)

View File

@@ -0,0 +1,55 @@
<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>core-java-string-apis</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>core-java-string-apis</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-generator.version}</version>
</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>
</dependencies>
<build>
<finalName>core-java-string-apis</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<passay.version>1.3.1</passay.version>
<commons-text.version>1.4</commons-text.version>
</properties>
</project>

View File

@@ -1,4 +1,11 @@
package com.baeldung.string.password;
package com.baeldung.password;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.text.RandomStringGenerator;
import org.passay.CharacterRule;
import org.passay.EnglishCharacterData;
import org.passay.CharacterData;
import org.passay.PasswordGenerator;
import java.security.SecureRandom;
import java.util.Collections;
@@ -8,13 +15,6 @@ import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.text.RandomStringGenerator;
import org.passay.CharacterData;
import org.passay.CharacterRule;
import org.passay.EnglishCharacterData;
import org.passay.PasswordGenerator;
public class RandomPasswordGenerator {
/**

View File

@@ -1,4 +1,4 @@
package com.baeldung.string.streamtokenizer;
package com.baeldung.streamtokenizer;
import java.io.*;
import java.util.ArrayList;

View File

@@ -1,4 +1,4 @@
package com.baeldung.string;
package com.baeldung.stringbuilderstringbuffer;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
@@ -8,12 +8,12 @@ import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
public class StringBufferStringBuilder {
public class StringBuilderStringBuffer {
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(StringBufferStringBuilder.class.getSimpleName())
.include(StringBuilderStringBuffer.class.getSimpleName())
.build();
new Runner(opt).run();

View File

@@ -1,12 +1,12 @@
package com.baeldung.string.formatter;
package com.baeldung.formatter;
import org.junit.Test;
import java.util.Calendar;
import java.util.Formatter;
import java.util.GregorianCalendar;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import static org.junit.Assert.*;
public class StringFormatterExampleUnitTest {

View File

@@ -1,9 +1,9 @@
package com.baeldung.string.password;
import static org.junit.Assert.assertTrue;
package com.baeldung.password;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
/**
* Examples of passwords conforming to various specifications, using different libraries.
*

View File

@@ -1,6 +1,6 @@
package com.baeldung.stringjoiner;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
@@ -8,7 +8,7 @@ import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class StringJoinerUnitTest {
private final String DELIMITER_COMMA = ",";

View File

@@ -0,0 +1,3 @@
1|IND|India
2|MY|Malaysia
3|AU|Australia
1 1 IND India
2 2 MY Malaysia
3 3 AU Australia

5
gradle-5/README.md Normal file
View File

@@ -0,0 +1,5 @@
### Relevant Articles:
- [Run a Java main Method Using Gradle](https://www.baeldung.com/gradle-run-java-main)

View File

@@ -26,8 +26,6 @@
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@@ -7,12 +7,14 @@
<version>0.1.0-SNAPSHOT</version>
<name>java-blockchain</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<build>
<finalName>java-blockchain</finalName>
<resources>
@@ -33,8 +35,10 @@
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>

View File

@@ -14,3 +14,5 @@ This module contains articles about math in Java.
- [Convert Latitude and Longitude to a 2D Point in Java](https://www.baeldung.com/java-convert-latitude-longitude)
- [Debugging with Eclipse](https://www.baeldung.com/eclipse-debugging)
- [Matrix Multiplication in Java](https://www.baeldung.com/java-matrix-multiplication)
- [Calculating Logarithms in Java](https://www.baeldung.com/java-logarithms)
- [Finding Greatest Common Divisor in Java](https://www.baeldung.com/java-greatest-common-divisor)

View File

@@ -13,4 +13,6 @@ This module contains articles about numbers in Java.
- [Binary Numbers in Java](https://www.baeldung.com/java-binary-numbers)
- [Finding the Least Common Multiple in Java](https://www.baeldung.com/java-least-common-multiple)
- [Binary Numbers in Java](https://www.baeldung.com/java-binary-numbers)
- [Generating Random Numbers in a Range in Java](https://www.baeldung.com/java-generating-random-numbers)
- [Listing Numbers Within a Range in Java](https://www.baeldung.com/java-listing-numbers-within-a-range)
- More articles: [[<-- prev]](/../java-numbers)

View File

@@ -9,12 +9,10 @@ This module contains articles about strings in Java.
- [Removing Stopwords from a String in Java](https://www.baeldung.com/java-string-remove-stopwords)
- [Java Generate Random String](https://www.baeldung.com/java-random-string)
- [Java Base64 Encoding and Decoding](https://www.baeldung.com/java-base64-encode-and-decode)
- [Generate a Secure Random Password in Java](https://www.baeldung.com/java-generate-secure-password)
- [Removing Repeated Characters from a String](https://www.baeldung.com/java-remove-repeated-char)
- [Join Array of Primitives with Separator in Java](https://www.baeldung.com/java-join-primitive-array)
- [Pad a String with Zeros or Spaces in Java](https://www.baeldung.com/java-pad-string)
- [Remove Emojis from a Java String](https://www.baeldung.com/java-string-remove-emojis)
- [Guide to java.util.Formatter](https://www.baeldung.com/java-string-formatter)
- [Remove Leading and Trailing Characters from a String](https://www.baeldung.com/java-remove-trailing-characters)
- [Concatenating Strings In Java](https://www.baeldung.com/java-strings-concatenation)
- [Java String Interview Questions and Answers](https://www.baeldung.com/java-string-interview-questions)
@@ -26,5 +24,4 @@ This module contains articles about strings in Java.
- [Checking If a String Is a Repeated Substring](https://www.baeldung.com/java-repeated-substring)
- [How to Reverse a String in Java](https://www.baeldung.com/java-reverse-string)
- [String toLowerCase and toUpperCase Methods in Java](https://www.baeldung.com/java-string-convert-case)
- [Guide to StreamTokenizer](https://www.baeldung.com/java-streamtokenizer)
- More articles: [[<-- prev>]](/java-strings) [[next -->]](/java-strings-3)

View File

@@ -14,5 +14,3 @@ This module contains articles about operations on strings in Java.
- [Split a String in Java](https://www.baeldung.com/java-split-string)
- [Common String Operations in Java](https://www.baeldung.com/java-string-operations)
- [Java toString() Method](https://www.baeldung.com/java-tostring)
- [CharSequence vs. String in Java](https://www.baeldung.com/java-char-sequence-string)
- [StringBuilder and StringBuffer in Java](https://www.baeldung.com/java-string-builder-string-buffer)

View File

@@ -4,12 +4,9 @@ This module contains articles about strings in Java.
### Relevant Articles:
- [String Operations with Java Streams](https://www.baeldung.com/java-stream-operations-on-strings)
- [Java 8 StringJoiner](https://www.baeldung.com/java-string-joiner)
- [Quick Guide to the Java StringTokenizer](https://www.baeldung.com/java-stringtokenizer)
- [Use char[] Array Over a String for Manipulating Passwords in Java?](https://www.baeldung.com/java-storing-passwords)
- [Compact Strings in Java 9](https://www.baeldung.com/java-9-compact-string)
- [Java Check a String for Lowercase/Uppercase Letter, Special Character and Digit](https://www.baeldung.com/java-lowercase-uppercase-special-character-digit-regex)
- [Sorting a String Alphabetically in Java](https://www.baeldung.com/java-sort-string-alphabetically)
- [String Not Empty Test Assertions in Java](https://www.baeldung.com/java-assert-string-not-empty)
- [String Performance Hints](https://www.baeldung.com/java-string-performance)
- [Using indexOf to Find All Occurrences of a Word in a String](https://www.baeldung.com/java-indexOf-find-string-occurrences)

View File

@@ -4,3 +4,5 @@ This module contains articles about JAXB.
### Relevant Articles:
- [Guide to JAXB](https://www.baeldung.com/jaxb)
- [Unmarshalling Dates Using JAXB](https://www.baeldung.com/jaxb-unmarshalling-dates)

View File

@@ -9,3 +9,5 @@ This module contains articles about Jersey.
- [Set a Response Body in JAX-RS](https://www.baeldung.com/jax-rs-response)
- [Exploring the Jersey Test Framework](https://www.baeldung.com/jersey-test)
- [Explore Jersey Request Parameters](https://www.baeldung.com/jersey-request-parameters)
- [Add a Header to a Jersey SSE Client Request](https://www.baeldung.com/jersey-sse-client-request-headers)

View File

@@ -1,3 +1,5 @@
## Relevant articles:
- [Creating New APIs and Views in JHipster](https://www.baeldung.com/jhipster-new-apis-and-views)
- [JHipster Authentication with an External Service](https://www.baeldung.com/jhipster-authentication-external-service)

View File

@@ -6,4 +6,6 @@ This module contains articles about Kotlin Libraries.
- [Jackson Support for Kotlin](https://www.baeldung.com/jackson-kotlin)
- [Introduction to RxKotlin](https://www.baeldung.com/rxkotlin)
- [MockK: A Mocking Library for Kotlin](https://www.baeldung.com/kotlin-mockk)
- [Kotlin Immutable Collections](https://www.baeldung.com/kotlin-immutable-collections)
- More articles: [[<-- prev]](/kotlin-libraries)

View File

@@ -28,9 +28,30 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-collections-immutable -->
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-collections-immutable</artifactId>
<version>${kotlinx-collections-immutable.version}</version>
</dependency>
<!-- MockK dependencies-->
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk</artifactId>
<version>${mockk.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<guava.version>27.1-jre</guava.version>
<mockk.version>1.9.3</mockk.version>
<kotlinx-collections-immutable.version>0.1</kotlinx-collections-immutable.version>
</properties>
</project>

View File

@@ -14,6 +14,4 @@ This module contains articles about Kotlin Libraries.
- [Introduction to Arrow in Kotlin](https://www.baeldung.com/kotlin-arrow)
- [Kotlin with Ktor](https://www.baeldung.com/kotlin-ktor)
- [REST API With Kotlin and Kovert](https://www.baeldung.com/kotlin-kovert)
- [MockK: A Mocking Library for Kotlin](https://www.baeldung.com/kotlin-mockk)
- [Kotlin Immutable Collections](https://www.baeldung.com/kotlin-immutable-collections)
- More articles: [[next -->]](/kotlin-libraries-2)

View File

@@ -129,25 +129,7 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-collections-immutable -->
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-collections-immutable</artifactId>
<version>${kotlinx-collections-immutable.version}</version>
</dependency>
<!-- MockK dependencies-->
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk</artifactId>
<version>${mockk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
@@ -185,13 +167,10 @@
<junit.vintage.version>5.2.0</junit.vintage.version>
<assertj.version>3.10.0</assertj.version>
<exposed.version>0.10.4</exposed.version>
<mockk.version>1.9.3</mockk.version>
<guava.version>27.1-jre</guava.version>
<kovenant.version>3.3.0</kovenant.version>
<byte-buddy.version>1.8.13</byte-buddy.version>
<objenesis.version>2.6</objenesis.version>
<rxkotlin.version>2.3.0</rxkotlin.version>
<kotlinx-collections-immutable.version>0.1</kotlinx-collections-immutable.version>
<arrow-core.version>0.7.3</arrow-core.version>
</properties>

View File

@@ -1,3 +1,5 @@
### Relevant Articles
- [Guide to FastUtil](https://www.baeldung.com/fastutil)
- [Primitive Collections in Eclipse Collections](https://www.baeldung.com/java-eclipse-primitive-collections)

View File

@@ -4,3 +4,5 @@ This module contains articles about MapStruct.
###Relevant Articles:
- [Quick Guide to MapStruct](https://www.baeldung.com/mapstruct)
- [Custom Mapper with MapStruct](https://www.baeldung.com/mapstruct-custom-mapper)

View File

@@ -7,3 +7,5 @@ This module contains articles about metrics.
- [Intro to Dropwizard Metrics](https://www.baeldung.com/dropwizard-metrics)
- [Introduction to Netflix Servo](https://www.baeldung.com/netflix-servo)
- [Quick Guide to Micrometer](https://www.baeldung.com/micrometer)
- [@Timed Annotation Using Metrics and AspectJ](https://www.baeldung.com/timed-metrics-aspectj)

View File

@@ -0,0 +1,103 @@
package com.baeldung.jpa.text;
import javax.persistence.*;
@Entity
public class Exam {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Lob
private String description;
@Column(columnDefinition = "text")
private String text;
@Column(length = 20)
private String shortText;
public Exam() {
}
public Exam(String description, String text, String shortText) {
this.description = description;
this.text = text;
this.shortText = shortText;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getShortText() {
return shortText;
}
public void setShortText(String shortText) {
this.shortText = shortText;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((shortText == null) ? 0 : shortText.hashCode());
result = prime * result + ((text == null) ? 0 : text.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Exam other = (Exam) obj;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (shortText == null) {
if (other.shortText != null)
return false;
} else if (!shortText.equals(other.shortText))
return false;
if (text == null) {
if (other.text != null)
return false;
} else if (!text.equals(other.text))
return false;
return true;
}
}

View File

@@ -0,0 +1,39 @@
package com.baeldung.jpa.text;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class ExamRepository {
private EntityManagerFactory emf = null;
public ExamRepository() {
emf = Persistence.createEntityManagerFactory("jpa-h2-text");
}
public Exam find(Long id) {
EntityManager entityManager = emf.createEntityManager();
Exam exam = entityManager.find(Exam.class, id);
entityManager.close();
return exam;
}
public Exam save(Exam exam) {
EntityManager entityManager = emf.createEntityManager();
entityManager.getTransaction()
.begin();
exam = entityManager.merge(exam);
entityManager.getTransaction()
.commit();
entityManager.close();
return exam;
}
public void clean() {
emf.close();
}
}

View File

@@ -1,122 +1,138 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
version="2.2">
version="2.2">
<persistence-unit name="jpa-h2-queryparams"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.queryparams.Employee</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user"
value="sa" />
<property name="javax.persistence.jdbc.password"
value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto"
value="create-drop" />
<property name="show_sql" value="true" />
<property
name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
<property
name="javax.persistence.sql-load-script-source"
value="queryparams.sql" />
</properties>
</persistence-unit>
<persistence-unit name="jpa-h2-queryparams"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.queryparams.Employee</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
<property name="javax.persistence.sql-load-script-source"
value="queryparams.sql" />
</properties>
</persistence-unit>
<persistence-unit name="entity-default-values">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.defaultvalues.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
</properties>
</persistence-unit>
<persistence-unit name="jpa-query-types">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.querytypes.UserEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
<property name="javax.persistence.sql-load-script-source"
value="users.sql" />
</properties>
</persistence-unit>
<persistence-unit name="jpa-h2-text">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.text.Exam</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
</properties>
</persistence-unit>
<persistence-unit name="jpa-projections">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.projections.Product</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
<property name="javax.persistence.sql-load-script-source"
value="products_jpa.sql" />
</properties>
</persistence-unit>
<persistence-unit name="entity-default-values">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.defaultvalues.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
</properties>
</persistence-unit>
<persistence-unit name="jpa-h2-criteria">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.criteria.Item</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
<property name="javax.persistence.sql-load-script-source"
value="item.sql" />
</properties>
</persistence-unit>
<persistence-unit name="jpa-query-types">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.querytypes.UserEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
<property name="javax.persistence.sql-load-script-source"
value="users.sql" />
</properties>
</persistence-unit>
<persistence-unit name="jpa-projections">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.projections.Product</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
<property name="javax.persistence.sql-load-script-source"
value="products_jpa.sql" />
</properties>
</persistence-unit>
<persistence-unit name="jpa-h2-criteria">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.criteria.Item</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
<property name="javax.persistence.sql-load-script-source"
value="item.sql" />
</properties>
</persistence-unit>
</persistence>

View File

@@ -0,0 +1,41 @@
package com.baeldung.jpa.text;
import static org.junit.Assert.assertEquals;
import javax.persistence.PersistenceException;
import org.junit.BeforeClass;
import org.junit.Test;
public class JPATextUnitTest {
private static ExamRepository examRepository = null;
@BeforeClass
public static void once() {
examRepository = new ExamRepository();
}
@Test
public void givenExam_whenSaveExam_thenReturnExpectedExam() {
Exam exam = new Exam();
exam.setDescription("This is a description. Sometimes the description can be very very long! ");
exam.setText("This is a text. Sometimes the text can be very very long!");
exam.setShortText("A short text");
exam = examRepository.save(exam);
assertEquals(examRepository.find(exam.getId()), exam);
}
@Test(expected = PersistenceException.class)
public void givenExamWithVeryLongShortText_whenSaveExam_thenThrowPersistenceException() {
Exam exam = new Exam();
exam.setDescription("This is a very long text");
exam.setText("This is a long text");
exam.setShortText("This is a very long long short text. Maybe this can cause problems!!");
examRepository.save(exam);
}
}

View File

@@ -1,4 +1,8 @@
## Relevant articles:
## MongoDB
This module contains articles about MongoDB in Java.
### Relevant articles:
- [A Guide to MongoDB with Java](http://www.baeldung.com/java-mongodb)
- [A Simple Tagging Implementation with MongoDB](http://www.baeldung.com/mongodb-tagging)

View File

@@ -1,2 +1,6 @@
## JNoSQL
This module contains articles about JNoSQL.
### Relevant Articles:
- [A Guide to Eclipse JNoSQL](http://www.baeldung.com/eclipse-jnosql)

View File

@@ -1,3 +1,5 @@
## Relevant articles:
- [Introduction to Sirix](https://www.baeldung.com/introduction-to-sirix)
- [Introduction to Sirix](https://www.baeldung.com/introduction-to-sirix)
- [A Guide to SirixDB](https://www.baeldung.com/sirix)

View File

@@ -431,6 +431,7 @@
<module>core-java-modules/core-java-sun</module>
<module>core-java-modules/core-java-string-conversions</module>
<module>core-java-modules/core-java-string-conversions-2</module>
<module>core-java-modules/core-java-string-apis</module>
<module>core-java-modules/core-java</module>
<module>core-java-modules/core-java-jvm</module>
<module>core-scala</module>
@@ -1191,6 +1192,7 @@
<module>core-java-modules/core-java-sun</module>
<module>core-java-modules/core-java-string-conversions</module>
<module>core-java-modules/core-java-string-conversions-2</module>
<module>core-java-modules/core-java-string-apis</module>
<module>core-scala</module>
<module>couchbase</module>
<module>custom-pmd</module>

View File

@@ -10,3 +10,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Spring Data Reactive Repositories with MongoDB](https://www.baeldung.com/spring-data-mongodb-reactive)
- [Spring Data MongoDB Tailable Cursors](https://www.baeldung.com/spring-data-mongodb-tailable-cursors)
- [A Quick Look at R2DBC with Spring Data](https://www.baeldung.com/spring-data-r2dbc)
- [Spring Data Reactive Repositories with Couchbase](https://www.baeldung.com/spring-data-reactive-couchbase)

View File

@@ -6,5 +6,6 @@ This module contains articles about reactive Spring 5 WebClient
The "REST With Spring" Classes: http://bit.ly/restwithspring
### Relevant Articles
- [Logging Spring WebClient Calls](https://www.baeldung.com/spring-log-webclient-calls)
- [Simultaneous Spring WebClient Calls](https://www.baeldung.com/spring-webclient-simultaneous-calls)

View File

@@ -15,4 +15,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Spring ResponseStatusException](https://www.baeldung.com/spring-response-status-exception)
- [Spring Assert Statements](https://www.baeldung.com/spring-assert)
- [Configuring a Hikari Connection Pool with Spring Boot](https://www.baeldung.com/spring-boot-hikari)
- [Difference between \<context:annotation-config\> vs \<context:component-scan\>](https://www.baeldung.com/spring-contextannotation-contextcomponentscan)

View File

@@ -9,3 +9,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Create a Custom Auto-Configuration with Spring Boot](https://www.baeldung.com/spring-boot-custom-auto-configuration)
- [Guide to ApplicationContextRunner in Spring Boot](https://www.baeldung.com/spring-boot-context-runner)
- [A Guide to Spring Boot Configuration Metadata](https://www.baeldung.com/spring-boot-configuration-metadata)

View File

@@ -4,3 +4,5 @@ This module contains articles about Spring with FreeMarker
### Relevant Articles:
- [Introduction to Using FreeMarker in Spring MVC](https://www.baeldung.com/freemarker-in-spring-mvc-tutorial)
- [FreeMarker Common Operations](https://www.baeldung.com/freemarker-operations)

View File

@@ -68,6 +68,11 @@
<artifactId>spring-integration-security</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
@@ -75,6 +80,12 @@
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
</dependencies>
<build>
@@ -106,7 +117,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.0.3.RELEASE</spring.version>
<spring.version>5.0.13.RELEASE</spring.version>
<spring-social.version>1.1.4.RELEASE</spring-social.version>
<javax-mail.version>1.4.7</javax-mail.version>
<javax-activation.version>1.1.1</javax-activation.version>

View File

@@ -0,0 +1,27 @@
package com.baeldung.tx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
public class ServiceActivator {
@Autowired
private JdbcTemplate jdbcTemplate;
private final Logger log = LoggerFactory.getLogger(this.getClass());
public void checkTestResults(String payload) {
this.jdbcTemplate.update("insert into STUDENT values(?)", payload);
if (payload.toLowerCase().startsWith("fail")) {
log.error("Service failure. Test result: {}", payload);
throw new RuntimeException("Service failure.");
}
log.info("Service success. Test result: {}", payload);
}
}

View File

@@ -0,0 +1,155 @@
package com.baeldung.tx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.dsl.Pollers;
import org.springframework.integration.file.FileReadingMessageSource;
import org.springframework.integration.file.filters.SimplePatternFileListFilter;
import org.springframework.integration.file.transformer.FileToStringTransformer;
import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.integration.transaction.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.messaging.MessageChannel;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import javax.sql.DataSource;
import java.io.File;
import java.util.Scanner;
@Configuration
@EnableIntegration
public class TxIntegrationConfig {
private final Logger log = LoggerFactory.getLogger(this.getClass());
public final String INPUT_DIR = System.getProperty("java.io.tmpdir") + "/tx/";
public final String FILE_PATTERN = "*.txt";
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private DataSource dataSource;
@Autowired
private TransactionSynchronizationFactory transactionSynchronizationFactory;
@Autowired
DataSourceTransactionManager txManager;
@Bean
public MessageChannel inputChannel() {
return new DirectChannel();
}
@Bean
@InboundChannelAdapter(value = "inputChannel", poller = @Poller(value = "pollerMetadata"))
public MessageSource<File> fileReadingMessageSource() {
FileReadingMessageSource sourceReader = new FileReadingMessageSource();
sourceReader.setDirectory(new File(INPUT_DIR));
sourceReader.setFilter(new SimplePatternFileListFilter(FILE_PATTERN));
return sourceReader;
}
@Bean
public PollerMetadata pollerMetadata() {
return Pollers.fixedDelay(5000)
.advice(transactionInterceptor())
.transactionSynchronizationFactory(transactionSynchronizationFactory)
.get();
}
private TransactionInterceptor transactionInterceptor() {
return new TransactionInterceptorBuilder().transactionManager(txManager).build();
}
@Bean
public TransactionSynchronizationFactory transactionSynchronizationFactory() {
ExpressionEvaluatingTransactionSynchronizationProcessor processor =
new ExpressionEvaluatingTransactionSynchronizationProcessor();
SpelExpressionParser spelParser = new SpelExpressionParser();
processor.setAfterCommitExpression(
spelParser.parseExpression(
"payload.renameTo(new java.io.File(payload.absolutePath + '.PASSED'))"));
processor.setAfterRollbackExpression(
spelParser.parseExpression(
"payload.renameTo(new java.io.File(payload.absolutePath + '.FAILED'))"));
return new DefaultTransactionSynchronizationFactory(processor);
}
@Bean
@Transformer(inputChannel = "inputChannel", outputChannel = "toServiceChannel")
public FileToStringTransformer fileToStringTransformer() {
return new FileToStringTransformer();
}
@ServiceActivator(inputChannel = "toServiceChannel")
public void serviceActivator(String payload) {
jdbcTemplate.update("insert into STUDENT values(?)", payload);
if (payload.toLowerCase().startsWith("fail")) {
log.error("Service failure. Test result: {} ", payload);
throw new RuntimeException("Service failure.");
}
log.info("Service success. Test result: {}", payload);
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:table.sql")
.build();
}
@Bean
public DataSourceTransactionManager txManager() {
return new DataSourceTransactionManager(dataSource);
}
public static void main(final String... args) {
final AbstractApplicationContext context = new AnnotationConfigApplicationContext(TxIntegrationConfig.class);
context.registerShutdownHook();
final Scanner scanner = new Scanner(System.in);
System.out.print("Integration flow is running. Type q + <enter> to quit ");
while (true) {
final String input = scanner.nextLine();
if ("q".equals(input.trim())) {
context.close();
scanner.close();
break;
}
}
System.exit(0);
}
}

View File

@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int-file="http://www.springframework.org/schema/integration/file"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc https://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file https://www.springframework.org/schema/integration/file/spring-integration-file.xsd
http://www.springframework.org/schema/integration/jdbc https://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:property-placeholder/>
<int-file:inbound-channel-adapter
channel="inputChannel"
auto-create-directory="true"
filename-pattern="*.txt"
directory="${java.io.tmpdir}/tx/">
<int:poller fixed-delay="500">
<int:transactional transaction-manager="txManager" synchronization-factory="syncFactory"/>
</int:poller>
</int-file:inbound-channel-adapter>
<int:transaction-synchronization-factory id="syncFactory">
<int:after-commit expression="payload.renameTo(new java.io.File(payload.absolutePath + '.PASSED'))"
channel="infoLogger"/>
<int:after-rollback expression="payload.renameTo(new java.io.File(payload.absolutePath + '.FAILED'))"
channel="errorLogger"/>
</int:transaction-synchronization-factory>
<int:channel id="inputChannel"/>
<int-file:file-to-string-transformer input-channel="inputChannel" output-channel="toServiceChannel"/>
<int:service-activator input-channel="toServiceChannel"
ref="serviceActivator"
method="checkTestResults"/>
<int:logging-channel-adapter id="infoLogger" level="INFO"/>
<int:logging-channel-adapter id="errorLogger" level="ERROR"/>
<bean id="serviceActivator" class="com.baeldung.tx.ServiceActivator"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath*:table.sql"/>
</jdbc:embedded-database>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<int-jdbc:inbound-channel-adapter channel="infoLogger"
query="select TEST from STUDENT" data-source="dataSource">
<int:poller fixed-delay="5000"/>
</int-jdbc:inbound-channel-adapter>
</beans>

View File

@@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS STUDENT (
TEST VARCHAR(256)
);

View File

@@ -0,0 +1,56 @@
package com.baeldung.tx;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public final class TxIntegrationTest {
private static final String CONTEXT_CONFIG = "classpath:META-INF/spring/integration/spring-integration-tx-context.xml";
@Test
public void whenFileDoesntStartWithFail_thenTxSuccessful() throws InterruptedException, IOException {
final AbstractApplicationContext context =
new ClassPathXmlApplicationContext(CONTEXT_CONFIG);
String fileName = System.getProperty("java.io.tmpdir") + "/tx/test1.txt";
FileWriter fw = new FileWriter(fileName);
fw.write("PASSED!");
fw.close();
context.registerShutdownHook();
Thread.sleep(5000);
File file = new File(fileName + ".PASSED");
Assert.assertTrue(file.exists());
}
@Test
public void whenFileStartsWithFail_thenTxFailed() {
String fileName = System.getProperty("java.io.tmpdir") + "/tx/test2.txt";
try {
final AbstractApplicationContext context =
new ClassPathXmlApplicationContext(CONTEXT_CONFIG);
FileWriter fw = new FileWriter(fileName);
fw.write("FAILED!");
fw.close();
context.registerShutdownHook();
Thread.sleep(5000);
} catch (Exception e) {
// Exception is expected, do nothing
}
File file = new File(fileName + ".FAILED");
Assert.assertTrue(file.exists());
}
}

View File

@@ -5,4 +5,5 @@ This module contains articles about Spring MVC
## Relevant articles:
- [How to Read HTTP Headers in Spring REST Controllers](https://www.baeldung.com/spring-rest-http-headers)
- [A Custom Data Binder in Spring MVC](https://www.baeldung.com/spring-mvc-custom-data-binder)
- [Validating Lists in a Spring Controller](https://www.baeldung.com/spring-validate-list-controller)
- More articles: [[<-- prev]](/spring-mvc-simple)

View File

@@ -14,6 +14,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [web.xml vs Initializer with Spring](https://www.baeldung.com/spring-xml-vs-java-config)
- [A Java Web Application Without a web.xml](https://www.baeldung.com/java-web-app-without-web-xml)
- [Validating RequestParams and PathVariables in Spring](https://www.baeldung.com/spring-validate-requestparam-pathvariable)
- [Debugging the Spring MVC 404 “No mapping found for HTTP request” Error](https://www.baeldung.com/spring-mvc-404-error)
## Spring MVC with XML Configuration Example Project
- access a sample jsp page at: `http://localhost:8080/spring-mvc-xml/sample.html`

View File

@@ -17,3 +17,5 @@ The "REST With Spring" Classes: http://github.learnspringsecurity.com
- [Spring Security Whitelist IP Range](https://www.baeldung.com/spring-security-whitelist-ip-range)
- [Find the Registered Spring Security Filters](https://www.baeldung.com/spring-security-registered-filters)
- [HTTPS using Self-Signed Certificate in Spring Boot](https://www.baeldung.com/spring-boot-https-self-signed-certificate)
- [Spring Security: Exploring JDBC Authentication](https://www.baeldung.com/spring-security-jdbc-authentication)

View File

@@ -1,3 +0,0 @@
### Relevant Articles:
- [Spring Security: Exploring JDBC Authentication](https://www.baeldung.com/spring-security-jdbc-authentication)

View File

@@ -3,3 +3,5 @@
- [Working with Enums in Thymeleaf](https://www.baeldung.com/thymeleaf-enums)
- [Changing the Thymeleaf Template Directory in Spring Boot](https://www.baeldung.com/spring-thymeleaf-template-directory)
- [Spring Request Parameters with Thymeleaf](https://www.baeldung.com/spring-thymeleaf-request-parameters)
- [Thymeleaf lists Utility Object](https://www.baeldung.com/thymeleaf-lists-utility)

View File

@@ -2,3 +2,5 @@
- [JUnit 5 TestWatcher API](https://www.baeldung.com/junit-testwatcher)
- [JUnit Custom Display Name Generator API](https://www.baeldung.com/junit-custom-display-name-generator)
- [@TestInstance Annotation in JUnit 5](https://www.baeldung.com/junit-testinstance-annotation)

View File

@@ -1,4 +1,8 @@
## Relevant articles:
## Vaadin
This module contains articles about Vaadin.
### Relevant articles:
- [Introduction to Vaadin](https://www.baeldung.com/vaadin)
- [Sample Application with Spring Boot and Vaadin](https://www.baeldung.com/spring-boot-vaadin)

View File

@@ -1,3 +1,7 @@
## Vavr
This module contains articles about Vavr.
### Relevant Articles:
- [Introduction to Vavr](https://www.baeldung.com/vavr)
- [Guide to Try in Vavr](https://www.baeldung.com/vavr-try)

View File

@@ -10,3 +10,5 @@ This module contains articles about eXtensible Markup Language (XML)
- [Write an org.w3.dom.Document to a File](https://www.baeldung.com/java-write-xml-document-file)
- [Modifying an XML Attribute in Java](https://www.baeldung.com/java-modify-xml-attribute)
- [Convert XML to HTML in Java](https://www.baeldung.com/java-convert-xml-to-html)
- [Parsing an XML File Using StAX](https://www.baeldung.com/java-stax)