Merge branch 'eugenp:master' into master

This commit is contained in:
Ekatereana 2023-11-03 17:54:04 +02:00 committed by GitHub
commit e2ddfeefb5
2244 changed files with 37568 additions and 7880 deletions

32
.github/ISSUE_TEMPLATE/issue_report.md vendored Normal file
View File

@ -0,0 +1,32 @@
---
name: Issue Report
about: Report an issue to help us improve
title: '[ISSUE] '
---
**Article and Module Links**
A link to the affected article and the affected module. You can find the link to the module in the Conclusion section in the "on Github" standard phase.
**Describe the Issue**
A clear and concise description of what the issue is.
**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error
**Expected Behavior**
A clear and concise description of what you expected to happen.
**Screenshots**
If applicable, add screenshots to help explain your problem.
**Environment (please complete the following information):**
- OS: [e.g. Windows]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]
**Additional Context**
Add any other context about the issue here.

5
.gitignore vendored
View File

@ -124,4 +124,7 @@ devDb*.db
*.xjb *.xjb
#neo4j #neo4j
persistence-modules/neo4j/data/** persistence-modules/neo4j/data/**
/deep-shallow-copy/.mvn/wrapper
/deep-shallow-copy/mvnw
/deep-shallow-copy/mvnw.cmd

11
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,11 @@
# Contributing to Baeldung Tutorials
First off, thank you for considering contributing to Baeldung Tutorials.
## Reporting Issues
Before you submit an issue, please review the guidelines below:
1. **No Custom Modifications:** If your issue arises from any custom modifications you've made to the code in the repository, we won't be able to assist. We can only help if the issue is reproducible with the untouched codebase from this repo. If you're working with a modified version, consider asking for help on StackOverflow or other relevant forums.
2. **Use a clear and descriptive title** for the issue to identify the problem.
3. **Include a link to the article** you're having issues with.
4. **Describe the exact steps which reproduce the problem** in as many details as possible.
5. **Additional Details:** Offer any other context or descriptions that could be useful. Screenshots, error messages, copy/pasteable snippets, or logs can be immensely helpful.

View File

@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory;
public class KadaneAlgorithm { public class KadaneAlgorithm {
private Logger logger = LoggerFactory.getLogger(BruteForceAlgorithm.class.getName()); private Logger logger = LoggerFactory.getLogger(KadaneAlgorithm.class.getName());
public int maxSubArraySum(int[] arr) { public int maxSubArraySum(int[] arr) {
@ -14,15 +14,15 @@ public class KadaneAlgorithm {
int end = 0; int end = 0;
int maxSoFar = arr[0], maxEndingHere = arr[0]; int maxSoFar = arr[0], maxEndingHere = arr[0];
for (int i = 1; i < size; i++) { for (int i = 1; i < size; i++) {
maxEndingHere = maxEndingHere + arr[i];
if (arr[i] > maxEndingHere + arr[i]) { if (arr[i] > maxEndingHere) {
start = i;
maxEndingHere = arr[i]; maxEndingHere = arr[i];
} else { if (maxSoFar < maxEndingHere) {
maxEndingHere = maxEndingHere + arr[i]; start = i;
}
} }
if (maxSoFar < maxEndingHere) { if (maxSoFar < maxEndingHere) {
maxSoFar = maxEndingHere; maxSoFar = maxEndingHere;
end = i; end = i;

View File

@ -7,7 +7,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
class KadaneAlgorithmUnitTest { class KadaneAlgorithmUnitTest {
@Test @Test
void givenArrayWithNegativeNumberWhenMaximumSubarrayThenReturns6() { void givenArrayWithNegativeNumberWhenMaximumSubarrayThenReturnsExpectedResult() {
//given //given
int[] arr = new int[] { -3, 1, -8, 4, -1, 2, 1, -5, 5 }; int[] arr = new int[] { -3, 1, -8, 4, -1, 2, 1, -5, 5 };
//when //when
@ -27,7 +27,7 @@ class KadaneAlgorithmUnitTest {
//then //then
assertEquals(-1, maxSum); assertEquals(-1, maxSum);
} }
@Test @Test
void givenArrayWithAllPosiitveNumbersWhenMaximumSubarrayThenReturnsExpectedResult() { void givenArrayWithAllPosiitveNumbersWhenMaximumSubarrayThenReturnsExpectedResult() {
//given //given
@ -39,4 +39,15 @@ class KadaneAlgorithmUnitTest {
assertEquals(10, maxSum); assertEquals(10, maxSum);
} }
@Test
void givenArrayToTestStartIndexWhenMaximumSubarrayThenReturnsExpectedResult() {
//given
int[] arr = new int[] { 1, 2, -1, 3, -6, -2 };
//when
KadaneAlgorithm algorithm = new KadaneAlgorithm();
int maxSum = algorithm.maxSubArraySum(arr);
//then
assertEquals(5, maxSum);
}
} }

View File

@ -3,4 +3,6 @@
- [Algorithm to Identify and Validate a Credit Card Number](https://www.baeldung.com/java-validate-cc-number) - [Algorithm to Identify and Validate a Credit Card Number](https://www.baeldung.com/java-validate-cc-number)
- [Find the N Most Frequent Elements in a Java Array](https://www.baeldung.com/java-n-most-frequent-elements-array) - [Find the N Most Frequent Elements in a Java Array](https://www.baeldung.com/java-n-most-frequent-elements-array)
- [Getting Pixel Array From Image in Java](https://www.baeldung.com/java-getting-pixel-array-from-image) - [Getting Pixel Array From Image in Java](https://www.baeldung.com/java-getting-pixel-array-from-image)
- [Calculate Distance Between Two Coordinates in Java](https://www.baeldung.com/java-find-distance-between-points)
- [Rotate Arrays in Java](https://www.baeldung.com/java-rotate-arrays)
- More articles: [[<-- prev]](/algorithms-miscellaneous-6) - More articles: [[<-- prev]](/algorithms-miscellaneous-6)

View File

@ -0,0 +1,19 @@
package com.baeldung.algorithms.latlondistance;
public class EquirectangularApproximation {
private static final int EARTH_RADIUS = 6371; // Approx Earth radius in KM
public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
double lat1Rad = Math.toRadians(lat1);
double lat2Rad = Math.toRadians(lat2);
double lon1Rad = Math.toRadians(lon1);
double lon2Rad = Math.toRadians(lon2);
double x = (lon2Rad - lon1Rad) * Math.cos((lat1Rad + lat2Rad) / 2);
double y = (lat2Rad - lat1Rad);
double distance = Math.sqrt(x * x + y * y) * EARTH_RADIUS;
return distance;
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.algorithms.latlondistance;
public class HaversineDistance {
private static final int EARTH_RADIUS = 6371; // Approx Earth radius in KM
public static double calculateDistance(double startLat, double startLong,
double endLat, double endLong) {
double dLat = Math.toRadians((endLat - startLat));
double dLong = Math.toRadians((endLong - startLong));
startLat = Math.toRadians(startLat);
endLat = Math.toRadians(endLat);
double a = haversine(dLat) + Math.cos(startLat) * Math.cos(endLat) * haversine(dLong);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return EARTH_RADIUS * c;
}
public static double haversine(double val) {
return Math.pow(Math.sin(val / 2), 2);
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.algorithms.latlondistance;
public class VincentyDistance {
// Constants for WGS84 ellipsoid model of Earth
private static final double SEMI_MAJOR_AXIS_MT = 6378137;
private static final double SEMI_MINOR_AXIS_MT = 6356752.314245;
private static final double FLATTENING = 1 / 298.257223563;
private static final double ERROR_TOLERANCE = 1e-12;
public static double calculateDistance(double latitude1, double longitude1, double latitude2, double longitude2) {
double U1 = Math.atan((1 - FLATTENING) * Math.tan(Math.toRadians(latitude1)));
double U2 = Math.atan((1 - FLATTENING) * Math.tan(Math.toRadians(latitude2)));
double sinU1 = Math.sin(U1);
double cosU1 = Math.cos(U1);
double sinU2 = Math.sin(U2);
double cosU2 = Math.cos(U2);
double longitudeDifference = Math.toRadians(longitude2 - longitude1);
double previousLongitudeDifference;
double sinSigma, cosSigma, sigma, sinAlpha, cosSqAlpha, cos2SigmaM;
do {
sinSigma = Math.sqrt(Math.pow(cosU2 * Math.sin(longitudeDifference), 2) +
Math.pow(cosU1 * sinU2 - sinU1 * cosU2 * Math.cos(longitudeDifference), 2));
cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * Math.cos(longitudeDifference);
sigma = Math.atan2(sinSigma, cosSigma);
sinAlpha = cosU1 * cosU2 * Math.sin(longitudeDifference) / sinSigma;
cosSqAlpha = 1 - Math.pow(sinAlpha, 2);
cos2SigmaM = cosSigma - 2 * sinU1 * sinU2 / cosSqAlpha;
if (Double.isNaN(cos2SigmaM)) {
cos2SigmaM = 0;
}
previousLongitudeDifference = longitudeDifference;
double C = FLATTENING / 16 * cosSqAlpha * (4 + FLATTENING * (4 - 3 * cosSqAlpha));
longitudeDifference = Math.toRadians(longitude2 - longitude1) + (1 - C) * FLATTENING * sinAlpha *
(sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * Math.pow(cos2SigmaM, 2))));
} while (Math.abs(longitudeDifference - previousLongitudeDifference) > ERROR_TOLERANCE);
double uSq = cosSqAlpha * (Math.pow(SEMI_MAJOR_AXIS_MT, 2) - Math.pow(SEMI_MINOR_AXIS_MT, 2)) / Math.pow(SEMI_MINOR_AXIS_MT, 2);
double A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
double B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
double deltaSigma = B * sinSigma * (cos2SigmaM + B / 4 * (cosSigma * (-1 + 2 * Math.pow(cos2SigmaM, 2)) -
B / 6 * cos2SigmaM * (-3 + 4 * Math.pow(sinSigma, 2)) * (-3 + 4 * Math.pow(cos2SigmaM, 2))));
double distanceMt = SEMI_MINOR_AXIS_MT * A * (sigma - deltaSigma);
return distanceMt / 1000;
}
}

View File

@ -0,0 +1,102 @@
package com.baeldung.algorithms.rotatearray;
/**
* To speed up the rotation, we narrow k rotations to the remainder of k divided by the array length, or k module the array length.
* Therefore, a large rotation number will be translated into the relative smallest rotation.
* All solutions replace the original array, although they might use an extra array to compute the rotation.
*/
public class RotateArray {
private RotateArray() {
throw new IllegalStateException("Rotate array algorithm utility methods class");
}
/**
*
* @param arr array to apply rotation to
* @param k number of rotations
*/
public static void bruteForce(int[] arr, int k) {
checkInvalidInput(arr, k);
k %= arr.length;
int temp;
int previous;
for (int i = 0; i < k; i++) {
previous = arr[arr.length - 1];
for (int j = 0; j < arr.length; j++) {
temp = arr[j];
arr[j] = previous;
previous = temp;
}
}
}
/**
*
* @param arr array to apply rotation to
* @param k number of rotations
*/
public static void withExtraArray(int[] arr, int k) {
checkInvalidInput(arr, k);
int[] extraArray = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
extraArray[(i + k) % arr.length] = arr[i];
}
System.arraycopy(extraArray, 0, arr, 0, arr.length);
}
/**
*
* @param arr array to apply rotation to
* @param k number of rotations
*/
public static void cyclicReplacement(int[] arr, int k) {
checkInvalidInput(arr, k);
k = k % arr.length;
int count = 0;
for (int start = 0; count < arr.length; start++) {
int current = start;
int prev = arr[start];
do {
int next = (current + k) % arr.length;
int temp = arr[next];
arr[next] = prev;
prev = temp;
current = next;
count++;
} while (start != current);
}
}
/**
*
* @param arr array to apply rotation to
* @param k number of rotations
*/
public static void reverse(int[] arr, int k) {
checkInvalidInput(arr, k);
k %= arr.length;
reverse(arr, 0, arr.length - 1);
reverse(arr, 0, k - 1);
reverse(arr, k, arr.length - 1);
}
private static void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
private static void checkInvalidInput(int[] arr, int rotation) {
if (rotation < 1 || arr == null)
throw new IllegalArgumentException("Rotation must be greater than zero or array must be not null");
}
}

View File

@ -0,0 +1,129 @@
package com.baeldung.algorithms.connect4;
import java.util.ArrayList;
import java.util.List;
public class GameBoard {
private final List<List<Piece>> columns;
private final int rows;
public GameBoard(int columns, int rows) {
this.rows = rows;
this.columns = new ArrayList<>();
for (int i = 0; i < columns; ++i) {
this.columns.add(new ArrayList<>());
}
}
public int getRows() {
return rows;
}
public int getColumns() {
return columns.size();
}
public Piece getCell(int x, int y) {
assert(x >= 0 && x < getColumns());
assert(y >= 0 && y < getRows());
List<Piece> column = columns.get(x);
if (column.size() > y) {
return column.get(y);
} else {
return null;
}
}
public boolean move(int x, Piece player) {
assert(x >= 0 && x < getColumns());
List<Piece> column = columns.get(x);
if (column.size() >= this.rows) {
throw new IllegalArgumentException("That column is full");
}
column.add(player);
return checkWin(x, column.size() - 1, player);
}
private boolean checkWin(int x, int y, Piece player) {
// Vertical line
if (checkLine(x, y, 0, -1, player)) {
return true;
}
for (int offset = 0; offset < 4; ++offset) {
// Horizontal line
if (checkLine(x - 3 + offset, y, 1, 0, player)) {
return true;
}
// Leading diagonal
if (checkLine(x - 3 + offset, y + 3 - offset, 1, -1, player)) {
return true;
}
// Trailing diagonal
if (checkLine(x - 3 + offset, y - 3 + offset, 1, 1, player)) {
return true;
}
}
return false;
}
private boolean checkLine(int x1, int y1, int xDiff, int yDiff, Piece player) {
for (int i = 0; i < 4; ++i) {
int x = x1 + (xDiff * i);
int y = y1 + (yDiff * i);
if (x < 0 || x > columns.size() - 1) {
return false;
}
if (y < 0 || y > rows - 1) {
return false;
}
if (player != getCell(x, y)) {
return false;
}
}
return true;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
for (int y = getRows() - 1; y >= 0; --y) {
for (int x = 0; x < getColumns(); ++x) {
Piece piece = getCell(x, y);
result.append("|");
if (piece == null) {
result.append(" ");
} else if (piece == Piece.PLAYER_1) {
result.append("X");
} else if (piece == Piece.PLAYER_2) {
result.append("O");
}
}
result.append("|\n");
for (int i = 0; i < getColumns(); ++i) {
result.append("+-");
}
result.append("+\n");
}
return result.toString();
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.algorithms.connect4;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class GameUnitTest {
@Test
public void blankGame() {
GameBoard gameBoard = new GameBoard(8, 6);
System.out.println(gameBoard);
}
@Test
public void playedGame() {
GameBoard gameBoard = new GameBoard(8, 6);
assertFalse(gameBoard.move(3, Piece.PLAYER_1));
assertFalse(gameBoard.move(2, Piece.PLAYER_2));
assertFalse(gameBoard.move(4, Piece.PLAYER_1));
assertFalse(gameBoard.move(3, Piece.PLAYER_2));
assertFalse(gameBoard.move(5, Piece.PLAYER_1));
assertFalse(gameBoard.move(6, Piece.PLAYER_2));
assertFalse(gameBoard.move(5, Piece.PLAYER_1));
assertFalse(gameBoard.move(4, Piece.PLAYER_2));
assertFalse(gameBoard.move(5, Piece.PLAYER_1));
assertFalse(gameBoard.move(5, Piece.PLAYER_2));
assertFalse(gameBoard.move(6, Piece.PLAYER_1));
assertTrue(gameBoard.move(4, Piece.PLAYER_2));
System.out.println(gameBoard);
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.algorithms.connect4;
public enum Piece {
PLAYER_1,
PLAYER_2
}

View File

@ -0,0 +1,26 @@
package com.baeldung.algorithms.latlondistance;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
class GeoDistanceUnitTest {
@Test
public void testCalculateDistance() {
double lat1 = 40.714268; // New York
double lon1 = -74.005974;
double lat2 = 34.0522; // Los Angeles
double lon2 = -118.2437;
double equirectangularDistance = EquirectangularApproximation.calculateDistance(lat1, lon1, lat2, lon2);
double haversineDistance = HaversineDistance.calculateDistance(lat1, lon1, lat2, lon2);
double vincentyDistance = VincentyDistance.calculateDistance(lat1, lon1, lat2, lon2);
double expectedDistance = 3944;
assertTrue(Math.abs(equirectangularDistance - expectedDistance) < 100);
assertTrue(Math.abs(haversineDistance - expectedDistance) < 10);
assertTrue(Math.abs(vincentyDistance - expectedDistance) < 0.5);
}
}

View File

@ -0,0 +1,124 @@
package com.baeldung.algorithms.rotatearray;
import static com.baeldung.algorithms.rotatearray.RotateArray.bruteForce;
import static com.baeldung.algorithms.rotatearray.RotateArray.cyclicReplacement;
import static com.baeldung.algorithms.rotatearray.RotateArray.reverse;
import static com.baeldung.algorithms.rotatearray.RotateArray.withExtraArray;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
class RotateArrayUnitTest {
private final int[] arr = { 1, 2, 3, 4, 5, 6 };
private final int rotationLtArrayLength = 1;
private final int rotationGtArrayLength = arr.length + 2;
private final int[] ltArrayLengthRotation = { 6, 1, 2, 3, 4, 5 };
private final int[] gtArrayLengthRotation = { 5, 6, 1, 2, 3, 4 };
@Test
void givenInputArray_whenNoRotationOrEmptyArray_thenThrowIllegalArgumentException() {
final int noRotation = 0;
final int someRotation = arr.length - 1;
assertThrows(IllegalArgumentException.class, () -> bruteForce(arr, noRotation));
assertThrows(IllegalArgumentException.class, () -> withExtraArray(arr, noRotation));
assertThrows(IllegalArgumentException.class, () -> cyclicReplacement(arr, noRotation));
assertThrows(IllegalArgumentException.class, () -> reverse(arr, noRotation));
assertThrows(IllegalArgumentException.class, () -> bruteForce(null, someRotation));
assertThrows(IllegalArgumentException.class, () -> withExtraArray(null, someRotation));
assertThrows(IllegalArgumentException.class, () -> cyclicReplacement(null, someRotation));
assertThrows(IllegalArgumentException.class, () -> reverse(null, someRotation));
}
@Test
void givenInputArray_whenUseBruteForceRotationLtArrayLength_thenRotateArrayOk() {
bruteForce(arr, rotationLtArrayLength);
assertArrayEquals(ltArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseBruteForceRotationGtArrayLength_thenRotateArrayOk() {
bruteForce(arr, rotationGtArrayLength);
assertArrayEquals(gtArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseBruteForceRotationEqArrayLength_thenDoNothing() {
int[] expected = arr.clone();
bruteForce(arr, arr.length);
assertArrayEquals(expected, arr);
}
@Test
void givenInputArray_whenUseExtraArrayRotationLtArrayLength_thenRotateArrayOk() {
withExtraArray(arr, rotationLtArrayLength);
assertArrayEquals(ltArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseExtraArrayRotationGtArrayLength_thenRotateArrayOk() {
withExtraArray(arr, rotationGtArrayLength);
assertArrayEquals(gtArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseExtraArrayWithRotationEqArrayLength_thenDoNothing() {
int[] clone = arr.clone();
withExtraArray(arr, arr.length);
assertArrayEquals(clone, arr);
}
@Test
void givenInputArray_whenUseCyclicReplacementRotationLtArrayLength_thenRotateArrayOk() {
cyclicReplacement(arr, rotationLtArrayLength);
assertArrayEquals(ltArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseCyclicReplacementRotationGtArrayLength_thenRotateArrayOk() {
cyclicReplacement(arr, rotationGtArrayLength);
assertArrayEquals(gtArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseCyclicReplacementRotationEqArrayLength_thenDoNothing() {
int[] clone = arr.clone();
cyclicReplacement(arr, arr.length);
assertArrayEquals(clone, arr);
}
@Test
void givenInputArray_whenUseReverseRotationLtArrayLength_thenRotateArrayOk() {
reverse(arr, rotationLtArrayLength);
assertArrayEquals(ltArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseReverseRotationGtArrayLength_thenRotateArrayOk() {
reverse(arr, rotationGtArrayLength);
assertArrayEquals(gtArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseReverseRotationEqArrayLength_thenDoNothing() {
int[] clone = arr.clone();
reverse(arr, arr.length);
assertArrayEquals(clone, arr);
}
}

View File

@ -1,7 +0,0 @@
## Annotations
This module contains articles about Java annotations
### Relevant Articles:
- [Java Annotation Processing and Creating a Builder](https://www.baeldung.com/java-annotation-processing-builder)

View File

@ -1,21 +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>
<artifactId>annotations</artifactId>
<name>annotations</name>
<packaging>pom</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modules>
<module>annotation-processing</module>
<module>annotation-user</module>
</modules>
</project>

View File

@ -43,19 +43,19 @@
<dependency> <dependency>
<groupId>com.sun.xml.ws</groupId> <groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-ri</artifactId> <artifactId>jaxws-ri</artifactId>
<version>2.3.3</version> <version>${jaxws-ri.version}</version>
<type>pom</type> <type>pom</type>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version> <version>${javax.servlet-api.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId> <artifactId>jstl</artifactId>
<version>1.2</version> <version>${jstl.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
@ -117,7 +117,9 @@
<properties> <properties>
<spring.version>5.3.25</spring.version> <spring.version>5.3.25</spring.version>
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version> <cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version> <jstl.version>1.2</jstl.version>
<javax.servlet-api.version>4.0.1</javax.servlet-api.version>
<jaxws-ri.version>2.3.3</jaxws-ri.version>
</properties> </properties>
</project> </project>

View File

@ -1,36 +1,43 @@
package com.baeldung.tlsversion; package com.baeldung.tlsversion;
import javax.net.ssl.SSLSocket;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import java.io.IOException; import java.io.IOException;
import javax.net.ssl.SSLSocket;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.config.TlsConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.ssl.TLS;
import org.apache.hc.core5.ssl.SSLContexts;
import org.apache.hc.core5.util.Timeout;
public class ClientTlsVersionExamples { public class ClientTlsVersionExamples {
public static CloseableHttpClient setViaSocketFactory() { public static CloseableHttpClient setViaSocketFactory() {
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
SSLContexts.createDefault(), .setDefaultTlsConfig(TlsConfig.custom()
new String[] { "TLSv1.2", "TLSv1.3" }, .setHandshakeTimeout(Timeout.ofSeconds(30))
null, .setSupportedProtocols(TLS.V_1_2, TLS.V_1_3)
SSLConnectionSocketFactory.getDefaultHostnameVerifier()); .build())
.build();
return HttpClients.custom().setSSLSocketFactory(sslsf).build(); return HttpClients.custom()
.setConnectionManager(cm)
.build();
} }
public static CloseableHttpClient setTlsVersionPerConnection() { public static CloseableHttpClient setTlsVersionPerConnection() {
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(SSLContexts.createDefault()) { SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(SSLContexts.createDefault()) {
@Override @Override
protected void prepareSocket(SSLSocket socket) { protected void prepareSocket(SSLSocket socket) {
String hostname = socket.getInetAddress().getHostName(); String hostname = socket.getInetAddress()
.getHostName();
if (hostname.endsWith("internal.system.com")) { if (hostname.endsWith("internal.system.com")) {
socket.setEnabledProtocols(new String[] { "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" }); socket.setEnabledProtocols(new String[] { "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" });
} else { } else {
@ -39,7 +46,14 @@ public class ClientTlsVersionExamples {
} }
}; };
return HttpClients.custom().setSSLSocketFactory(sslsf).build(); HttpClientConnectionManager connManager = PoolingHttpClientConnectionManagerBuilder.create()
.setSSLSocketFactory(sslsf)
.build();
return HttpClients.custom()
.setConnectionManager(connManager)
.build();
} }
// To configure the TLS versions for the client, set the https.protocols system property during runtime. // To configure the TLS versions for the client, set the https.protocols system property during runtime.
@ -47,15 +61,11 @@ public class ClientTlsVersionExamples {
public static CloseableHttpClient setViaSystemProperties() { public static CloseableHttpClient setViaSystemProperties() {
return HttpClients.createSystem(); return HttpClients.createSystem();
// Alternatively: // Alternatively:
// return HttpClients.custom().useSystemProperties().build(); //return HttpClients.custom().useSystemProperties().build();
} }
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
// Alternatively: try (CloseableHttpClient httpClient = setViaSocketFactory(); CloseableHttpResponse response = httpClient.execute(new HttpGet("https://httpbin.org/"))) {
// CloseableHttpClient httpClient = setTlsVersionPerConnection();
// CloseableHttpClient httpClient = setViaSystemProperties();
try (CloseableHttpClient httpClient = setViaSocketFactory();
CloseableHttpResponse response = httpClient.execute(new HttpGet("https://httpbin.org/"))) {
HttpEntity entity = response.getEntity(); HttpEntity entity = response.getEntity();
EntityUtils.consume(entity); EntityUtils.consume(entity);

View File

@ -14,6 +14,7 @@ import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.cookie.BasicClientCookie; import org.apache.hc.client5.http.impl.cookie.BasicClientCookie;
import org.apache.hc.client5.http.protocol.HttpClientContext; import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.http.cookie.ClientCookie;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -51,8 +52,8 @@ class HttpClientGettingCookieValueUnitTest {
private BasicCookieStore createCustomCookieStore() { private BasicCookieStore createCustomCookieStore() {
BasicCookieStore cookieStore = new BasicCookieStore(); BasicCookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("custom_cookie", "test_value"); BasicClientCookie cookie = new BasicClientCookie("custom_cookie", "test_value");
cookie.setDomain("baeldung.com"); cookie.setDomain("github.com");
cookie.setAttribute("domain", "true"); cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "github.com");
cookie.setPath("/"); cookie.setPath("/");
cookieStore.addCookie(cookie); cookieStore.addCookie(cookie);
return cookieStore; return cookieStore;

View File

@ -16,7 +16,7 @@ public class ApacheHttpClient5UnitTest {
public static final String DUMMY_URL = "https://postman-echo.com/get"; public static final String DUMMY_URL = "https://postman-echo.com/get";
@Test @Test
public void whenUseApacheHttpClient_thenCorrect() throws IOException { void whenUseApacheHttpClient_thenCorrect() throws IOException {
HttpGet request = new HttpGet(DUMMY_URL); HttpGet request = new HttpGet(DUMMY_URL);
try (CloseableHttpClient client = HttpClients.createDefault()) { try (CloseableHttpClient client = HttpClients.createDefault()) {

View File

@ -1,6 +1,5 @@
package com.baeldung.httpclient.readresponsebodystring; package com.baeldung.httpclient.readresponsebodystring;
import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
@ -8,11 +7,13 @@ import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
public class HttpClientUnitTest { import org.junit.jupiter.api.Test;
class HttpClientUnitTest {
public static final String DUMMY_URL = "https://postman-echo.com/get"; public static final String DUMMY_URL = "https://postman-echo.com/get";
@Test @Test
public void whenUseHttpClient_thenCorrect() throws IOException, InterruptedException { void whenUseHttpClient_thenCorrect() throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(DUMMY_URL)).build(); HttpRequest request = HttpRequest.newBuilder().uri(URI.create(DUMMY_URL)).build();

View File

@ -1,7 +1,7 @@
package com.baeldung.httpclient.readresponsebodystring; package com.baeldung.httpclient.readresponsebodystring;
import org.junit.Assert;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
@ -10,12 +10,14 @@ import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import org.junit.jupiter.api.Test;
public class HttpUrlConnectionUnitTest { public class HttpUrlConnectionUnitTest {
public static final String DUMMY_URL = "https://postman-echo.com/get"; public static final String DUMMY_URL = "https://postman-echo.com/get";
@Test @Test
public void whenUseHttpUrlConnection_thenCorrect() throws IOException { void whenUseHttpUrlConnection_thenCorrect() throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(DUMMY_URL).openConnection(); HttpURLConnection connection = (HttpURLConnection) new URL(DUMMY_URL).openConnection();
InputStream inputStream = connection.getInputStream(); InputStream inputStream = connection.getInputStream();
@ -28,7 +30,7 @@ public class HttpUrlConnectionUnitTest {
response.append(currentLine); response.append(currentLine);
in.close(); in.close();
Assert.assertNotNull(response.toString()); assertNotNull(response.toString());
System.out.println("Response -> " + response.toString()); System.out.println("Response -> " + response.toString());
} }
} }

View File

@ -1,6 +1,6 @@
package com.baeldung.httpclient.readresponsebodystring; package com.baeldung.httpclient.readresponsebodystring;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
public class SpringRestTemplateUnitTest { public class SpringRestTemplateUnitTest {

View File

@ -1,6 +1,6 @@
package com.baeldung.httpclient.readresponsebodystring; package com.baeldung.httpclient.readresponsebodystring;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -8,7 +8,7 @@ public class SpringWebClientUnitTest {
public static final String DUMMY_URL = "https://postman-echo.com/get"; public static final String DUMMY_URL = "https://postman-echo.com/get";
@Test @Test
public void whenUseWebClientRetrieve_thenCorrect() { void whenUseWebClientRetrieve_thenCorrect() {
WebClient webClient = WebClient.create(DUMMY_URL); WebClient webClient = WebClient.create(DUMMY_URL);
Mono<String> body = webClient.get().retrieve().bodyToMono(String.class); Mono<String> body = webClient.get().retrieve().bodyToMono(String.class);
String s = body.block(); String s = body.block();

View File

@ -4,6 +4,7 @@
<pattern>%date [%level] %logger - %msg %n</pattern> <pattern>%date [%level] %logger - %msg %n</pattern>
</encoder> </encoder>
</appender> </appender>
<logger name="com.baeldung.httpclient.cookies" level="info"/>
<logger name="com.baeldung.httpclient.readresponsebodystring" level="debug"/> <logger name="com.baeldung.httpclient.readresponsebodystring" level="debug"/>
<logger name="org.apache.http" level="debug"/> <logger name="org.apache.http" level="debug"/>

View File

@ -15,45 +15,6 @@
</parent> </parent>
<dependencies> <dependencies>
<!-- http client -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>${httpclient.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>${httpasyncclient.version}</version> <!-- 4.0.2 --> <!-- 4.1-beta1 -->
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency> <dependency>
<groupId>org.apache.httpcomponents.core5</groupId> <groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId> <artifactId>httpcore5</artifactId>
@ -115,12 +76,8 @@
</build> </build>
<properties> <properties>
<!-- util -->
<httpasyncclient.version>4.1.4</httpasyncclient.version>
<!-- testing -->
<mockserver.version>5.6.1</mockserver.version> <mockserver.version>5.6.1</mockserver.version>
<wiremock.version>2.5.1</wiremock.version> <wiremock.version>2.5.1</wiremock.version>
<httpclient.version>4.5.8</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
<!-- http client & core 5 --> <!-- http client & core 5 -->
<httpcore5.version>5.2</httpcore5.version> <httpcore5.version>5.2</httpcore5.version>
<httpclient5.version>5.2</httpclient5.version> <httpclient5.version>5.2</httpclient5.version>

View File

@ -4,10 +4,10 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.apache.hc.core5.http.ParseException;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.apache.hc.client5.http.classic.methods.HttpPost; import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.mime.FileBody; import org.apache.hc.client5.http.entity.mime.FileBody;
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode; import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
@ -19,6 +19,7 @@ import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.ParseException;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -30,9 +31,6 @@ import java.net.URL;
class HttpClientMultipartLiveTest extends GetRequestMockServer { class HttpClientMultipartLiveTest extends GetRequestMockServer {
// No longer available
// private static final String SERVER = "http://echo.200please.com";
private static final String SERVER = "http://localhost:8080/spring-mvc-java/stub/multipart"; private static final String SERVER = "http://localhost:8080/spring-mvc-java/stub/multipart";
private static final String TEXTFILENAME = "temp.txt"; private static final String TEXTFILENAME = "temp.txt";
private static final String IMAGEFILENAME = "image.jpg"; private static final String IMAGEFILENAME = "image.jpg";

View File

@ -1,15 +1,16 @@
package com.baeldung.httpclient; package com.baeldung.httpclient;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import org.junit.jupiter.api.Test;
import org.apache.hc.client5.http.classic.methods.HttpGet; import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost; import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.DefaultRedirectStrategy; import org.apache.hc.client5.http.impl.DefaultRedirectStrategy;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import java.io.IOException; import java.io.IOException;

View File

@ -1,29 +1,5 @@
package com.baeldung.httpclient.advancedconfig; package com.baeldung.httpclient.advancedconfig;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.junit.Rule;
import org.junit.Test;
import java.io.IOException;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.containing; import static com.github.tomakehurst.wiremock.client.WireMock.containing;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
@ -32,18 +8,55 @@ import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class HttpClientAdvancedConfigurationIntegrationTest { import java.io.IOException;
@Rule import org.apache.hc.client5.http.auth.AuthCache;
public WireMockRule serviceMock = new WireMockRule(8089); import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.CredentialsProvider;
import org.apache.hc.client5.http.auth.StandardAuthScheme;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
import org.apache.hc.client5.http.impl.auth.BasicScheme;
import org.apache.hc.client5.http.impl.auth.CredentialsProviderBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.io.entity.StringEntity;
@Rule import org.junit.jupiter.api.AfterEach;
public WireMockRule proxyMock = new WireMockRule(8090); import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.github.tomakehurst.wiremock.WireMockServer;
class HttpClientAdvancedConfigurationIntegrationTest {
public WireMockServer serviceMock;
public WireMockServer proxyMock;
@BeforeEach
public void before () {
serviceMock = new WireMockServer(8089);
serviceMock.start();
proxyMock = new WireMockServer(8090);
proxyMock.start();
}
@AfterEach
public void after () {
serviceMock.stop();
proxyMock.stop();
}
@Test @Test
public void givenClientWithCustomUserAgentHeader_whenExecuteRequest_shouldReturn200() throws IOException { void givenClientWithCustomUserAgentHeader_whenExecuteRequest_shouldReturn200() throws IOException {
//given //given
String userAgent = "BaeldungAgent/1.0"; String userAgent = "BaeldungAgent/1.0";
serviceMock.stubFor(get(urlEqualTo("/detail")) serviceMock.stubFor(get(urlEqualTo("/detail"))
@ -59,11 +72,11 @@ public class HttpClientAdvancedConfigurationIntegrationTest {
HttpResponse response = httpClient.execute(httpGet); HttpResponse response = httpClient.execute(httpGet);
//then //then
assertEquals(response.getStatusLine().getStatusCode(), 200); assertEquals(200, response.getCode());
} }
@Test @Test
public void givenClientThatSendDataInBody_whenSendXmlInBody_shouldReturn200() throws IOException { void givenClientThatSendDataInBody_whenSendXmlInBody_shouldReturn200() throws IOException {
//given //given
String xmlBody = "<xml><id>1</id></xml>"; String xmlBody = "<xml><id>1</id></xml>";
serviceMock.stubFor(post(urlEqualTo("/person")) serviceMock.stubFor(post(urlEqualTo("/person"))
@ -82,12 +95,12 @@ public class HttpClientAdvancedConfigurationIntegrationTest {
HttpResponse response = httpClient.execute(httpPost); HttpResponse response = httpClient.execute(httpPost);
//then //then
assertEquals(response.getStatusLine().getStatusCode(), 200); assertEquals(200, response.getCode());
} }
@Test @Test
public void givenServerThatIsBehindProxy_whenClientIsConfiguredToSendRequestViaProxy_shouldReturn200() throws IOException { void givenServerThatIsBehindProxy_whenClientIsConfiguredToSendRequestViaProxy_shouldReturn200() throws IOException {
//given //given
proxyMock.stubFor(get(urlMatching(".*")) proxyMock.stubFor(get(urlMatching(".*"))
.willReturn(aResponse().proxiedFrom("http://localhost:8089/"))); .willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
@ -107,7 +120,7 @@ public class HttpClientAdvancedConfigurationIntegrationTest {
HttpResponse response = httpclient.execute(httpGet); HttpResponse response = httpclient.execute(httpGet);
//then //then
assertEquals(response.getStatusLine().getStatusCode(), 200); assertEquals(200, response.getCode());
proxyMock.verify(getRequestedFor(urlEqualTo("/private"))); proxyMock.verify(getRequestedFor(urlEqualTo("/private")));
serviceMock.verify(getRequestedFor(urlEqualTo("/private"))); serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
} }
@ -125,14 +138,12 @@ public class HttpClientAdvancedConfigurationIntegrationTest {
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
// Client credentials // Client credentials
CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); CredentialsProvider credentialsProvider = CredentialsProviderBuilder.create()
credentialsProvider.setCredentials(new AuthScope(proxy), .add(new AuthScope(proxy), "username_admin", "secret_password".toCharArray())
new UsernamePasswordCredentials("username_admin", "secret_password")); .build();
// Create AuthCache instance // Create AuthCache instance
AuthCache authCache = new BasicAuthCache(); AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache // Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme(); BasicScheme basicAuth = new BasicScheme();
authCache.put(proxy, basicAuth); authCache.put(proxy, basicAuth);
@ -149,10 +160,11 @@ public class HttpClientAdvancedConfigurationIntegrationTest {
//when //when
final HttpGet httpGet = new HttpGet("http://localhost:8089/private"); final HttpGet httpGet = new HttpGet("http://localhost:8089/private");
httpGet.setHeader("Authorization", StandardAuthScheme.BASIC);
HttpResponse response = httpclient.execute(httpGet, context); HttpResponse response = httpclient.execute(httpGet, context);
//then //then
assertEquals(response.getStatusLine().getStatusCode(), 200); assertEquals(200, response.getCode());
proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic"))); proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic")));
serviceMock.verify(getRequestedFor(urlEqualTo("/private"))); serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
} }

View File

@ -1,59 +0,0 @@
package com.baeldung.httpclient.base;
import com.baeldung.httpclient.ResponseUtil;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class HttpClientBasicPostLiveTest {
private static final String SAMPLE_URL = "http://www.github.com";
private CloseableHttpClient instance;
private CloseableHttpResponse response;
@Before
public final void before() {
instance = HttpClientBuilder.create().build();
}
@After
public final void after() throws IllegalStateException, IOException {
ResponseUtil.closeResponse(response);
}
// tests - non-GET
@Test
public final void whenExecutingPostRequest_thenNoExceptions() throws IOException {
instance.execute(new HttpPost(SAMPLE_URL));
}
@Test
public final void whenExecutingPostRequestWithBody_thenNoExceptions() throws IOException {
final HttpPost request = new HttpPost(SAMPLE_URL);
request.setEntity(new StringEntity("in the body of the POST"));
instance.execute(request);
}
@Test
public final void givenAuth_whenExecutingPostRequestWithBody_thenNoExceptions() throws IOException, AuthenticationException {
final HttpPost request = new HttpPost(SAMPLE_URL);
request.setEntity(new StringEntity("in the body of the POST"));
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials("username", "password");
request.addHeader(new BasicScheme().authenticate(creds, request, null));
instance.execute(request);
}
}

View File

@ -1,75 +0,0 @@
package com.baeldung.httpclient.base;
import com.baeldung.httpclient.ResponseUtil;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
public class HttpClientLiveTest {
private static final String SAMPLE_URL = "http://www.github.com";
private CloseableHttpClient instance;
private CloseableHttpResponse response;
@Before
public final void before() {
instance = HttpClientBuilder.create().build();
}
@After
public final void after() throws IllegalStateException, IOException {
ResponseUtil.closeResponse(response);
}
// tests
@Test(expected = ConnectTimeoutException.class)
public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws IOException {
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5).setConnectTimeout(5).setSocketTimeout(2).build();
final HttpGet request = new HttpGet(SAMPLE_URL);
request.setConfig(requestConfig);
response = instance.execute(request);
}
// tests - configs
@Test
public final void givenHttpClientIsConfiguredWithCustomConnectionManager_whenExecutingRequest_thenNoExceptions() throws IOException {
instance = HttpClientBuilder.create().setConnectionManager(new BasicHttpClientConnectionManager()).build();
response = instance.execute(new HttpGet(SAMPLE_URL));
}
@Test
public final void givenCustomHeaderIsSet_whenSendingRequest_thenNoExceptions() throws IOException {
final HttpGet request = new HttpGet(SAMPLE_URL);
request.addHeader(HttpHeaders.ACCEPT, "application/xml");
response = instance.execute(request);
}
@Test
public final void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCorrect() throws IOException {
response = instance.execute(new HttpGet(SAMPLE_URL));
final Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
assertThat(headers, not(emptyArray()));
}
}

View File

@ -1,69 +1,71 @@
package com.baeldung.httpclient.conn; package com.baeldung.httpclient.conn;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Iterator;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.http.HeaderElement; import org.apache.hc.client5.http.ConnectionKeepAliveStrategy;
import org.apache.http.HeaderElementIterator; import org.apache.hc.client5.http.HttpRoute;
import org.apache.http.HttpClientConnection; import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.http.HttpException; import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.http.HttpHost; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.http.HttpResponse; import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.http.client.ClientProtocolException; import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.http.client.config.RequestConfig; import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.http.client.methods.HttpGet; import org.apache.hc.client5.http.io.ConnectionEndpoint;
import org.apache.http.client.protocol.HttpClientContext; import org.apache.hc.client5.http.io.LeaseRequest;
import org.apache.http.config.SocketConfig; import org.apache.hc.core5.http.HeaderElement;
import org.apache.http.conn.ConnectionKeepAliveStrategy; import org.apache.hc.core5.http.HeaderElements;
import org.apache.http.conn.ConnectionPoolTimeoutException; import org.apache.hc.core5.http.HttpHost;
import org.apache.http.conn.ConnectionRequest; import org.apache.hc.core5.http.HttpResponse;
import org.apache.http.conn.routing.HttpRoute; import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.hc.core5.http.message.MessageSupport;
import org.apache.http.impl.client.HttpClients; import org.apache.hc.core5.http.message.StatusLine;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager; import org.apache.hc.core5.http.protocol.BasicHttpContext;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.http.message.BasicHeaderElementIterator; import org.apache.hc.core5.pool.PoolStats;
import org.apache.http.protocol.HTTP; import org.apache.hc.core5.util.Args;
import org.apache.http.protocol.HttpContext; import org.apache.hc.core5.util.TimeValue;
import org.apache.http.protocol.HttpCoreContext; import org.apache.hc.core5.util.Timeout;
import org.apache.http.protocol.HttpRequestExecutor; import org.junit.jupiter.api.Test;
import org.apache.http.util.EntityUtils;
import org.junit.Ignore;
import org.junit.Test;
public class HttpClientConnectionManagementLiveTest { class HttpClientConnectionManagementLiveTest {
// Example 2.1. Getting a Connection Request for a Low Level Connection (HttpClientConnection) // Example 2.1. Getting a Connection Request for a Low Level Connection (HttpClientConnection)
@Test @Test
public final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws ConnectionPoolTimeoutException, InterruptedException, ExecutionException { final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws ExecutionException, InterruptedException, TimeoutException {
try (BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager()) { BasicHttpClientConnectionManager connMgr = new BasicHttpClientConnectionManager();
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80)); HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 443));
final ConnectionRequest connRequest = connManager.requestConnection(route, null); final LeaseRequest connRequest = connMgr.lease("some-id", route, null);
assertNotNull(connRequest.get(1000, TimeUnit.SECONDS)); assertNotNull(connRequest.get(Timeout.ZERO_MILLISECONDS));
} connMgr.close();
} }
// Example 3.1. Setting the PoolingHttpClientConnectionManager on a HttpClient // Example 3.1. Setting the PoolingHttpClientConnectionManager on a HttpClient
@Test @Test
public final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExceptions() throws ClientProtocolException, IOException { final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExceptions() throws IOException {
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager(); PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom() CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(poolingConnManager) .setConnectionManager(poolingConnManager)
.build(); .build();
client.execute(new HttpGet("https://www.baeldung.com")); client.execute(new HttpGet("https://www.baeldung.com"));
assertTrue(poolingConnManager.getTotalStats() assertTrue(poolingConnManager.getTotalStats()
.getLeased() == 1); .getLeased() == 1);
client.close();
poolingConnManager.close();
} }
// Example 3.2. Using Two HttpClients to Connect to One Target Host Each // Example 3.2. Using Two HttpClients to Connect to One Target Host Each
@Test @Test
public final void whenTwoConnectionsForTwoRequests_thenNoExceptions() throws InterruptedException { final void whenTwoConnectionsForTwoRequests_thenNoExceptions() throws InterruptedException, IOException {
HttpGet get1 = new HttpGet("https://www.baeldung.com"); HttpGet get1 = new HttpGet("https://www.baeldung.com");
HttpGet get2 = new HttpGet("https://www.google.com"); HttpGet get2 = new HttpGet("https://www.google.com");
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
@ -81,61 +83,71 @@ public class HttpClientConnectionManagementLiveTest {
thread1.join(); thread1.join();
thread2.join(); thread2.join();
assertTrue(connManager.getTotalStats() assertEquals(0, connManager.getTotalStats().getLeased());
.getLeased() == 0); client1.close();
client2.close();
connManager.close();
} }
// Example 4.1. Increasing the Number of Connections that Can be Open and Managed Beyond the default Limits // Example 4.1. Increasing the Number of Connections that Can be Open and Managed Beyond the default Limits
@Test @Test
public final void whenIncreasingConnectionPool_thenNoEceptions() { final void whenIncreasingConnectionPool_thenNoExceptions() {
try (PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager()) { PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setMaxTotal(5); connManager.setMaxTotal(5);
connManager.setDefaultMaxPerRoute(4); connManager.setDefaultMaxPerRoute(4);
HttpHost host = new HttpHost("www.baeldung.com", 80); HttpHost host = new HttpHost("www.baeldung.com", 80);
connManager.setMaxPerRoute(new HttpRoute(host), 5); connManager.setMaxPerRoute(new HttpRoute(host), 5);
} connManager.close();
} }
// Example 4.2. Using Threads to Execute Connections // Example 4.2. Using Threads to Execute Connections
@Test @Test
public final void whenExecutingSameRequestsInDifferentThreads_thenExecuteReuqest() throws InterruptedException { final void whenExecutingSameRequestsInDifferentThreads_thenExecuteRequest() throws InterruptedException, IOException {
HttpGet get = new HttpGet("http://www.baeldung.com"); HttpGet get = new HttpGet("http://www.baeldung.com");
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom() CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connManager) .setConnectionManager(connManager)
.build(); .build();
MultiHttpClientConnThread thread1 = new MultiHttpClientConnThread(client, get); MultiHttpClientConnThread thread1 = new MultiHttpClientConnThread(client, get, connManager);
MultiHttpClientConnThread thread2 = new MultiHttpClientConnThread(client, get); MultiHttpClientConnThread thread2 = new MultiHttpClientConnThread(client, get, connManager);
MultiHttpClientConnThread thread3 = new MultiHttpClientConnThread(client, get); MultiHttpClientConnThread thread3 = new MultiHttpClientConnThread(client, get, connManager);
MultiHttpClientConnThread thread4 = new MultiHttpClientConnThread(client, get, connManager);
MultiHttpClientConnThread thread5 = new MultiHttpClientConnThread(client, get, connManager);
MultiHttpClientConnThread thread6 = new MultiHttpClientConnThread(client, get, connManager);
thread1.start(); thread1.start();
thread2.start(); thread2.start();
thread3.start(); thread3.start();
thread4.start();
thread5.start();
thread6.start();
thread1.join(); thread1.join();
thread2.join(); thread2.join();
thread3.join(); thread3.join();
thread4.join();
thread5.join();
thread6.join();
client.close();
connManager.close();
} }
// Example 5.1. A Custom Keep Alive Strategy // Example 5.1. A Custom Keep Alive Strategy
@Test @Test
public final void whenCustomizingKeepAliveStrategy_thenNoExceptions() { final void whenCustomizingKeepAliveStrategy_thenNoExceptions() {
final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() { final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
@Override @Override
public long getKeepAliveDuration(final HttpResponse myResponse, final HttpContext myContext) { public TimeValue getKeepAliveDuration(HttpResponse response, HttpContext context) {
final HeaderElementIterator it = new BasicHeaderElementIterator(myResponse.headerIterator(HTTP.CONN_KEEP_ALIVE)); Args.notNull(response, "HTTP response");
while (it.hasNext()) { final Iterator<HeaderElement> it = MessageSupport.iterate(response, HeaderElements.KEEP_ALIVE);
final HeaderElement he = it.nextElement(); final HeaderElement he = it.next();
final String param = he.getName(); final String param = he.getName();
final String value = he.getValue(); final String value = he.getValue();
if ((value != null) && param.equalsIgnoreCase("timeout")) { if (value != null && param.equalsIgnoreCase("timeout")) {
return Long.parseLong(value) * 1000; try {
return TimeValue.ofSeconds(Long.parseLong(value));
} catch (final NumberFormatException ignore) {
} }
} }
final HttpHost target = (HttpHost) myContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST); return TimeValue.ofSeconds(5);
if ("localhost".equalsIgnoreCase(target.getHostName())) {
return 10 * 1000;
} else {
return 5 * 1000;
}
} }
}; };
@ -144,42 +156,38 @@ public class HttpClientConnectionManagementLiveTest {
.setKeepAliveStrategy(myStrategy) .setKeepAliveStrategy(myStrategy)
.setConnectionManager(connManager) .setConnectionManager(connManager)
.build(); .build();
connManager.close();
} }
// Example 6.1. BasicHttpClientConnectionManager Connection Reuse //Example 6.1. BasicHttpClientConnectionManager Connection Reuse
@Test @Test
public final void givenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws IOException, HttpException, InterruptedException, ExecutionException { final void givenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws InterruptedException, ExecutionException, TimeoutException, IOException, URISyntaxException {
BasicHttpClientConnectionManager basicConnManager = new BasicHttpClientConnectionManager(); BasicHttpClientConnectionManager connMgr = new BasicHttpClientConnectionManager();
HttpClientContext context = HttpClientContext.create();
// low level
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 443)); HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 443));
ConnectionRequest connRequest = basicConnManager.requestConnection(route, null); final HttpContext context = new BasicHttpContext();
HttpClientConnection conn = connRequest.get(10, TimeUnit.SECONDS);
basicConnManager.connect(conn, route, 1000, context);
basicConnManager.routeComplete(conn, route, context);
HttpRequestExecutor exeRequest = new HttpRequestExecutor(); final LeaseRequest connRequest = connMgr.lease("some-id", route, null);
context.setTargetHost((new HttpHost("www.baeldung.com", 80))); final ConnectionEndpoint endpoint = connRequest.get(Timeout.ZERO_MILLISECONDS);
HttpGet get = new HttpGet("http://www.baeldung.com"); connMgr.connect(endpoint, Timeout.ZERO_MILLISECONDS, context);
exeRequest.execute(get, conn, context);
basicConnManager.releaseConnection(conn, null, 1, TimeUnit.SECONDS); connMgr.release(endpoint, null, TimeValue.ZERO_MILLISECONDS);
// high level
CloseableHttpClient client = HttpClients.custom() CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(basicConnManager) .setConnectionManager(connMgr)
.build(); .build();
client.execute(get); HttpGet httpGet = new HttpGet("https://www.example.com");
client.execute(httpGet, context, response -> response);
client.close();
connMgr.close();
} }
// Example 6.2. PoolingHttpClientConnectionManager: Re-Using Connections with Threads // Example 6.2. PoolingHttpClientConnectionManager: Re-Using Connections with Threads
@Test @Test
public final void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse() throws InterruptedException { final void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse() throws InterruptedException, IOException {
HttpGet get = new HttpGet("http://echo.200please.com"); HttpGet get = new HttpGet("http://www.baeldung.com");
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setDefaultMaxPerRoute(5); connManager.setDefaultMaxPerRoute(6);
connManager.setMaxTotal(5); connManager.setMaxTotal(6);
CloseableHttpClient client = HttpClients.custom() CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connManager) .setConnectionManager(connManager)
.build(); .build();
@ -193,48 +201,71 @@ public class HttpClientConnectionManagementLiveTest {
for (MultiHttpClientConnThread thread : threads) { for (MultiHttpClientConnThread thread : threads) {
thread.join(1000); thread.join(1000);
} }
client.close();
connManager.close();
} }
// Example 7.1. Setting Socket Timeout to 5 Seconds // Example 7.1. Setting Socket Timeout to 5 Seconds
@Test @Test
public final void whenConfiguringTimeOut_thenNoExceptions() { final void whenConfiguringTimeOut_thenNoExceptions() throws ExecutionException, InterruptedException, TimeoutException, IOException {
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80)); final HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80));
try (PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager()) { final HttpContext context = new BasicHttpContext();
connManager.setSocketConfig(route.getTargetHost(), SocketConfig.custom() final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
.setSoTimeout(5000)
.build()); final ConnectionConfig connConfig = ConnectionConfig.custom()
assertTrue(connManager.getSocketConfig(route.getTargetHost()) .setSocketTimeout(5, TimeUnit.SECONDS)
.getSoTimeout() == 5000); .build();
}
connManager.setDefaultConnectionConfig(connConfig);
final LeaseRequest leaseRequest = connManager.lease("id1", route, null);
final ConnectionEndpoint endpoint = leaseRequest.get(Timeout.ZERO_MILLISECONDS);
connManager.connect(endpoint, null, context);
connManager.close();
} }
// Example 8.1. Setting the HttpClient to Check for Stale Connections // Example 8.1. Setting the HttpClient to Check for Stale Connections
@Test @Test
public final void whenHttpClientChecksStaleConns_thenNoExceptions() { final void whenEvictIdealConn_thenNoExceptions() throws InterruptedException, IOException {
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
HttpClients.custom() connManager.setMaxTotal(100);
.setDefaultRequestConfig(RequestConfig.custom() try (final CloseableHttpClient httpclient = HttpClients.custom()
.setStaleConnectionCheckEnabled(true)
.build())
.setConnectionManager(connManager) .setConnectionManager(connManager)
.build(); .evictExpiredConnections()
} .evictIdleConnections(TimeValue.ofSeconds(2))
.build()) {
// create an array of URIs to perform GETs on
final String[] urisToGet = { "http://hc.apache.org/", "http://hc.apache.org/httpcomponents-core-ga/"};
// Example 8.2. Using a Stale Connection Monitor Thread for (final String requestURI : urisToGet) {
@Test final HttpGet request = new HttpGet(requestURI);
public final void whenCustomizedIdleConnMonitor_thenNoExceptions() throws InterruptedException {
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); System.out.println("Executing request " + request.getMethod() + " " + request.getRequestUri());
HttpClients.custom()
.setConnectionManager(connManager) httpclient.execute(request, response -> {
.build(); System.out.println("----------------------------------------");
IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(connManager); System.out.println(request + "->" + new StatusLine(response));
staleMonitor.start(); EntityUtils.consume(response.getEntity());
staleMonitor.join(1000); return null;
});
}
final PoolStats stats1 = connManager.getTotalStats();
System.out.println("Connections kept alive: " + stats1.getAvailable());
// Sleep 10 sec and let the connection evict or do its job
Thread.sleep(4000);
final PoolStats stats2 = connManager.getTotalStats();
System.out.println("Connections kept alive: " + stats2.getAvailable());
connManager.close();
}
} }
// Example 9.1. Closing Connection and Releasing Resources // Example 9.1. Closing Connection and Releasing Resources
@Test(expected = IllegalStateException.class) @Test
public final void whenClosingConnectionsandManager_thenCloseWithNoExceptions1() throws InterruptedException, ExecutionException, IOException, HttpException { final void whenClosingConnectionsAndManager_thenCloseWithNoExceptions1() throws IOException {
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom() CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connManager) .setConnectionManager(connManager)
@ -246,16 +277,11 @@ public class HttpClientConnectionManagementLiveTest {
response.close(); response.close();
client.close(); client.close();
connManager.close(); connManager.close();
connManager.shutdown();
client.execute(get);
assertTrue(response.getEntity() == null);
} }
@Test @Test
// Example 3.2. TESTER VERSION // Example 3.2. TESTER VERSION
public final void whenTwoConnectionsForTwoRequests_thenTwoConnectionsAreLeased() throws InterruptedException { final void whenTwoConnectionsForTwoRequests_thenTwoConnectionsAreLeased() throws InterruptedException, IOException {
HttpGet get1 = new HttpGet("https://www.baeldung.com"); HttpGet get1 = new HttpGet("https://www.baeldung.com");
HttpGet get2 = new HttpGet("https://www.google.com"); HttpGet get2 = new HttpGet("https://www.google.com");
@ -273,77 +299,10 @@ public class HttpClientConnectionManagementLiveTest {
thread2.start(); thread2.start();
thread1.join(); thread1.join();
thread2.join(1000); thread2.join(1000);
assertTrue(poolingConnManager.getTotalStats() assertEquals(2, poolingConnManager.getTotalStats().getLeased());
.getLeased() == 2);
}
@Test client1.close();
// Example 4.2 Tester Version client2.close();
public final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConnLimit() throws InterruptedException { poolingConnManager.close();
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(poolingConnManager)
.build();
final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager);
final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager);
final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager);
thread1.start();
thread2.start();
thread3.start();
thread1.join(10000);
thread2.join(10000);
thread3.join(10000);
}
@Test
// 6.2 TESTER VERSION
public final void whenConnectionsNeededGreaterThanMaxTotal_thenReuseConnections() throws InterruptedException {
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
poolingConnManager.setDefaultMaxPerRoute(5);
poolingConnManager.setMaxTotal(5);
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(poolingConnManager)
.build();
final MultiHttpClientConnThread[] threads = new MultiHttpClientConnThread[10];
int countConnMade = 0;
for (int i = 0; i < threads.length; i++) {
threads[i] = new MultiHttpClientConnThread(client, new HttpGet("http://www.baeldung.com/"), poolingConnManager);
}
for (final MultiHttpClientConnThread thread : threads) {
thread.start();
}
for (final MultiHttpClientConnThread thread : threads) {
thread.join(10000);
countConnMade++;
if (countConnMade == 0) {
assertTrue(thread.getLeasedConn() == 5);
}
}
}
@Test
@Ignore("Very Long Running")
// 8.2 TESTER VERSION
public final void whenCustomizedIdleConnMonitor_thenEliminateIdleConns() throws InterruptedException {
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(poolingConnManager)
.build();
final IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(poolingConnManager);
final HttpGet get = new HttpGet("http://google.com");
final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager);
final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager);
final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager);
staleMonitor.start();
thread1.start();
thread1.join();
thread2.start();
thread2.join();
thread3.start();
assertTrue(poolingConnManager.getTotalStats()
.getAvailable() == 1);
thread3.join(32000);
assertTrue(poolingConnManager.getTotalStats()
.getAvailable() == 0);
} }
} }

View File

@ -2,12 +2,11 @@ package com.baeldung.httpclient.conn;
import java.io.IOException; import java.io.IOException;
import org.apache.http.HttpResponse; import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.http.client.ClientProtocolException; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.http.client.methods.HttpGet; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.hc.core5.http.HttpEntity;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -45,22 +44,21 @@ public class MultiHttpClientConnThread extends Thread {
try { try {
logger.debug("Thread Running: " + getName()); logger.debug("Thread Running: " + getName());
logger.debug("Thread Running: " + getName());
if (connManager != null) { if (connManager != null) {
logger.info("Before - Leased Connections = " + connManager.getTotalStats().getLeased()); logger.info("Before - Leased Connections = " + connManager.getTotalStats().getLeased());
logger.info("Before - Available Connections = " + connManager.getTotalStats().getAvailable()); logger.info("Before - Available Connections = " + connManager.getTotalStats().getAvailable());
} }
final HttpResponse response = client.execute(get); HttpEntity entity = client.execute(get).getEntity();
if (connManager != null) { if (connManager != null) {
leasedConn = connManager.getTotalStats().getLeased(); leasedConn = connManager.getTotalStats().getLeased();
logger.info("After - Leased Connections = " + connManager.getTotalStats().getLeased()); logger.info("After - Leased Connections = " + connManager.getTotalStats().getLeased());
logger.info("After - Available Connections = " + connManager.getTotalStats().getAvailable()); logger.info("After - Available Connections = " + connManager.getTotalStats().getAvailable());
} }
EntityUtils.consume(entity);
EntityUtils.consume(response.getEntity());
} catch (final IOException ex) { } catch (final IOException ex) {
logger.error("", ex); logger.error("", ex);
} }

View File

@ -2,10 +2,9 @@ package com.baeldung.httpclient.conn;
import java.io.IOException; import java.io.IOException;
import org.apache.http.client.ClientProtocolException; import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.http.client.methods.HttpGet; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

View File

@ -241,8 +241,6 @@
<httpcore.version>4.4.16</httpcore.version> <httpcore.version>4.4.16</httpcore.version>
<httpclient.version>4.5.14</httpclient.version> <httpclient.version>4.5.14</httpclient.version>
<mockserver.version>5.11.2</mockserver.version> <mockserver.version>5.11.2</mockserver.version>
<!-- maven plugins -->
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
</properties> </properties>
</project> </project>

View File

@ -0,0 +1,64 @@
package com.baeldung.tlsversion;
import javax.net.ssl.SSLSocket;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class ClientTlsVersionExamples {
public static CloseableHttpClient setViaSocketFactory() {
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
SSLContexts.createDefault(),
new String[] { "TLSv1.2", "TLSv1.3" },
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
}
public static CloseableHttpClient setTlsVersionPerConnection() {
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(SSLContexts.createDefault()) {
@Override
protected void prepareSocket(SSLSocket socket) {
String hostname = socket.getInetAddress().getHostName();
if (hostname.endsWith("internal.system.com")) {
socket.setEnabledProtocols(new String[] { "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" });
} else {
socket.setEnabledProtocols(new String[] { "TLSv1.3" });
}
}
};
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
}
// To configure the TLS versions for the client, set the https.protocols system property during runtime.
// For example: java -Dhttps.protocols=TLSv1.1,TLSv1.2,TLSv1.3 -jar webClient.jar
public static CloseableHttpClient setViaSystemProperties() {
return HttpClients.createSystem();
// Alternatively:
// return HttpClients.custom().useSystemProperties().build();
}
public static void main(String[] args) throws IOException {
// Alternatively:
// CloseableHttpClient httpClient = setTlsVersionPerConnection();
// CloseableHttpClient httpClient = setViaSystemProperties();
try (CloseableHttpClient httpClient = setViaSocketFactory();
CloseableHttpResponse response = httpClient.execute(new HttpGet("https://httpbin.org/"))) {
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
}
}
}

View File

@ -1,7 +1,7 @@
package com.baeldung.httpclient.base; package com.baeldung.client;
import java.io.IOException;
import com.baeldung.httpclient.GetRequestMockServer;
import com.baeldung.httpclient.ResponseUtil;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider; import org.apache.http.client.CredentialsProvider;
@ -12,15 +12,16 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.IOException; import com.baeldung.GetRequestMockServer;
import com.baeldung.httpclient.ResponseUtil;
/* /*
* NOTE : Need module spring-security-rest-basic-auth to be running * NOTE : Need module spring-security-rest-basic-auth to be running
*/ */
public class HttpClientSandboxLiveTest extends GetRequestMockServer { class HttpClientSandboxLiveTest extends GetRequestMockServer {
@Test @Test
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws IOException { final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws IOException {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
final AuthScope authscp = new AuthScope("localhost", 8080); final AuthScope authscp = new AuthScope("localhost", 8080);
credentialsProvider.setCredentials(authscp, new UsernamePasswordCredentials("user1", "user1Pass")); credentialsProvider.setCredentials(authscp, new UsernamePasswordCredentials("user1", "user1Pass"));

View File

@ -35,7 +35,7 @@ import org.springframework.web.client.RestTemplate;
* This test requires a localhost server over HTTPS <br> * This test requires a localhost server over HTTPS <br>
* It should only be manually run, not part of the automated build * It should only be manually run, not part of the automated build
* */ * */
public class RestClientV4LiveManualTest { class RestClientV4LiveManualTest {
final String urlOverHttps = "http://localhost:8082/httpclient-simple/api/bars/1"; final String urlOverHttps = "http://localhost:8082/httpclient-simple/api/bars/1";
@ -81,7 +81,7 @@ public class RestClientV4LiveManualTest {
} }
@Test @Test
public void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException { void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpClient httpClient = HttpClients.createDefault();
String urlOverHttps = "https://localhost:8082/httpclient-simple"; String urlOverHttps = "https://localhost:8082/httpclient-simple";
HttpGet getMethod = new HttpGet(urlOverHttps); HttpGet getMethod = new HttpGet(urlOverHttps);

View File

@ -2,8 +2,6 @@ package com.baeldung.httpclient;
import java.io.IOException; import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
public final class ClientUtil { public final class ClientUtil {

View File

@ -1,4 +1,5 @@
package com.baeldung.httpclient; package com.baeldung.httpclient;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;

View File

@ -9,11 +9,11 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
public class HttpClientCancelRequestV4LiveTest { class HttpClientCancelRequestV4LiveTest {
private static final String SAMPLE_URL = "http://www.github.com"; private static final String SAMPLE_URL = "http://www.github.com";
@ -21,18 +21,18 @@ public class HttpClientCancelRequestV4LiveTest {
private CloseableHttpResponse response; private CloseableHttpResponse response;
@Before @BeforeEach
public final void before() { public final void before() {
instance = HttpClientBuilder.create().build(); instance = HttpClientBuilder.create().build();
} }
@After @AfterEach
public final void after() throws IllegalStateException, IOException { public final void after() throws IllegalStateException, IOException {
ResponseUtil.closeResponse(response); ResponseUtil.closeResponse(response);
} }
@Test @Test
public final void whenRequestIsCanceled_thenCorrect() throws IOException { final void whenRequestIsCanceled_thenCorrect() throws IOException {
instance = HttpClients.custom().build(); instance = HttpClients.custom().build();
final HttpGet request = new HttpGet(SAMPLE_URL); final HttpGet request = new HttpGet(SAMPLE_URL);
response = instance.execute(request); response = instance.execute(request);

View File

@ -1,5 +1,9 @@
package com.baeldung.httpclient; package com.baeldung.httpclient;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -181,4 +185,20 @@ class HttpClientCookBookV4LiveTest {
} }
} }
@Test
final void whenExecutingPostRequestWithBody_thenNoExceptions() throws IOException {
final HttpPost request = new HttpPost(SAMPLE_POST_URL);
request.setEntity(new StringEntity("in the body of the POST"));
client.execute(request);
}
@Test
final void givenAuth_whenExecutingPostRequestWithBody_thenNoExceptions() throws IOException, AuthenticationException {
final HttpPost request = new HttpPost(SAMPLE_POST_URL);
request.setEntity(new StringEntity("in the body of the POST"));
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials("username", "password");
request.addHeader(new BasicScheme().authenticate(creds, request, null));
client.execute(request);
}
} }

View File

@ -1,12 +1,12 @@
package com.baeldung.httpclient; package com.baeldung.httpclient;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import java.io.IOException; import java.io.IOException;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;

View File

@ -92,6 +92,26 @@ class HttpClientTimeoutV4LiveTest extends GetRequestMockServer {
} }
@Test
final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() {
final RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(5)
.setConnectTimeout(5)
.setSocketTimeout(2)
.build();
final CloseableHttpClient client = HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig)
.build();
final HttpGet request = new HttpGet("http://www.github.com");
assertThrows(ConnectTimeoutException.class, () -> {
response = client.execute(request);
});
}
@Test @Test
void whenSecuredRestApiIsConsumed_then200OK() throws IOException { void whenSecuredRestApiIsConsumed_then200OK() throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build(); CloseableHttpClient httpClient = HttpClientBuilder.create().build();

View File

@ -0,0 +1,173 @@
package com.baeldung.httpclient.advancedconfig;
import com.github.tomakehurst.wiremock.WireMockServer;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static org.junit.jupiter.api.Assertions.assertEquals;
class HttpClientAdvancedConfigurationIntegrationTest {
public WireMockServer serviceMock;
public WireMockServer proxyMock;
@BeforeEach
public void before () {
serviceMock = new WireMockServer(8089);
serviceMock.start();
proxyMock = new WireMockServer(8090);
proxyMock.start();
}
@AfterEach
public void after () {
serviceMock.stop();
proxyMock.stop();
}
@Test
void givenClientWithCustomUserAgentHeader_whenExecuteRequest_shouldReturn200() throws IOException {
//given
String userAgent = "BaeldungAgent/1.0";
serviceMock.stubFor(get(urlEqualTo("/detail"))
.withHeader("User-Agent", equalTo(userAgent))
.willReturn(aResponse()
.withStatus(200)));
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://localhost:8089/detail");
httpGet.setHeader(HttpHeaders.USER_AGENT, userAgent);
//when
HttpResponse response = httpClient.execute(httpGet);
//then
assertEquals(200, response.getStatusLine().getStatusCode());
}
@Test
void givenClientThatSendDataInBody_whenSendXmlInBody_shouldReturn200() throws IOException {
//given
String xmlBody = "<xml><id>1</id></xml>";
serviceMock.stubFor(post(urlEqualTo("/person"))
.withHeader("Content-Type", equalTo("application/xml"))
.withRequestBody(equalTo(xmlBody))
.willReturn(aResponse()
.withStatus(200)));
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:8089/person");
httpPost.setHeader("Content-Type", "application/xml");
StringEntity xmlEntity = new StringEntity(xmlBody);
httpPost.setEntity(xmlEntity);
//when
HttpResponse response = httpClient.execute(httpPost);
//then
assertEquals(200, response.getStatusLine().getStatusCode());
}
@Test
void givenServerThatIsBehindProxy_whenClientIsConfiguredToSendRequestViaProxy_shouldReturn200() throws IOException {
//given
proxyMock.stubFor(get(urlMatching(".*"))
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
serviceMock.stubFor(get(urlEqualTo("/private"))
.willReturn(aResponse().withStatus(200)));
HttpHost proxy = new HttpHost("localhost", 8090);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
HttpClient httpclient = HttpClients.custom()
.setRoutePlanner(routePlanner)
.build();
//when
final HttpGet httpGet = new HttpGet("http://localhost:8089/private");
HttpResponse response = httpclient.execute(httpGet);
//then
assertEquals(200, response.getStatusLine().getStatusCode());
proxyMock.verify(getRequestedFor(urlEqualTo("/private")));
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
}
@Test
void givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly() throws IOException {
//given
proxyMock.stubFor(get(urlMatching("/private"))
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
serviceMock.stubFor(get(urlEqualTo("/private"))
.willReturn(aResponse().withStatus(200)));
HttpHost proxy = new HttpHost("localhost", 8090);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
// Client credentials
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(proxy),
new UsernamePasswordCredentials("username_admin", "secret_password"));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(proxy, basicAuth);
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProvider);
context.setAuthCache(authCache);
HttpClient httpclient = HttpClients.custom()
.setRoutePlanner(routePlanner)
.setDefaultCredentialsProvider(credentialsProvider)
.build();
//when
final HttpGet httpGet = new HttpGet("http://localhost:8089/private");
HttpResponse response = httpclient.execute(httpGet, context);
//then
assertEquals(200, response.getStatusLine().getStatusCode());
proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic")));
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
}
}

View File

@ -12,34 +12,35 @@ import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
public class HttpClientExpandUrlLiveTest {
class HttpClientExpandUrlLiveTest {
private CloseableHttpClient client; private CloseableHttpClient client;
@Before @BeforeEach
public final void before() { public final void beforeEach() {
client = HttpClientBuilder.create().disableRedirectHandling().build(); client = HttpClientBuilder.create().disableRedirectHandling().build();
} }
@Test @Test
public final void givenShortenedOnce_whenUrlIsExpanded_thenCorrectResult() throws IOException { final void givenShortenedOnce_whenUrlIsExpanded_thenCorrectResult() throws IOException {
final String expectedResult = "https://www.baeldung.com/rest-versioning"; final String expectedResult = "https://www.baeldung.com/rest-versioning";
final String actualResult = expandSingleLevel("http://bit.ly/3LScTri"); final String actualResult = expandSingleLevel("http://bit.ly/3LScTri");
assertThat(actualResult, equalTo(expectedResult)); assertThat(actualResult, equalTo(expectedResult));
} }
@Test @Test
public final void givenShortenedMultiple_whenUrlIsExpanded_thenCorrectResult() throws IOException { final void givenShortenedMultiple_whenUrlIsExpanded_thenCorrectResult() throws IOException {
final String expectedResult = "https://www.baeldung.com/rest-versioning"; final String expectedResult = "https://www.baeldung.com/rest-versioning";
final String actualResult = expand("http://t.co/e4rDDbnzmk"); final String actualResult = expand("http://t.co/e4rDDbnzmk");
assertThat(actualResult, equalTo(expectedResult)); assertThat(actualResult, equalTo(expectedResult));

View File

@ -17,7 +17,6 @@ import com.baeldung.GetRequestMockServer;
class ApacheHttpClientUnitTest extends GetRequestMockServer { class ApacheHttpClientUnitTest extends GetRequestMockServer {
@Test @Test
void givenDeveloperUsedCloseableHttpResponse_whenExecutingGetRequest_thenStatusIsOk() throws IOException { void givenDeveloperUsedCloseableHttpResponse_whenExecutingGetRequest_thenStatusIsOk() throws IOException {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

View File

@ -0,0 +1,352 @@
package com.baeldung.httpclient.httpclient.conn;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpClientConnection;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.conn.ConnectionRequest;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpCoreContext;
import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
class HttpClientConnectionManagementLiveTest {
// Example 2.1. Getting a Connection Request for a Low Level Connection (HttpClientConnection)
@Test
final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws ConnectionPoolTimeoutException, InterruptedException, ExecutionException {
try (BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager()) {
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80));
final ConnectionRequest connRequest = connManager.requestConnection(route, null);
assertNotNull(connRequest.get(1000, TimeUnit.SECONDS));
}
}
// Example 3.1. Setting the PoolingHttpClientConnectionManager on a HttpClient
@Test
final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExceptions() throws ClientProtocolException, IOException {
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(poolingConnManager)
.build();
client.execute(new HttpGet("https://www.baeldung.com"));
assertEquals(1, poolingConnManager.getTotalStats()
.getLeased());
}
// Example 3.2. Using Two HttpClients to Connect to One Target Host Each
@Test
final void whenTwoConnectionsForTwoRequests_thenNoExceptions() throws InterruptedException {
HttpGet get1 = new HttpGet("https://www.baeldung.com");
HttpGet get2 = new HttpGet("https://www.google.com");
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient client1 = HttpClients.custom()
.setConnectionManager(connManager)
.build();
CloseableHttpClient client2 = HttpClients.custom()
.setConnectionManager(connManager)
.build();
MultiHttpClientConnThread thread1 = new MultiHttpClientConnThread(client1, get1);
MultiHttpClientConnThread thread2 = new MultiHttpClientConnThread(client2, get2);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
assertEquals(0, connManager.getTotalStats()
.getLeased());
}
// Example 4.1. Increasing the Number of Connections that Can be Open and Managed Beyond the default Limits
@Test
final void whenIncreasingConnectionPool_thenNoEceptions() {
try (PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager()) {
connManager.setMaxTotal(5);
connManager.setDefaultMaxPerRoute(4);
HttpHost host = new HttpHost("www.baeldung.com", 80);
connManager.setMaxPerRoute(new HttpRoute(host), 5);
}
}
// Example 4.2. Using Threads to Execute Connections
@Test
final void whenExecutingSameRequestsInDifferentThreads_thenExecuteReuqest() throws InterruptedException {
HttpGet get = new HttpGet("http://www.baeldung.com");
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connManager)
.build();
MultiHttpClientConnThread thread1 = new MultiHttpClientConnThread(client, get);
MultiHttpClientConnThread thread2 = new MultiHttpClientConnThread(client, get);
MultiHttpClientConnThread thread3 = new MultiHttpClientConnThread(client, get);
thread1.start();
thread2.start();
thread3.start();
thread1.join();
thread2.join();
thread3.join();
}
// Example 5.1. A Custom Keep Alive Strategy
@Test
final void whenCustomizingKeepAliveStrategy_thenNoExceptions() {
final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(final HttpResponse myResponse, final HttpContext myContext) {
final HeaderElementIterator it = new BasicHeaderElementIterator(myResponse.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
final HeaderElement he = it.nextElement();
final String param = he.getName();
final String value = he.getValue();
if ((value != null) && param.equalsIgnoreCase("timeout")) {
return Long.parseLong(value) * 1000;
}
}
final HttpHost target = (HttpHost) myContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
if ("localhost".equalsIgnoreCase(target.getHostName())) {
return 10 * 1000;
} else {
return 5 * 1000;
}
}
};
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
HttpClients.custom()
.setKeepAliveStrategy(myStrategy)
.setConnectionManager(connManager)
.build();
}
// Example 6.1. BasicHttpClientConnectionManager Connection Reuse
@Test
final void givenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws IOException, HttpException, InterruptedException, ExecutionException {
BasicHttpClientConnectionManager basicConnManager = new BasicHttpClientConnectionManager();
HttpClientContext context = HttpClientContext.create();
// low level
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 443));
ConnectionRequest connRequest = basicConnManager.requestConnection(route, null);
HttpClientConnection conn = connRequest.get(10, TimeUnit.SECONDS);
basicConnManager.connect(conn, route, 1000, context);
basicConnManager.routeComplete(conn, route, context);
HttpRequestExecutor exeRequest = new HttpRequestExecutor();
context.setTargetHost((new HttpHost("www.baeldung.com", 80)));
HttpGet get = new HttpGet("http://www.baeldung.com");
exeRequest.execute(get, conn, context);
basicConnManager.releaseConnection(conn, null, 1, TimeUnit.SECONDS);
// high level
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(basicConnManager)
.build();
client.execute(get);
}
// Example 6.2. PoolingHttpClientConnectionManager: Re-Using Connections with Threads
@Test
final void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse() throws InterruptedException {
HttpGet get = new HttpGet("http://echo.200please.com");
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setDefaultMaxPerRoute(5);
connManager.setMaxTotal(5);
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connManager)
.build();
MultiHttpClientConnThread[] threads = new MultiHttpClientConnThread[10];
for (int i = 0; i < threads.length; i++) {
threads[i] = new MultiHttpClientConnThread(client, get, connManager);
}
for (MultiHttpClientConnThread thread : threads) {
thread.start();
}
for (MultiHttpClientConnThread thread : threads) {
thread.join(1000);
}
}
// Example 7.1. Setting Socket Timeout to 5 Seconds
@Test
final void whenConfiguringTimeOut_thenNoExceptions() {
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80));
try (PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager()) {
connManager.setSocketConfig(route.getTargetHost(), SocketConfig.custom()
.setSoTimeout(5000)
.build());
assertEquals(5000, connManager.getSocketConfig(route.getTargetHost())
.getSoTimeout());
}
}
// Example 8.1. Setting the HttpClient to Check for Stale Connections
@Test
final void whenHttpClientChecksStaleConns_thenNoExceptions() {
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
HttpClients.custom()
.setDefaultRequestConfig(RequestConfig.custom()
.setStaleConnectionCheckEnabled(true)
.build())
.setConnectionManager(connManager)
.build();
}
// Example 8.2. Using a Stale Connection Monitor Thread
@Test
final void whenCustomizedIdleConnMonitor_thenNoExceptions() throws InterruptedException {
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
HttpClients.custom()
.setConnectionManager(connManager)
.build();
IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(connManager);
staleMonitor.start();
staleMonitor.join(1000);
}
// Example 9.1. Closing Connection and Releasing Resources
@Test
final void whenClosingConnectionsAndManager_thenCloseWithNoExceptions1() throws IOException {
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connManager)
.build();
final HttpGet get = new HttpGet("http://google.com");
CloseableHttpResponse response = client.execute(get);
EntityUtils.consume(response.getEntity());
response.close();
client.close();
connManager.close();
connManager.shutdown();
assertThrows(IllegalStateException.class, () -> {
client.execute(get);
});
}
@Test
// Example 3.2. TESTER VERSION
final void whenTwoConnectionsForTwoRequests_thenTwoConnectionsAreLeased() throws InterruptedException {
HttpGet get1 = new HttpGet("https://www.baeldung.com");
HttpGet get2 = new HttpGet("https://www.google.com");
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
final CloseableHttpClient client1 = HttpClients.custom()
.setConnectionManager(poolingConnManager)
.build();
final CloseableHttpClient client2 = HttpClients.custom()
.setConnectionManager(poolingConnManager)
.build();
final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client1, get1, poolingConnManager);
final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client2, get2, poolingConnManager);
thread1.start();
thread2.start();
thread1.join();
thread2.join(1000);
assertEquals(2, poolingConnManager.getTotalStats()
.getLeased());
}
@Test
// Example 4.2 Tester Version
final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConnLimit() throws InterruptedException {
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(poolingConnManager)
.build();
final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager);
final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager);
final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager);
thread1.start();
thread2.start();
thread3.start();
thread1.join(10000);
thread2.join(10000);
thread3.join(10000);
}
@Test
// 6.2 TESTER VERSION
final void whenConnectionsNeededGreaterThanMaxTotal_thenReuseConnections() throws InterruptedException {
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
poolingConnManager.setDefaultMaxPerRoute(5);
poolingConnManager.setMaxTotal(5);
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(poolingConnManager)
.build();
final MultiHttpClientConnThread[] threads = new MultiHttpClientConnThread[10];
int countConnMade = 0;
for (int i = 0; i < threads.length; i++) {
threads[i] = new MultiHttpClientConnThread(client, new HttpGet("http://www.baeldung.com/"), poolingConnManager);
}
for (final MultiHttpClientConnThread thread : threads) {
thread.start();
}
for (final MultiHttpClientConnThread thread : threads) {
thread.join(10000);
countConnMade++;
if (countConnMade == 0) {
assertEquals(5, thread.getLeasedConn());
}
}
}
@Test
@Disabled("Very Long Running")
// 8.2 TESTER VERSION
final void whenCustomizedIdleConnMonitor_thenEliminateIdleConns() throws InterruptedException {
PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(poolingConnManager)
.build();
final IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(poolingConnManager);
final HttpGet get = new HttpGet("http://google.com");
final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager);
final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager);
final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager);
staleMonitor.start();
thread1.start();
thread1.join();
thread2.start();
thread2.join();
thread3.start();
assertEquals(1, poolingConnManager.getTotalStats()
.getAvailable());
thread3.join(32000);
assertEquals(0, poolingConnManager.getTotalStats()
.getAvailable());
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.httpclient.conn; package com.baeldung.httpclient.httpclient.conn;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;

View File

@ -0,0 +1,68 @@
package com.baeldung.httpclient.httpclient.conn;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MultiHttpClientConnThread extends Thread {
private final Logger logger = LoggerFactory.getLogger(getClass());
private final CloseableHttpClient client;
private final HttpGet get;
private PoolingHttpClientConnectionManager connManager;
private int leasedConn;
MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get, final PoolingHttpClientConnectionManager connManager) {
this.client = client;
this.get = get;
this.connManager = connManager;
leasedConn = 0;
}
MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get) {
this.client = client;
this.get = get;
}
// API
final int getLeasedConn() {
return leasedConn;
}
//
@Override
public final void run() {
try {
logger.debug("Thread Running: " + getName());
logger.debug("Thread Running: " + getName());
if (connManager != null) {
logger.info("Before - Leased Connections = " + connManager.getTotalStats().getLeased());
logger.info("Before - Available Connections = " + connManager.getTotalStats().getAvailable());
}
final HttpResponse response = client.execute(get);
if (connManager != null) {
leasedConn = connManager.getTotalStats().getLeased();
logger.info("After - Leased Connections = " + connManager.getTotalStats().getLeased());
logger.info("After - Available Connections = " + connManager.getTotalStats().getAvailable());
}
EntityUtils.consume(response.getEntity());
} catch (final IOException ex) {
logger.error("", ex);
}
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.httpclient.httpclient.conn;
import java.io.IOException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
public class TesterVersion_MultiHttpClientConnThread extends Thread {
private final Logger logger = LoggerFactory.getLogger(getClass());
private final CloseableHttpClient client;
private final HttpGet get;
private PoolingHttpClientConnectionManager connManager;
TesterVersion_MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get, final PoolingHttpClientConnectionManager connManager) {
this.client = client;
this.get = get;
this.connManager = Preconditions.checkNotNull(connManager);
}
//
@Override
public final void run() {
try {
logger.debug("Thread Running: " + getName());
logger.info("Before - Leased Connections = " + connManager.getTotalStats().getLeased());
logger.info("Before - Available Connections = " + connManager.getTotalStats().getAvailable());
client.execute(get);
logger.info("After - Leased Connections = " + connManager.getTotalStats().getLeased());
logger.info("After - Available Connections = " + connManager.getTotalStats().getAvailable());
} catch (final IOException ex) {
logger.error("", ex);
}
}
}

View File

@ -6,18 +6,19 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
public class ApacheHttpClientUnitTest { class ApacheHttpClientUnitTest {
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
public static final String DUMMY_URL = "https://postman-echo.com/get"; public static final String DUMMY_URL = "https://postman-echo.com/get";
@Test @Test
public void whenUseApacheHttpClient_thenCorrect() throws IOException { void whenUseApacheHttpClient_thenCorrect() throws IOException {
HttpGet request = new HttpGet(DUMMY_URL); HttpGet request = new HttpGet(DUMMY_URL);
try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(request)) { try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(request)) {

View File

@ -24,7 +24,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class ApacheHttpClientRetryLiveTest { class ApacheHttpClientRetryLiveTest {
private Integer requestCounter; private Integer requestCounter;
private CloseableHttpClient httpClient; private CloseableHttpClient httpClient;
@ -93,14 +93,14 @@ public class ApacheHttpClientRetryLiveTest {
} }
@Test @Test
public void givenDefaultConfiguration_whenReceivedIOException_thenRetriesPerformed() { void givenDefaultConfiguration_whenReceivedIOException_thenRetriesPerformed() {
createFailingHttpClient(); createFailingHttpClient();
assertThrows(IOException.class, () -> httpClient.execute(new HttpGet("https://httpstat.us/200"))); assertThrows(IOException.class, () -> httpClient.execute(new HttpGet("https://httpstat.us/200")));
assertThat(requestCounter).isEqualTo(4); assertThat(requestCounter).isEqualTo(4);
} }
@Test @Test
public void givenDefaultConfiguration_whenDomainNameNotResolved_thenNoRetryApplied() { void givenDefaultConfiguration_whenDomainNameNotResolved_thenNoRetryApplied() {
createDefaultApacheHttpClient(); createDefaultApacheHttpClient();
HttpGet request = new HttpGet(URI.create("http://domain.that.does.not.exist:80/api/v1")); HttpGet request = new HttpGet(URI.create("http://domain.that.does.not.exist:80/api/v1"));
@ -109,7 +109,7 @@ public class ApacheHttpClientRetryLiveTest {
} }
@Test @Test
public void givenDefaultConfiguration_whenGotInternalServerError_thenNoRetryLogicApplied() throws IOException { void givenDefaultConfiguration_whenGotInternalServerError_thenNoRetryLogicApplied() throws IOException {
createDefaultApacheHttpClient(); createDefaultApacheHttpClient();
HttpGet request = new HttpGet(URI.create("https://httpstat.us/500")); HttpGet request = new HttpGet(URI.create("https://httpstat.us/500"));
@ -120,7 +120,7 @@ public class ApacheHttpClientRetryLiveTest {
} }
@Test @Test
public void givenDefaultConfiguration_whenHttpPatchRequest_thenRetryIsNotApplied() { void givenDefaultConfiguration_whenHttpPatchRequest_thenRetryIsNotApplied() {
createFailingHttpClient(); createFailingHttpClient();
HttpPatch request = new HttpPatch(URI.create("https://httpstat.us/500")); HttpPatch request = new HttpPatch(URI.create("https://httpstat.us/500"));
@ -129,7 +129,7 @@ public class ApacheHttpClientRetryLiveTest {
} }
@Test @Test
public void givenDefaultConfiguration_whenHttpPutRequest_thenRetryIsNotApplied() { void givenDefaultConfiguration_whenHttpPutRequest_thenRetryIsNotApplied() {
createFailingHttpClient(); createFailingHttpClient();
HttpPut request = new HttpPut(URI.create("https://httpstat.us/500")); HttpPut request = new HttpPut(URI.create("https://httpstat.us/500"));
@ -138,7 +138,7 @@ public class ApacheHttpClientRetryLiveTest {
} }
@Test @Test
public void givenConfiguredRetryHandler_whenHttpPostRequest_thenRetriesPerformed() { void givenConfiguredRetryHandler_whenHttpPostRequest_thenRetriesPerformed() {
createHttpClientWithRetryHandler(); createHttpClientWithRetryHandler();
HttpPost request = new HttpPost(URI.create("https://httpstat.us/200")); HttpPost request = new HttpPost(URI.create("https://httpstat.us/200"));
@ -148,7 +148,7 @@ public class ApacheHttpClientRetryLiveTest {
} }
@Test @Test
public void givenCustomRetryHandler_whenUnknownHostException_thenRetryAnyway() { void givenCustomRetryHandler_whenUnknownHostException_thenRetryAnyway() {
createHttpClientWithCustomRetryHandler(); createHttpClientWithCustomRetryHandler();
HttpGet request = new HttpGet(URI.create("https://domain.that.does.not.exist/200")); HttpGet request = new HttpGet(URI.create("https://domain.that.does.not.exist/200"));
@ -158,7 +158,7 @@ public class ApacheHttpClientRetryLiveTest {
} }
@Test @Test
public void givenDisabledRetries_whenExecutedHttpRequestEndUpWithIOException_thenRetryIsNotApplied() { void givenDisabledRetries_whenExecutedHttpRequestEndUpWithIOException_thenRetryIsNotApplied() {
createHttpClientWithRetriesDisabled(); createHttpClientWithRetriesDisabled();
HttpGet request = new HttpGet(URI.create("https://httpstat.us/200")); HttpGet request = new HttpGet(URI.create("https://httpstat.us/200"));

View File

@ -12,3 +12,5 @@ You can build the project from the command line using: *mvn clean install*, or i
- [Is a Key Required as Part of Sending Messages to Kafka?](https://www.baeldung.com/java-kafka-message-key) - [Is a Key Required as Part of Sending Messages to Kafka?](https://www.baeldung.com/java-kafka-message-key)
- [Read Data From the Beginning Using Kafka Consumer API](https://www.baeldung.com/java-kafka-consumer-api-read) - [Read Data From the Beginning Using Kafka Consumer API](https://www.baeldung.com/java-kafka-consumer-api-read)
- [Get Partition Count for a Topic in Kafka](https://www.baeldung.com/java-kafka-partition-count-topic) - [Get Partition Count for a Topic in Kafka](https://www.baeldung.com/java-kafka-partition-count-topic)
- [bootstrap-server in Kafka Configuration](https://www.baeldung.com/java-kafka-bootstrap-server)
- [Introduction to Apache Kafka](https://www.baeldung.com/apache-kafka)

View File

@ -0,0 +1,39 @@
package com.baeldung.kafka.consumer;
import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.serialization.LongDeserializer;
import org.apache.kafka.common.serialization.StringDeserializer;
import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;
public class SimpleConsumerWithBootStrapServers {
public static void main(String[] args) {
try(final Consumer<Long, String> consumer = createConsumer()) {
ConsumerRecords<Long, String> records = consumer.poll(Duration.ofMinutes(1));
for(ConsumerRecord<Long, String> record : records) {
System.out.println(record.value());
}
}
}
private static Consumer<Long, String> createConsumer() {
final Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
"localhost:9092,another-host.com:29092");
props.put(ConsumerConfig.GROUP_ID_CONFIG,
"MySampleConsumer");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
LongDeserializer.class.getName());
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
StringDeserializer.class.getName());
// Create the consumer using props.
final Consumer<Long, String> consumer = new KafkaConsumer<Long, String>(props);
// Subscribe to the topic.
consumer.subscribe(Arrays.asList("samples"));
return consumer;
}
}

View File

@ -0,0 +1,103 @@
package com.baeldung.kafka.multipletopics;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
// This live test needs a Docker Daemon running so that a kafka container can be created
@Testcontainers
public class MultipleTopicsLiveTest {
private final Logger log = LoggerFactory.getLogger(MultipleTopicsLiveTest.class);
private static final String CARD_PAYMENTS_TOPIC = "card-payments";
private static final String BANK_TRANSFERS_TOPIC = "bank-transfers";
private static KafkaProducer<String, String> producer;
private static KafkaConsumer<String, String> consumer;
@Container
private static final KafkaContainer KAFKA_CONTAINER = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:latest"));
@BeforeAll
static void setup() {
KAFKA_CONTAINER.addExposedPort(9092);
producer = new KafkaProducer<>(getProducerProperties());
consumer = new KafkaConsumer<>(getConsumerProperties());
}
@AfterAll
static void destroy() {
KAFKA_CONTAINER.stop();
}
private static Properties getProducerProperties() {
Properties producerProperties = new Properties();
producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, KAFKA_CONTAINER.getBootstrapServers());
producerProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
producerProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
return producerProperties;
}
private static Properties getConsumerProperties() {
Properties consumerProperties = new Properties();
consumerProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, KAFKA_CONTAINER.getBootstrapServers());
consumerProperties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
consumerProperties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
consumerProperties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
consumerProperties.put(ConsumerConfig.GROUP_ID_CONFIG, "payments");
return consumerProperties;
}
@Test
void whenSendingMessagesOnTwoTopics_thenConsumerReceivesMessages() throws Exception {
publishMessages();
consumer.subscribe(Arrays.asList(CARD_PAYMENTS_TOPIC, BANK_TRANSFERS_TOPIC));
int eventsProcessed = 0;
for (ConsumerRecord<String, String> record : consumer.poll(Duration.ofSeconds(10))) {
log.info("Event on topic={}, payload={}", record.topic(), record.value());
eventsProcessed++;
}
assertThat(eventsProcessed).isEqualTo(2);
}
private void publishMessages() throws ExecutionException, InterruptedException {
ProducerRecord<String, String> cardPayment = new ProducerRecord<>(CARD_PAYMENTS_TOPIC, createCardPayment());
producer.send(cardPayment).get();
ProducerRecord<String, String> bankTransfer = new ProducerRecord<>(BANK_TRANSFERS_TOPIC, createBankTransfer());
producer.send(bankTransfer).get();
}
private String createCardPayment() {
return "{\"paymentReference\":\"A184028KM0013790\", \"type\":\"card\", \"amount\":\"275\", \"currency\":\"GBP\"}";
}
private String createBankTransfer() {
return "{\"paymentReference\":\"19ae2-18mk73-009\", \"type\":\"bank\", \"amount\":\"150\", \"currency\":\"EUR\"}";
}
}

View File

@ -0,0 +1,2 @@
## Relevant Articles
- [Understanding XSLT Processing in Java](https://www.baeldung.com/java-extensible-stylesheet-language-transformations)

View File

@ -0,0 +1,28 @@
<?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>
<artifactId>apache-libraries-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>apache-libraries-2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${javax.validation.validation-api.version}</version>
</dependency>
</dependencies>
<properties>
<javax.validation.validation-api.version>2.0.1.Final</javax.validation.validation-api.version>
</properties>
</project>

View File

@ -0,0 +1,18 @@
package com.baeldung.xslt;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
public class XSLTProcessor {
public static void transformXMLUsingXSLT(String inputXMLPath, String xsltPath, String outputHTMLPath) throws TransformerException {
Source xmlSource = new StreamSource(new File(inputXMLPath));
Source xsltSource = new StreamSource(new File(xsltPath));
Result output = new StreamResult(new File(outputHTMLPath));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xsltSource);
transformer.transform(xmlSource, output);
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.xsltProcessing;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
public class XSLTProcessorWithParametersAndOption {
public static void transformXMLWithParametersAndOption(
String inputXMLPath,
String xsltPath,
String outputHTMLPath,
String companyName,
boolean enableIndentation
) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Source xsltSource = new StreamSource(new File(xsltPath));
Transformer transformer = transformerFactory.newTransformer(xsltSource);
transformer.setParameter("companyName", companyName);
if (enableIndentation) {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
}
Source xmlSource = new StreamSource(new File(inputXMLPath));
Result outputResult = new StreamResult(new File(outputHTMLPath));
transformer.transform(xmlSource, outputResult);
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.xsltProcessing;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
public class XSLTProcessorWithTemplate {
public static void transformXMLUsingTemplate(String inputXMLPath, String xsltPath, String outputHTMLPath) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Source xsltSource = new StreamSource(new File(xsltPath));
Templates templates = transformerFactory.newTemplates(xsltSource);
Transformer transformer = templates.newTransformer();
Source xmlSource = new StreamSource(new File(inputXMLPath));
Result outputResult = new StreamResult(new File(outputHTMLPath));
transformer.transform(xmlSource, outputResult);
}
}

View File

@ -0,0 +1,47 @@
{
"type":"record",
"name":"AvroHttpRequest",
"namespace":"com.baeldung.avro.model",
"fields":[
{
"name":"requestTime",
"type":"long"
},
{
"name":"clientIdentifier",
"type":{
"type":"record",
"name":"ClientIdentifier",
"fields":[
{
"name":"hostName",
"type":"string"
},
{
"name":"ipAddress",
"type":"string"
}
]
}
},
{
"name":"employeeNames",
"type":{
"type":"array",
"items":"string"
},
"default":null
},
{
"name":"active",
"type":{
"type":"enum",
"name":"Active",
"symbols":[
"YES",
"NO"
]
}
}
]
}

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.apache.meecrowave" level="warn">
<AppenderRef ref="Console"/>
</Logger>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

View File

@ -0,0 +1,29 @@
package com.baeldung.xslt;
import org.junit.jupiter.api.Test;
import javax.xml.transform.TransformerException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class XSLTProcessorUnitTest {
@Test
public void givenValidInputAndStylesheet_whenTransformingXML_thenOutputHTMLCreated() throws TransformerException, IOException {
// Given
String inputXMLPath = "src/test/resources/input.xml";
String xsltPath = "src/test/resources/stylesheet.xslt";
String outputHTMLPath = "src/test/resources/output.html";
XSLTProcessor.transformXMLUsingXSLT(inputXMLPath, xsltPath, outputHTMLPath);
Path outputFile = Paths.get(outputHTMLPath);
assertTrue(Files.exists(outputFile));
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
<person gender="male">
<name>John Doe</name>
<age>30</age>
</person>
<person gender="female">
<name>Jane Smith</name>
<age>25</age>
</person>
</root>

View File

@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<p>Male person: John Doe, Age: 30</p>
<p>Female person: Jane Smith, Age: 25</p>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="person[@gender='male']">
<xsl:element name="p">
<xsl:text>Male person: </xsl:text>
<xsl:value-of select="name"/>
<xsl:text>, Age: </xsl:text>
<xsl:value-of select="age"/>
</xsl:element>
</xsl:template>
<xsl:template match="person[@gender='female']">
<xsl:element name="p">
<xsl:text>Female person: </xsl:text>
<xsl:value-of select="name"/>
<xsl:text>, Age: </xsl:text>
<xsl:value-of select="age"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

View File

@ -13,4 +13,5 @@ This module contains articles about Apache POI.
- [Setting Formulas in Excel with Apache POI](https://www.baeldung.com/java-apache-poi-set-formulas) - [Setting Formulas in Excel with Apache POI](https://www.baeldung.com/java-apache-poi-set-formulas)
- [Set the Date Format Using Apache POI](https://www.baeldung.com/java-apache-poi-date-format) - [Set the Date Format Using Apache POI](https://www.baeldung.com/java-apache-poi-date-format)
- [Replacing Variables in a Document Template with Java](https://www.baeldung.com/java-replace-pattern-word-document-doc-docx) - [Replacing Variables in a Document Template with Java](https://www.baeldung.com/java-replace-pattern-word-document-doc-docx)
- [Lock Header Rows With Apache POI](https://www.baeldung.com/java-apache-poi-lock-header-rows)
- More articles: [[<-- prev]](../apache-poi) - More articles: [[<-- prev]](../apache-poi)

3
apache-poi-3/README.md Normal file
View File

@ -0,0 +1,3 @@
## Relevant Articles
- [How To Convert Excel Data Into List Of Java Objects](https://www.baeldung.com/java-convert-excel-data-into-list)
- [Expand Columns with Apache POI](https://www.baeldung.com/java-apache-poi-expand-columns)

97
apache-poi-3/pom.xml Normal file
View File

@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>apache-poi-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>apache-poi-3</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>${poi.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.ozlerhakan/poiji -->
<dependency>
<groupId>com.github.ozlerhakan</groupId>
<artifactId>poiji</artifactId>
<version>${poiji.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi/5.2.3 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas/4.1.2 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans/5.1.1 -->
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>5.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.dhatim/fastexcel/0.15.7 -->
<dependency>
<groupId>org.dhatim</groupId>
<artifactId>fastexcel</artifactId>
<version>${fastexcel.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.dhatim/fastexcel-reader/0.15.7 -->
<dependency>
<groupId>org.dhatim</groupId>
<artifactId>fastexcel-reader</artifactId>
<version>${fastexcel.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl/2.6.12 -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>${jxl.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
</dependency>
</dependencies>
<properties>
<poi.version>5.2.3</poi.version>
<poiji.version>4.1.1</poiji.version>
<fastexcel.version>0.15.7</fastexcel.version>
<jxl.version>2.6.12</jxl.version>
</properties>
</project>

View File

@ -0,0 +1,54 @@
package com.baeldung.convert.exceldatatolist;
import com.poiji.annotation.ExcelCellName;
public class FoodInfo {
@ExcelCellName("Category")
private String category; //food category
@ExcelCellName("Name")
private String name; // food name
@ExcelCellName("Measure")
private String measure;
@ExcelCellName("Calories")
private double calories; //amount of calories in kcal/measure
@Override
public String toString() {
return "FoodInfo{" + "category='" + category + '\'' + ", name='" + name + '\'' + ", measure='" + measure + '\'' + ", calories=" + calories + "} \n";
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMeasure() {
return measure;
}
public void setMeasure(String measure) {
this.measure = measure;
}
public double getCalories() {
return calories;
}
public void setCalories(double calories) {
this.calories = calories;
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.convert.exceldatatolist.fastexcel;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
import org.dhatim.fastexcel.reader.ReadableWorkbook;
import org.dhatim.fastexcel.reader.Row;
import org.dhatim.fastexcel.reader.Sheet;
import com.baeldung.convert.exceldatatolist.FoodInfo;
public class ExcelDataToListOfObjectsFastExcel {
public static List<FoodInfo> excelDataToListOfObjets_withFastExcel(String fileLocation)throws IOException, NumberFormatException {
List<FoodInfo> foodData = new ArrayList<FoodInfo>();
try (FileInputStream file = new FileInputStream(fileLocation);
ReadableWorkbook wb = new ReadableWorkbook(file)) {
Sheet sheet = wb.getFirstSheet();
for (Row row:
sheet.read()
) {
if(row.getRowNum() == 1) {
continue;
}
FoodInfo food = new FoodInfo();
food.setCategory(row.getCellText(0));
food.setName(row.getCellText(1));
food.setMeasure(row.getCellText(2));
food.setCalories(Double.parseDouble(row.getCellText(3)));
foodData.add(food);
}
}
return foodData;
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.convert.exceldatatolist.jexcelapi;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.baeldung.convert.exceldatatolist.FoodInfo;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ExcelDataToListOfObjectsJxl {
public static List<FoodInfo> excelDataToListOfObjets_withJxl(String fileLocation) throws IOException, BiffException {
List<FoodInfo> foodData = new ArrayList<FoodInfo>();
Workbook workbook = Workbook.getWorkbook(new File(fileLocation));
Sheet sheet = workbook.getSheet(0);
int rows = sheet.getRows();
for (int i = 1; i < rows; i++) {
FoodInfo foodInfo = new FoodInfo();
foodInfo.setCategory(sheet.getCell(0, i).getContents());
foodInfo.setName(sheet.getCell(1, i).getContents());
foodInfo.setMeasure(sheet.getCell(2, i).getContents());
foodInfo.setCalories(Double.parseDouble(sheet.getCell(3, i).getContents()));
foodData.add(foodInfo);
}
return foodData;
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.convert.exceldatatolist.poi;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.baeldung.convert.exceldatatolist.FoodInfo;
public class ExcelDataToListApachePOI {
public static List<FoodInfo> excelDataToListOfObjets_withApachePOI(String fileLocation) throws IOException {
FileInputStream file = new FileInputStream(new File(fileLocation));
Workbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheetAt(0);
List<FoodInfo> foodData = new ArrayList<FoodInfo>();
DataFormatter dataFormatter = new DataFormatter();
for (int n = 1; n < sheet.getPhysicalNumberOfRows(); n++) {
Row row = sheet.getRow(n);
FoodInfo foodInfo = new FoodInfo();
int i = row.getFirstCellNum();
foodInfo.setCategory(dataFormatter.formatCellValue(row.getCell(i)));
foodInfo.setName(dataFormatter.formatCellValue(row.getCell(++i)));
foodInfo.setMeasure(dataFormatter.formatCellValue(row.getCell(++i)));
foodInfo.setCalories(Double.parseDouble(dataFormatter.formatCellValue(row.getCell(++i))));
foodData.add(foodInfo);
}
return foodData;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.convert.exceldatatolist.poiji;
import java.io.File;
import java.util.List;
import com.baeldung.convert.exceldatatolist.FoodInfo;
import com.poiji.bind.Poiji;
public class ExcelDataToListOfObjectsPOIJI {
public static List<FoodInfo> excelDataToListOfObjets_withPOIJI(String fileLocation){
return Poiji.fromExcel(new File(fileLocation), FoodInfo.class);
}
}

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,53 @@
package com.baeldung.convert.exceldatatolist;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.List;
// import org.junit.jupiter.api.Test;
// import static org.junit.jupiter.api.Assertions.*;
import org.junit.Test;
import com.baeldung.convert.exceldatatolist.fastexcel.ExcelDataToListOfObjectsFastExcel;
import com.baeldung.convert.exceldatatolist.jexcelapi.ExcelDataToListOfObjectsJxl;
import com.baeldung.convert.exceldatatolist.poi.ExcelDataToListApachePOI;
import com.baeldung.convert.exceldatatolist.poiji.ExcelDataToListOfObjectsPOIJI;
import jxl.read.biff.BiffException;
public class ExcelDataToListOfObjectsUnitTest {
@Test
public void whenParsingExcelFileWithPOIJI_thenConvertsToList() throws IOException {
List<FoodInfo> foodInfoList = ExcelDataToListOfObjectsPOIJI.excelDataToListOfObjets_withPOIJI("src/main/resources/food_info.xlsx");
assertEquals("Beverages", foodInfoList.get(0).getCategory());
assertEquals("Dairy", foodInfoList.get(3).getCategory());
}
@Test
public void whenParsingExcelFileWithApachePOI_thenConvertsToList() throws IOException {
List<FoodInfo> foodInfoList = ExcelDataToListApachePOI.excelDataToListOfObjets_withApachePOI("src/main/resources/food_info.xlsx");
assertEquals("Beverages", foodInfoList.get(0).getCategory());
assertEquals("Dairy", foodInfoList.get(3).getCategory());
}
@Test
public void whenParsingExcelFileWithFastExcel_thenConvertsToList() throws IOException {
List<FoodInfo> foodInfoList = ExcelDataToListOfObjectsFastExcel.excelDataToListOfObjets_withFastExcel("src/main/resources/food_info.xlsx");
assertEquals("Beverages", foodInfoList.get(0).getCategory());
assertEquals("Dairy", foodInfoList.get(3).getCategory());
}
@Test
public void whenParsingExcelFileWithJxl_thenConvertsToList() throws IOException, BiffException {
List<FoodInfo> foodInfoList = ExcelDataToListOfObjectsJxl.excelDataToListOfObjets_withJxl("src/main/resources/food_info.xls");
assertEquals("Beverages", foodInfoList.get(0).getCategory());
assertEquals("Dairy", foodInfoList.get(3).getCategory());
}
}

View File

@ -0,0 +1,70 @@
package com.baeldung.poi.excel.expandcolumn;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class ExpandColumnUnitTest {
private Workbook workbook;
private Sheet sheet;
@BeforeEach
void prepareSpreadsheet() {
workbook = new XSSFWorkbook();
sheet = workbook.createSheet();
Row headerRow = sheet.createRow(0);
Cell headerCell1 = headerRow.createCell(0);
headerCell1.setCellValue("Full Name");
Cell headerCell2 = headerRow.createCell(1);
headerCell2.setCellValue("Abbreviation");
Row dataRow = sheet.createRow(1);
Cell dataCell1 = dataRow.createCell(0);
dataCell1.setCellValue("Java Virtual Machine");
Cell dataCell2 = dataRow.createCell(1);
dataCell2.setCellValue("JVM");
dataRow = sheet.createRow(2);
dataCell1 = dataRow.createCell(0);
dataCell1.setCellValue("Java Runtime Environment");
dataCell2 = dataRow.createCell(1);
dataCell2.setCellValue("JRE");
}
@Test
void whenSetColumnWidth_thenColumnSetToTheSpecifiedWidth() {
Row row = sheet.getRow(2);
String cellValue = row.getCell(0).getStringCellValue();
int targetWidth = cellValue.length() * 256;
sheet.setColumnWidth(0, targetWidth);
assertEquals(targetWidth, sheet.getColumnWidth(0));
}
@Test
void whenAutoSizeColumn_thenColumnExpands() {
int originalWidth = sheet.getColumnWidth(0);
sheet.autoSizeColumn(0);
assertThat(sheet.getColumnWidth(0)).isGreaterThan(originalWidth);
}
@AfterEach
void cleanup() throws IOException {
workbook.close();
}
}

View File

@ -63,7 +63,6 @@
<org.apache.httpcomponents.version>4.5.2</org.apache.httpcomponents.version> <org.apache.httpcomponents.version>4.5.2</org.apache.httpcomponents.version>
<velocity-version>1.7</velocity-version> <velocity-version>1.7</velocity-version>
<velocity-tools-version>2.0</velocity-tools-version> <velocity-tools-version>2.0</velocity-tools-version>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
</properties> </properties>
</project> </project>

View File

@ -49,7 +49,6 @@
<properties> <properties>
<asm.version>5.2</asm.version> <asm.version>5.2</asm.version>
<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
</properties> </properties>
</project> </project>

2
aws-modules/aws-dynamodb/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target/
.idea/

View File

@ -0,0 +1,7 @@
## AWS DYNAMODB
This module contains articles about AWS DynamoDB
### Relevant articles
- [Integration Testing with a Local DynamoDB Instance](https://www.baeldung.com/dynamodb-local-integration-tests)

View File

@ -0,0 +1,87 @@
<?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>
<artifactId>aws-dynamodb</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>aws-dynamodb</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>aws-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>${aws-java-sdk.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>DynamoDBLocal</artifactId>
<version>${dynamodblocal.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-plugins-version}</version>
<executions>
<execution>
<id>copy</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope></includeScope>
<includeTypes>so,dll,dylib</includeTypes>
<outputDirectory>native-libs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<gson.version>2.8.0</gson.version>
<dynamodblocal.version>1.21.1</dynamodblocal.version>
<maven-plugins-version>3.1.1</maven-plugins-version>
</properties>
</project>

View File

@ -49,10 +49,10 @@ public class ProductInfoRepositoryIntegrationTest {
@BeforeClass @BeforeClass
public static void setupClass() { public static void setupClass() {
Properties testProperties = loadFromFileInClasspath("test.properties") Properties testProperties = loadFromFileInClasspath("test.properties")
.filter(properties -> !isEmpty(properties.getProperty(AWS_ACCESSKEY))) .filter(properties -> !isEmpty(properties.getProperty(AWS_ACCESSKEY)))
.filter(properties -> !isEmpty(properties.getProperty(AWS_SECRETKEY))) .filter(properties -> !isEmpty(properties.getProperty(AWS_SECRETKEY)))
.filter(properties -> !isEmpty(properties.getProperty(DYNAMODB_ENDPOINT))) .filter(properties -> !isEmpty(properties.getProperty(DYNAMODB_ENDPOINT)))
.orElseThrow(() -> new RuntimeException("Unable to get all of the required test property values")); .orElseThrow(() -> new RuntimeException("Unable to get all of the required test property values"));
String amazonAWSAccessKey = testProperties.getProperty(AWS_ACCESSKEY); String amazonAWSAccessKey = testProperties.getProperty(AWS_ACCESSKEY);
String amazonAWSSecretKey = testProperties.getProperty(AWS_SECRETKEY); String amazonAWSSecretKey = testProperties.getProperty(AWS_SECRETKEY);

View File

@ -5,6 +5,5 @@ This module contains articles about various Amazon Web Services (AWS) such as EC
### Relevant articles ### Relevant articles
- [Managing EC2 Instances in Java](https://www.baeldung.com/ec2-java) - [Managing EC2 Instances in Java](https://www.baeldung.com/ec2-java)
- [Integration Testing with a Local DynamoDB Instance](https://www.baeldung.com/dynamodb-local-integration-tests)
- [Managing Amazon SQS Queues in Java](https://www.baeldung.com/aws-queues-java) - [Managing Amazon SQS Queues in Java](https://www.baeldung.com/aws-queues-java)
- [Guide to AWS Aurora RDS with Java](https://www.baeldung.com/aws-aurora-rds-java) - [Guide to AWS Aurora RDS with Java](https://www.baeldung.com/aws-aurora-rds-java)

View File

@ -16,31 +16,9 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.amazonaws</groupId> <groupId>software.amazon.awssdk</groupId>
<artifactId>aws-java-sdk</artifactId> <artifactId>aws-sdk-java</artifactId>
<version>${aws-java-sdk.version}</version> <version>${aws-java-sdk-v2.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>${aws-lambda-java-core.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>${aws-lambda-java-events.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
@ -52,12 +30,6 @@
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>${gson.version}</version> <version>${gson.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>DynamoDBLocal</artifactId>
<version>${dynamodblocal.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -101,8 +73,6 @@
</build> </build>
<properties> <properties>
<aws-lambda-java-events.version>1.3.0</aws-lambda-java-events.version>
<aws-lambda-java-core.version>1.1.0</aws-lambda-java-core.version>
<gson.version>2.8.0</gson.version> <gson.version>2.8.0</gson.version>
<dynamodblocal.version>1.21.1</dynamodblocal.version> <dynamodblocal.version>1.21.1</dynamodblocal.version>
<commons-codec-version>1.10.L001</commons-codec-version> <commons-codec-version>1.10.L001</commons-codec-version>

View File

@ -2,136 +2,148 @@ package com.baeldung.ec2;
import java.util.Arrays; import java.util.Arrays;
import com.amazonaws.auth.AWSCredentials; import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider; import software.amazon.awssdk.regions.Region;
import com.amazonaws.auth.BasicAWSCredentials; import software.amazon.awssdk.services.ec2.Ec2Client;
import com.amazonaws.regions.Regions; import software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressRequest;
import com.amazonaws.services.ec2.AmazonEC2; import software.amazon.awssdk.services.ec2.model.CreateKeyPairRequest;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import software.amazon.awssdk.services.ec2.model.CreateKeyPairResponse;
import com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest; import software.amazon.awssdk.services.ec2.model.CreateSecurityGroupRequest;
import com.amazonaws.services.ec2.model.CreateKeyPairRequest; import software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest;
import com.amazonaws.services.ec2.model.CreateKeyPairResult; import software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse;
import com.amazonaws.services.ec2.model.CreateSecurityGroupRequest; import software.amazon.awssdk.services.ec2.model.DescribeKeyPairsRequest;
import com.amazonaws.services.ec2.model.DescribeInstancesRequest; import software.amazon.awssdk.services.ec2.model.DescribeKeyPairsResponse;
import com.amazonaws.services.ec2.model.DescribeInstancesResult; import software.amazon.awssdk.services.ec2.model.IpPermission;
import com.amazonaws.services.ec2.model.DescribeKeyPairsRequest; import software.amazon.awssdk.services.ec2.model.IpRange;
import com.amazonaws.services.ec2.model.DescribeKeyPairsResult; import software.amazon.awssdk.services.ec2.model.MonitorInstancesRequest;
import com.amazonaws.services.ec2.model.IpPermission; import software.amazon.awssdk.services.ec2.model.RebootInstancesRequest;
import com.amazonaws.services.ec2.model.IpRange; import software.amazon.awssdk.services.ec2.model.RunInstancesRequest;
import com.amazonaws.services.ec2.model.MonitorInstancesRequest; import software.amazon.awssdk.services.ec2.model.RunInstancesResponse;
import com.amazonaws.services.ec2.model.RebootInstancesRequest; import software.amazon.awssdk.services.ec2.model.StartInstancesRequest;
import com.amazonaws.services.ec2.model.RunInstancesRequest; import software.amazon.awssdk.services.ec2.model.StartInstancesResponse;
import com.amazonaws.services.ec2.model.StartInstancesRequest; import software.amazon.awssdk.services.ec2.model.StopInstancesRequest;
import com.amazonaws.services.ec2.model.StopInstancesRequest; import software.amazon.awssdk.services.ec2.model.UnmonitorInstancesRequest;
import com.amazonaws.services.ec2.model.UnmonitorInstancesRequest;
public class EC2Application { public class EC2Application {
private static final AWSCredentials credentials;
static {
// put your accesskey and secretkey here
credentials = new BasicAWSCredentials(
"<AWS accesskey>",
"<AWS secretkey>"
);
}
public static void main(String[] args) { public static void main(String[] args) {
// Set up the client // Set up the client
AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard() Ec2Client ec2Client = Ec2Client.builder()
.withCredentials(new AWSStaticCredentialsProvider(credentials)) .credentialsProvider(ProfileCredentialsProvider.create("default"))
.withRegion(Regions.US_EAST_1) .region(Region.US_EAST_1)
.build(); .build();
// Create a security group // Create a security group
CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest().withGroupName("BaeldungSecurityGroup") CreateSecurityGroupRequest createSecurityGroupRequest = CreateSecurityGroupRequest.builder()
.withDescription("Baeldung Security Group"); .groupName("BaeldungSecurityGroup")
.description("Baeldung Security Group")
.build();
ec2Client.createSecurityGroup(createSecurityGroupRequest); ec2Client.createSecurityGroup(createSecurityGroupRequest);
// Allow HTTP and SSH traffic // Allow HTTP and SSH traffic
IpRange ipRange1 = new IpRange().withCidrIp("0.0.0.0/0"); IpRange ipRange1 = IpRange.builder()
.cidrIp("0.0.0.0/0")
.build();
IpPermission ipPermission1 = new IpPermission().withIpv4Ranges(Arrays.asList(new IpRange[] { ipRange1 })) IpPermission ipPermission1 = IpPermission.builder()
.withIpProtocol("tcp") .ipRanges(Arrays.asList(ipRange1))
.withFromPort(80) .ipProtocol("tcp")
.withToPort(80); .fromPort(80)
.toPort(80)
.build();
IpPermission ipPermission2 = new IpPermission().withIpv4Ranges(Arrays.asList(new IpRange[] { ipRange1 })) IpPermission ipPermission2 = IpPermission.builder()
.withIpProtocol("tcp") .ipRanges(Arrays.asList(ipRange1))
.withFromPort(22) .ipProtocol("tcp")
.withToPort(22); .fromPort(22)
.toPort(22)
.build();
AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = new AuthorizeSecurityGroupIngressRequest() AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = AuthorizeSecurityGroupIngressRequest
.withGroupName("BaeldungSecurityGroup") .builder()
.withIpPermissions(ipPermission1, ipPermission2); .groupName("BaeldungSecurityGroup")
.ipPermissions(ipPermission1, ipPermission2)
.build();
ec2Client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest); ec2Client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);
// Create KeyPair // Create KeyPair
CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest() CreateKeyPairRequest createKeyPairRequest = CreateKeyPairRequest.builder()
.withKeyName("baeldung-key-pair"); .keyName("baeldung-key-pair")
CreateKeyPairResult createKeyPairResult = ec2Client.createKeyPair(createKeyPairRequest); .build();
String privateKey = createKeyPairResult
.getKeyPair() CreateKeyPairResponse createKeyPairResponse = ec2Client.createKeyPair(createKeyPairRequest);
.getKeyMaterial(); // make sure you keep it, the private key, Amazon doesn't store the private key String privateKey = createKeyPairResponse.keyMaterial();
// make sure you keep it, the private key, Amazon doesn't store the private key
// See what key-pairs you've got // See what key-pairs you've got
DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest(); DescribeKeyPairsRequest describeKeyPairsRequest = DescribeKeyPairsRequest.builder()
DescribeKeyPairsResult describeKeyPairsResult = ec2Client.describeKeyPairs(describeKeyPairsRequest); .build();
DescribeKeyPairsResponse describeKeyPairsResponse = ec2Client.describeKeyPairs(describeKeyPairsRequest);
// Launch an Amazon Instance // Launch an Amazon Instance
RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId("ami-97785bed") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html | https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/usingsharedamis-finding.html RunInstancesRequest runInstancesRequest = RunInstancesRequest.builder()
.withInstanceType("t2.micro") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html .imageId("ami-97785bed")
.withMinCount(1) .instanceType("t2.micro") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html
.withMaxCount(1) .minCount(1)
.withKeyName("baeldung-key-pair") // optional - if not present, can't connect to instance .maxCount(1)
.withSecurityGroups("BaeldungSecurityGroup"); .keyName("baeldung-key-pair") // optional - if not present, can't connect to instance
.securityGroups("BaeldungSecurityGroup")
.build();
String yourInstanceId = ec2Client.runInstances(runInstancesRequest).getReservation().getInstances().get(0).getInstanceId(); RunInstancesResponse runInstancesResponse = ec2Client.runInstances(runInstancesRequest);
String yourInstanceId = runInstancesResponse.instances().get(0).instanceId();
// Start an Instance // Start an Instance
StartInstancesRequest startInstancesRequest = new StartInstancesRequest() StartInstancesRequest startInstancesRequest = StartInstancesRequest.builder()
.withInstanceIds(yourInstanceId); .instanceIds(yourInstanceId)
.build();
StartInstancesResponse startInstancesResponse = ec2Client.startInstances(startInstancesRequest);
ec2Client.startInstances(startInstancesRequest);
// Monitor Instances // Monitor Instances
MonitorInstancesRequest monitorInstancesRequest = new MonitorInstancesRequest() MonitorInstancesRequest monitorInstancesRequest = MonitorInstancesRequest.builder()
.withInstanceIds(yourInstanceId); .instanceIds(yourInstanceId)
.build();
ec2Client.monitorInstances(monitorInstancesRequest); ec2Client.monitorInstances(monitorInstancesRequest);
UnmonitorInstancesRequest unmonitorInstancesRequest = new UnmonitorInstancesRequest() UnmonitorInstancesRequest unmonitorInstancesRequest = UnmonitorInstancesRequest.builder()
.withInstanceIds(yourInstanceId); .instanceIds(yourInstanceId)
.build();
ec2Client.unmonitorInstances(unmonitorInstancesRequest); ec2Client.unmonitorInstances(unmonitorInstancesRequest);
// Reboot an Instance // Reboot an Instance
RebootInstancesRequest rebootInstancesRequest = RebootInstancesRequest.builder()
RebootInstancesRequest rebootInstancesRequest = new RebootInstancesRequest() .instanceIds(yourInstanceId)
.withInstanceIds(yourInstanceId); .build();
ec2Client.rebootInstances(rebootInstancesRequest); ec2Client.rebootInstances(rebootInstancesRequest);
// Stop an Instance // Stop an Instance
StopInstancesRequest stopInstancesRequest = new StopInstancesRequest() StopInstancesRequest stopInstancesRequest = StopInstancesRequest.builder()
.withInstanceIds(yourInstanceId); .instanceIds(yourInstanceId)
.build();
ec2Client.stopInstances(stopInstancesRequest) ec2Client.stopInstances(stopInstancesRequest)
.getStoppingInstances() .stoppingInstances()
.get(0) .get(0)
.getPreviousState() .previousState()
.getName(); .name();
// Describe an Instance // Describe an Instance
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest(); DescribeInstancesRequest describeInstancesRequest = DescribeInstancesRequest.builder().build();
DescribeInstancesResult response = ec2Client.describeInstances(describeInstancesRequest); DescribeInstancesResponse response = ec2Client.describeInstances(describeInstancesRequest);
System.out.println(response.getReservations() System.out.println(response.reservations()
.get(0) .get(0)
.getInstances() .instances()
.get(0) .get(0)
.getKernelId()); .kernelId());
} }
} }

View File

@ -1,13 +1,5 @@
package com.baeldung.rds; package com.baeldung.rds;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.rds.AmazonRDS;
import com.amazonaws.services.rds.AmazonRDSClientBuilder;
import com.amazonaws.services.rds.model.*;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.sql.*; import java.sql.*;
@ -16,12 +8,22 @@ import java.util.Properties;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Logger; import java.util.logging.Logger;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.rds.RdsClient;
import software.amazon.awssdk.services.rds.model.CreateDbInstanceRequest;
import software.amazon.awssdk.services.rds.model.CreateDbInstanceResponse;
import software.amazon.awssdk.services.rds.model.DBInstance;
import software.amazon.awssdk.services.rds.model.DeleteDbInstanceRequest;
import software.amazon.awssdk.services.rds.model.DeleteDbInstanceResponse;
import software.amazon.awssdk.services.rds.model.DescribeDbInstancesResponse;
import software.amazon.awssdk.services.rds.model.Endpoint;
public class AWSRDSService { public class AWSRDSService {
final static Logger logger = Logger.getLogger(AWSRDSService.class.getName()); final static Logger logger = Logger.getLogger(AWSRDSService.class.getName());
private AWSCredentialsProvider credentials; private RdsClient rdsClient;
private AmazonRDS amazonRDS;
private String db_username; private String db_username;
private String db_password; private String db_password;
private String db_database; private String db_database;
@ -34,22 +36,17 @@ public class AWSRDSService {
* **/ * **/
public AWSRDSService() throws IOException { public AWSRDSService() throws IOException {
//Init RDS client with credentials and region. //Init RDS client with credentials and region.
credentials = new
AWSStaticCredentialsProvider(new
BasicAWSCredentials("<ACCESS_KEY>",
"<SECRET_KEY>"));
amazonRDS = AmazonRDSClientBuilder.standard().withCredentials(credentials)
.withRegion(Regions.AP_SOUTHEAST_2).build();
Properties prop = new Properties(); Properties prop = new Properties();
InputStream input = AWSRDSService.class.getClassLoader().getResourceAsStream("db.properties"); InputStream input = AWSRDSService.class.getClassLoader().getResourceAsStream("db.properties");
prop.load(input); prop.load(input);
db_username = prop.getProperty("db_username"); db_username = prop.getProperty("db_username");
db_password = prop.getProperty("db_password"); db_password = prop.getProperty("db_password");
db_database = prop.getProperty("db_database"); db_database = prop.getProperty("db_database");
}
public AWSRDSService(AmazonRDS amazonRDS){ rdsClient = RdsClient.builder()
this.amazonRDS = amazonRDS; .region(Region.AP_SOUTHEAST_2)
.credentialsProvider(ProfileCredentialsProvider.create("default"))
.build();
} }
/** /**
@ -60,29 +57,29 @@ public class AWSRDSService {
public String launchInstance() { public String launchInstance() {
String identifier = ""; String identifier = "";
CreateDBInstanceRequest request = new CreateDBInstanceRequest(); CreateDbInstanceRequest instanceRequest = CreateDbInstanceRequest.builder()
// RDS instance name .dbInstanceIdentifier("Sydney")
request.setDBInstanceIdentifier("Sydney"); .engine("postgres")
request.setEngine("postgres"); .multiAZ(false)
request.setMultiAZ(false); .masterUsername(db_username)
request.setMasterUsername(db_username); .masterUserPassword(db_password)
request.setMasterUserPassword(db_password); .dbName(db_database)
request.setDBName(db_database); .storageType("gp2")
request.setStorageType("gp2"); .allocatedStorage(10)
request.setAllocatedStorage(10); .build();
DBInstance instance = amazonRDS.createDBInstance(request); CreateDbInstanceResponse createDbInstanceResponse = rdsClient.createDBInstance(instanceRequest);
// Information about the new RDS instance // Information about the new RDS instance
identifier = instance.getDBInstanceIdentifier(); identifier = createDbInstanceResponse.dbInstance().dbInstanceIdentifier();
String status = instance.getDBInstanceStatus(); String status = createDbInstanceResponse.dbInstance().dbInstanceStatus();
Endpoint endpoint = instance.getEndpoint(); Endpoint endpoint = createDbInstanceResponse.dbInstance().endpoint();
String endpoint_url = "Endpoint URL not available yet."; String endpointUrl = "Endpoint URL not available yet.";
if (endpoint != null) { if (endpoint != null) {
endpoint_url = endpoint.toString(); endpointUrl = endpoint.toString();
} }
logger.info(identifier + "\t" + status); logger.info(identifier + "\t" + status);
logger.info(endpoint_url); logger.info(endpointUrl);
return identifier; return identifier;
@ -90,44 +87,44 @@ public class AWSRDSService {
// Describe DB instances // Describe DB instances
public void listInstances() { public void listInstances() {
DescribeDBInstancesResult result = amazonRDS.describeDBInstances(); DescribeDbInstancesResponse response = rdsClient.describeDBInstances();
List<DBInstance> instances = result.getDBInstances(); List<DBInstance> instances = response.dbInstances();
for (DBInstance instance : instances) { for (DBInstance instance : instances) {
// Information about each RDS instance // Information about each RDS instance
String identifier = instance.getDBInstanceIdentifier(); String identifier = instance.dbInstanceIdentifier();
String engine = instance.getEngine(); String engine = instance.engine();
String status = instance.getDBInstanceStatus(); String status = instance.dbInstanceStatus();
Endpoint endpoint = instance.getEndpoint(); Endpoint endpoint = instance.endpoint();
String endpoint_url = "Endpoint URL not available yet."; String endpointUrl = "Endpoint URL not available yet.";
if (endpoint != null) { if (endpoint != null) {
endpoint_url = endpoint.toString(); endpointUrl = endpoint.toString();
} }
logger.info(identifier + "\t" + engine + "\t" + status); logger.info(identifier + "\t" + engine + "\t" + status);
logger.info("\t" + endpoint_url); logger.info("\t" + endpointUrl);
} }
} }
//Delete RDS instance //Delete RDS instance
public void terminateInstance(String identifier) { public void terminateInstance(String identifier) {
DeleteDBInstanceRequest request = new DeleteDBInstanceRequest(); DeleteDbInstanceRequest request = DeleteDbInstanceRequest.builder()
request.setDBInstanceIdentifier(identifier); .dbInstanceIdentifier(identifier)
request.setSkipFinalSnapshot(true); .skipFinalSnapshot(true)
.build();
// Delete the RDS instance // Delete the RDS instance
DBInstance instance = amazonRDS.deleteDBInstance(request); DeleteDbInstanceResponse response = rdsClient.deleteDBInstance(request);
// Information about the RDS instance being deleted // Information about the RDS instance being deleted
String status = instance.getDBInstanceStatus(); String status = response.dbInstance().dbInstanceStatus();
Endpoint endpoint = instance.getEndpoint(); Endpoint endpoint = response.dbInstance().endpoint();
String endpoint_url = "Endpoint URL not available yet."; String endpointUrl = "Endpoint URL not available yet.";
if (endpoint != null) { if (endpoint != null) {
endpoint_url = endpoint.toString(); endpointUrl = endpoint.toString();
} }
logger.info(identifier + "\t" + status); logger.info(identifier + "\t" + status);
logger.info(endpoint_url); logger.info(endpointUrl);
} }

View File

@ -5,140 +5,190 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.amazonaws.auth.AWSCredentials; import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider; import software.amazon.awssdk.regions.Region;
import com.amazonaws.auth.BasicAWSCredentials; import software.amazon.awssdk.services.sqs.SqsClient;
import com.amazonaws.regions.Regions; import software.amazon.awssdk.services.sqs.model.CreateQueueRequest;
import com.amazonaws.services.sqs.AmazonSQSClientBuilder; import software.amazon.awssdk.services.sqs.model.DeleteMessageRequest;
import com.amazonaws.services.sqs.model.CreateQueueRequest; import software.amazon.awssdk.services.sqs.model.GetQueueAttributesRequest;
import com.amazonaws.services.sqs.model.DeleteMessageRequest; import software.amazon.awssdk.services.sqs.model.GetQueueAttributesResponse;
import com.amazonaws.services.sqs.model.GetQueueAttributesRequest; import software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest;
import com.amazonaws.services.sqs.model.GetQueueAttributesResult; import software.amazon.awssdk.services.sqs.model.GetQueueUrlResponse;
import com.amazonaws.services.sqs.model.MessageAttributeValue; import software.amazon.awssdk.services.sqs.model.Message;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest; import software.amazon.awssdk.services.sqs.model.MessageAttributeValue;
import com.amazonaws.services.sqs.model.SendMessageBatchRequest; import software.amazon.awssdk.services.sqs.model.QueueAttributeName;
import com.amazonaws.services.sqs.model.SendMessageRequest; import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest;
import com.amazonaws.services.sqs.model.SetQueueAttributesRequest; import software.amazon.awssdk.services.sqs.model.SendMessageBatchRequest;
import com.amazonaws.services.sqs.model.SendMessageBatchRequestEntry; import software.amazon.awssdk.services.sqs.model.SendMessageBatchRequestEntry;
import com.amazonaws.services.sqs.model.Message; import software.amazon.awssdk.services.sqs.model.SendMessageRequest;
import com.amazonaws.services.sqs.AmazonSQS; import software.amazon.awssdk.services.sqs.model.SetQueueAttributesRequest;
public class SQSApplication { public class SQSApplication {
private static final AWSCredentials credentials; private static final String STANDARD_QUEUE_NAME = "baeldung-queue";
private static final String FIFO_QUEUE_NAME = "baeldung-queue.fifo";
static { private static final String DEAD_LETTER_QUEUE_NAME = "baeldung-dead-letter-queue";
// put your accesskey and secretkey here
credentials = new BasicAWSCredentials(
"<AWS accesskey>",
"<AWS secretkey>"
);
}
public static void main(String[] args) { public static void main(String[] args) {
// Set up the client // Set up the client
AmazonSQS sqs = AmazonSQSClientBuilder.standard() SqsClient sqsClient = SqsClient.builder()
.withCredentials(new AWSStaticCredentialsProvider(credentials)) .region(Region.US_EAST_1)
.withRegion(Regions.US_EAST_1) .credentialsProvider(ProfileCredentialsProvider.create())
.build(); .build();
// Create a standard queue // Create a standard queue
CreateQueueRequest createStandardQueueRequest = CreateQueueRequest.builder()
.queueName(STANDARD_QUEUE_NAME)
.build();
CreateQueueRequest createStandardQueueRequest = new CreateQueueRequest("baeldung-queue"); sqsClient.createQueue(createStandardQueueRequest);
String standardQueueUrl = sqs.createQueue(createStandardQueueRequest)
.getQueueUrl(); System.out.println("\nGet queue url");
GetQueueUrlResponse getQueueUrlResponse = sqsClient.getQueueUrl(GetQueueUrlRequest.builder()
.queueName(STANDARD_QUEUE_NAME)
.build());
String standardQueueUrl = getQueueUrlResponse.queueUrl();
System.out.println(standardQueueUrl); System.out.println(standardQueueUrl);
// Create a fifo queue // Create a fifo queue
Map<QueueAttributeName, String> queueAttributes = new HashMap<>();
queueAttributes.put(QueueAttributeName.FIFO_QUEUE, "true");
queueAttributes.put(QueueAttributeName.CONTENT_BASED_DEDUPLICATION, "true");
Map<String, String> queueAttributes = new HashMap<String, String>(); CreateQueueRequest createFifoQueueRequest = CreateQueueRequest.builder()
queueAttributes.put("FifoQueue", "true"); .queueName(FIFO_QUEUE_NAME)
queueAttributes.put("ContentBasedDeduplication", "true"); .attributes(queueAttributes)
.build();
CreateQueueRequest createFifoQueueRequest = new CreateQueueRequest("baeldung-queue.fifo").withAttributes(queueAttributes); sqsClient.createQueue(createFifoQueueRequest);
String fifoQueueUrl = sqs.createQueue(createFifoQueueRequest)
.getQueueUrl(); GetQueueUrlResponse getFifoQueueUrlResponse = sqsClient.getQueueUrl(GetQueueUrlRequest.builder()
.queueName(FIFO_QUEUE_NAME)
.build());
String fifoQueueUrl = getFifoQueueUrlResponse.queueUrl();
System.out.println(fifoQueueUrl); System.out.println(fifoQueueUrl);
// Set up a dead letter queue // Set up a dead letter queue
CreateQueueRequest createDeadLetterQueueRequest = CreateQueueRequest.builder()
.queueName(DEAD_LETTER_QUEUE_NAME)
.build();
String deadLetterQueueUrl = sqs.createQueue("baeldung-dead-letter-queue") String deadLetterQueueUrl = sqsClient.createQueue(createDeadLetterQueueRequest)
.getQueueUrl(); .queueUrl();
GetQueueAttributesResult deadLetterQueueAttributes = sqs.getQueueAttributes(new GetQueueAttributesRequest(deadLetterQueueUrl).withAttributeNames("QueueArn")); GetQueueAttributesRequest getQueueAttributesRequest = GetQueueAttributesRequest.builder()
.queueUrl(deadLetterQueueUrl)
.attributeNames(QueueAttributeName.QUEUE_ARN)
.build();
String deadLetterQueueARN = deadLetterQueueAttributes.getAttributes() GetQueueAttributesResponse deadLetterQueueAttributes = sqsClient.getQueueAttributes(getQueueAttributesRequest);
String deadLetterQueueARN = deadLetterQueueAttributes.attributes()
.get("QueueArn"); .get("QueueArn");
SetQueueAttributesRequest queueAttributesRequest = new SetQueueAttributesRequest().withQueueUrl(standardQueueUrl) Map<QueueAttributeName, String> attributes = new HashMap<>();
.addAttributesEntry("RedrivePolicy", "{\"maxReceiveCount\":\"2\", " + "\"deadLetterTargetArn\":\"" + deadLetterQueueARN + "\"}"); attributes.put(QueueAttributeName.REDRIVE_POLICY, "{\"maxReceiveCount\":\"5\", \"deadLetterTargetArn\":\"" + deadLetterQueueARN + "\"}");
sqs.setQueueAttributes(queueAttributesRequest); SetQueueAttributesRequest queueAttributesRequest = SetQueueAttributesRequest.builder()
.queueUrl(standardQueueUrl)
.attributes(attributes)
.build();
sqsClient.setQueueAttributes(queueAttributesRequest);
// Send a message to a standard queue // Send a message to a standard queue
Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();
MessageAttributeValue messageAttributeValue = MessageAttributeValue.builder()
.stringValue("This is an attribute")
.dataType("String")
.build();
messageAttributes.put("AttributeOne", new MessageAttributeValue().withStringValue("This is an attribute") messageAttributes.put("AttributeOne", messageAttributeValue);
.withDataType("String"));
SendMessageRequest sendMessageStandardQueue = new SendMessageRequest().withQueueUrl(standardQueueUrl) SendMessageRequest sendMessageStandardQueue = SendMessageRequest.builder()
.withMessageBody("A simple message.") .queueUrl(standardQueueUrl)
.withDelaySeconds(30) // Message will arrive in the queue after 30 seconds. We can use this only in standard queues .messageBody("A simple message.")
.withMessageAttributes(messageAttributes); .delaySeconds(30) // Message will arrive in the queue after 30 seconds. We can use this only in standard queues
.messageAttributes(messageAttributes)
.build();
sqs.sendMessage(sendMessageStandardQueue); sqsClient.sendMessage(sendMessageStandardQueue);
// Send a message to a fifo queue // Send a message to a fifo queue
SendMessageRequest sendMessageFifoQueue = new SendMessageRequest().withQueueUrl(fifoQueueUrl) SendMessageRequest sendMessageFifoQueue = SendMessageRequest.builder()
.withMessageBody("FIFO Queue") .queueUrl(fifoQueueUrl)
.withMessageGroupId("baeldung-group-1") .messageBody("FIFO Queue")
.withMessageAttributes(messageAttributes); .messageGroupId("baeldung-group-1")
.messageAttributes(messageAttributes)
.build();
sqs.sendMessage(sendMessageFifoQueue); sqsClient.sendMessage(sendMessageFifoQueue);
// Send multiple messages // Send multiple messages
List<SendMessageBatchRequestEntry> messageEntries = new ArrayList<>(); List<SendMessageBatchRequestEntry> messageEntries = new ArrayList<>();
messageEntries.add(new SendMessageBatchRequestEntry().withId("id-1") SendMessageBatchRequestEntry messageBatchRequestEntry1 = SendMessageBatchRequestEntry.builder()
.withMessageBody("batch-1") .id("id-1")
.withMessageGroupId("baeldung-group-1")); .messageBody("batch-1")
messageEntries.add(new SendMessageBatchRequestEntry().withId("id-2") .messageGroupId("baeldung-group-1")
.withMessageBody("batch-2") .build();
.withMessageGroupId("baeldung-group-1"));
SendMessageBatchRequest sendMessageBatchRequest = new SendMessageBatchRequest(fifoQueueUrl, messageEntries); SendMessageBatchRequestEntry messageBatchRequestEntry2 = SendMessageBatchRequestEntry.builder()
sqs.sendMessageBatch(sendMessageBatchRequest); .id("id-2")
.messageBody("batch-2")
.messageGroupId("baeldung-group-1")
.build();
messageEntries.add(messageBatchRequestEntry1);
messageEntries.add(messageBatchRequestEntry2);
SendMessageBatchRequest sendMessageBatchRequest = SendMessageBatchRequest.builder()
.queueUrl(fifoQueueUrl)
.entries(messageEntries)
.build();
sqsClient.sendMessageBatch(sendMessageBatchRequest);
// Read a message from a queue // Read a message from a queue
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(fifoQueueUrl).withWaitTimeSeconds(10) // Long polling; ReceiveMessageRequest receiveMessageRequest = ReceiveMessageRequest.builder()
.withMaxNumberOfMessages(1); // Max is 10 .waitTimeSeconds(10)
.maxNumberOfMessages(10)
.build();
List<Message> sqsMessages = sqs.receiveMessage(receiveMessageRequest) List<Message> sqsMessages = sqsClient.receiveMessage(receiveMessageRequest)
.getMessages(); .messages();
sqsMessages.get(0) sqsMessages.get(0)
.getAttributes(); .attributes();
sqsMessages.get(0) sqsMessages.get(0)
.getBody(); .body();
// Delete a message from a queue // Delete a message from a queue
DeleteMessageRequest deleteMessageRequest = DeleteMessageRequest.builder()
.queueUrl(fifoQueueUrl)
.receiptHandle(sqsMessages.get(0)
.receiptHandle())
.build();
sqs.deleteMessage(new DeleteMessageRequest().withQueueUrl(fifoQueueUrl) sqsClient.deleteMessage(deleteMessageRequest);
.withReceiptHandle(sqsMessages.get(0)
.getReceiptHandle()));
// Monitoring // Monitoring
GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest(standardQueueUrl).withAttributeNames("All"); GetQueueAttributesRequest getQueueAttributesRequestForMonitoring = GetQueueAttributesRequest.builder()
GetQueueAttributesResult getQueueAttributesResult = sqs.getQueueAttributes(getQueueAttributesRequest); .queueUrl(standardQueueUrl)
System.out.println(String.format("The number of messages on the queue: %s", getQueueAttributesResult.getAttributes() .build();
GetQueueAttributesResponse attributesResponse = sqsClient.getQueueAttributes(getQueueAttributesRequestForMonitoring);
System.out.println(String.format("The number of messages on the queue: %s", attributesResponse.attributes()
.get("ApproximateNumberOfMessages"))); .get("ApproximateNumberOfMessages")));
System.out.println(String.format("The number of messages in flight: %s", getQueueAttributesResult.getAttributes() System.out.println(String.format("The number of messages in flight: %s", attributesResponse.attributes()
.get("ApproximateNumberOfMessagesNotVisible"))); .get("ApproximateNumberOfMessagesNotVisible")));
} }

View File

@ -4,8 +4,11 @@ This module contains articles about Simple Storage Service (S3) on AWS
### Relevant articles ### Relevant articles
- [AWS S3 with Java](https://www.baeldung.com/aws-s3-java) - [AWS S3 with Java](https://www.baeldung.com/java-aws-s3)
- [Multipart Uploads in Amazon S3 with Java](https://www.baeldung.com/aws-s3-multipart-upload) - [Multipart Uploads in Amazon S3 with Java](https://www.baeldung.com/aws-s3-multipart-upload)
- [Using the JetS3t Java Client With Amazon S3](https://www.baeldung.com/jets3t-amazon-s3) - [Using the JetS3t Java Client With Amazon S3](https://www.baeldung.com/jets3t-amazon-s3)
- [Check if a Specified Key Exists in a Given S3 Bucket Using Java](https://www.baeldung.com/java-aws-s3-check-specified-key-exists) - [Check if a Specified Key Exists in a Given S3 Bucket Using Java](https://www.baeldung.com/java-aws-s3-check-specified-key-exists)
- [Listing All AWS S3 Objects in a Bucket Using Java](https://www.baeldung.com/java-aws-s3-list-bucket-objects) - [Listing All AWS S3 Objects in a Bucket Using Java](https://www.baeldung.com/java-aws-s3-list-bucket-objects)
- [Update an Existing Amazon S3 Object Using Java](https://www.baeldung.com/java-update-amazon-s3-object)
- [How To Rename Files and Folders in Amazon S3](https://www.baeldung.com/java-amazon-s3-rename-files-folders)
- [Update an Existing Amazon S3 Object Using Java](https://www.baeldung.com/java-update-amazon-s3-object)

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> 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> <modelVersion>4.0.0</modelVersion>
<artifactId>aws-s3</artifactId> <artifactId>aws-s3</artifactId>
<version>0.1.0-SNAPSHOT</version> <version>0.1.0-SNAPSHOT</version>

Some files were not shown because too many files have changed in this diff Show More