Merge branch 'master' into BAEL-6541-convert-relative-path
This commit is contained in:
+5
-1
@@ -70,6 +70,7 @@ jmeter/src/main/resources/*-JMeter*.csv
|
||||
jmeter/src/main/resources/*ReportsDashboard*.csv
|
||||
jmeter/src/main/resources/dashboard/*ReportsDashboard*.csv
|
||||
jmeter/src/main/resources/*FileExtractionExample.csv
|
||||
jmeter/src/main/resources/*_Summary-Report.csv
|
||||
|
||||
ninja/devDb.mv.db
|
||||
|
||||
@@ -120,4 +121,7 @@ libraries-2/src/test/resources/crawler4j/**
|
||||
devDb*.db
|
||||
|
||||
#jaxb
|
||||
*.xjb
|
||||
*.xjb
|
||||
|
||||
#neo4j
|
||||
persistence-modules/neo4j/data/**
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.httpclient.httpclient;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
|
||||
public final class ClientUtil {
|
||||
|
||||
private ClientUtil(){}
|
||||
|
||||
public static void closeClient(CloseableHttpClient client) throws IOException {
|
||||
if (client == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
+211
@@ -0,0 +1,211 @@
|
||||
package com.baeldung.httpclient.httpclient;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.emptyArray;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
import org.apache.hc.client5.http.config.ConnectionConfig;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
|
||||
import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||
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.HttpClientBuilder;
|
||||
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
||||
import org.apache.hc.core5.http.ContentType;
|
||||
import org.apache.hc.core5.http.Header;
|
||||
import org.apache.hc.core5.http.HttpHeaders;
|
||||
import org.apache.hc.core5.http.NameValuePair;
|
||||
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
||||
import org.apache.hc.core5.http.message.BasicNameValuePair;
|
||||
import org.apache.hc.core5.util.Timeout;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
class HttpClientCookBookLiveTest {
|
||||
|
||||
private static final String SAMPLE_GET_URL = "http://www.google.com";
|
||||
private static final String SAMPLE_POST_URL = "http://www.github.com";
|
||||
|
||||
private CloseableHttpClient httpClient;
|
||||
|
||||
private CloseableHttpResponse response;
|
||||
|
||||
@BeforeEach
|
||||
public final void before() {
|
||||
httpClient = HttpClientBuilder.create().build();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public final void after() throws IOException {
|
||||
ClientUtil.closeClient(httpClient);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenGetRequestExecuted_thenCorrectStatusCode() throws IOException {
|
||||
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
|
||||
httpClient.execute(httpGet,
|
||||
response -> {
|
||||
assertThat(response.getCode()).isEqualTo(200);
|
||||
return response;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenGetRequestExecuted_thenCorrectContentMimeType() throws IOException {
|
||||
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
|
||||
httpClient.execute(httpGet,
|
||||
response -> {
|
||||
final String contentMimeType = ContentType.parse(response.getEntity().getContentType()).getMimeType();
|
||||
assertThat(contentMimeType).isEqualTo(ContentType.TEXT_HTML.getMimeType());
|
||||
return response;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenGetRequestExecuted_thenCorrectResponse() throws IOException {
|
||||
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
|
||||
httpClient.execute(httpGet,
|
||||
response -> {
|
||||
String bodyAsString = EntityUtils.toString(response.getEntity());
|
||||
assertThat(bodyAsString, notNullValue());
|
||||
return response;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenConfigureTimeoutOnRequest_thenCorrectResponse() throws IOException {
|
||||
RequestConfig requestConfig = RequestConfig.custom()
|
||||
.setConnectionRequestTimeout(Timeout.ofMilliseconds(2000L))
|
||||
.build();
|
||||
|
||||
CloseableHttpClient httpClient = HttpClientBuilder.create()
|
||||
.setDefaultRequestConfig(requestConfig)
|
||||
.build();
|
||||
|
||||
HttpGet request = new HttpGet(SAMPLE_GET_URL);
|
||||
request.setConfig(requestConfig);
|
||||
|
||||
httpClient.execute(request,
|
||||
response -> {
|
||||
assertThat(response.getCode()).isEqualTo(200);
|
||||
return response;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenLowSocketTimeOut_whenExecutingRequestWithTimeout_thenException() throws IOException {
|
||||
ConnectionConfig connConfig = ConnectionConfig.custom()
|
||||
.setConnectTimeout(1000, TimeUnit.MILLISECONDS)
|
||||
.setSocketTimeout(20, TimeUnit.MILLISECONDS)
|
||||
.build();
|
||||
|
||||
RequestConfig requestConfig = RequestConfig.custom()
|
||||
.setConnectionRequestTimeout(Timeout.ofMilliseconds(2000L))
|
||||
.build();
|
||||
|
||||
BasicHttpClientConnectionManager cm = new BasicHttpClientConnectionManager();
|
||||
cm.setConnectionConfig(connConfig);
|
||||
|
||||
CloseableHttpClient httpClient = HttpClientBuilder.create()
|
||||
.setDefaultRequestConfig(requestConfig)
|
||||
.setConnectionManager(cm)
|
||||
.build();
|
||||
|
||||
HttpGet request = new HttpGet(SAMPLE_GET_URL);
|
||||
|
||||
assertThrows(SocketTimeoutException.class, () -> {
|
||||
httpClient.execute(request, resp -> resp);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenExecutingPostRequest_thenNoExceptions() throws IOException {
|
||||
final HttpPost httpPost = new HttpPost(SAMPLE_POST_URL);
|
||||
httpClient.execute(httpPost,
|
||||
response -> {
|
||||
assertThat(response.getCode()).isEqualTo(200);
|
||||
return response;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenParametersAddedToRequest_thenCorrect() throws IOException {
|
||||
final HttpPost httpPost = new HttpPost(SAMPLE_POST_URL);
|
||||
final List<NameValuePair> params = new ArrayList<NameValuePair>();
|
||||
params.add(new BasicNameValuePair("key1", "value1"));
|
||||
params.add(new BasicNameValuePair("key2", "value2"));
|
||||
httpPost.setEntity(new UrlEncodedFormEntity(params, Charset.defaultCharset()));
|
||||
|
||||
httpClient.execute(httpPost, response -> {
|
||||
assertThat(response.getCode()).isEqualTo(200);
|
||||
return response;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() throws IOException {
|
||||
CloseableHttpClient client = HttpClientBuilder.create()
|
||||
.disableRedirectHandling()
|
||||
.build();
|
||||
client.execute(new HttpGet("http://t.co/I5YYd9tddw"), response -> {
|
||||
assertThat(response.getCode()).isEqualTo(301);
|
||||
return response;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenHeadersAddedToRequest_thenCorrect() throws IOException {
|
||||
HttpGet request = new HttpGet(SAMPLE_GET_URL);
|
||||
request.addHeader(HttpHeaders.ACCEPT, "application/xml");
|
||||
httpClient.execute(request,
|
||||
response -> {
|
||||
assertThat(response.getCode()).isEqualTo(200);
|
||||
return response;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCorrect() throws IOException {
|
||||
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
|
||||
httpClient.execute(httpGet,
|
||||
response -> {
|
||||
Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
|
||||
assertThat(headers, not(emptyArray()));
|
||||
return response;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAutoClosableClient_thenCorrect() throws IOException {
|
||||
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
httpClient.execute(httpGet, resp -> {
|
||||
assertThat(resp.getCode()).isEqualTo(200);
|
||||
return resp;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+7
-10
@@ -9,16 +9,12 @@ import java.util.concurrent.Future;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
|
||||
import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
|
||||
import org.apache.hc.client5.http.auth.AuthScope;
|
||||
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.cookie.BasicCookieStore;
|
||||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
|
||||
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
|
||||
@@ -26,9 +22,9 @@ import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
|
||||
import org.apache.hc.client5.http.impl.cookie.BasicClientCookie;
|
||||
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
|
||||
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
|
||||
import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner;
|
||||
import org.apache.hc.client5.http.protocol.HttpClientContext;
|
||||
import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
|
||||
import org.apache.hc.core5.concurrent.FutureCallback;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.HttpResponse;
|
||||
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
|
||||
@@ -37,6 +33,8 @@ import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
import org.apache.hc.core5.reactor.IOReactorConfig;
|
||||
import org.apache.hc.core5.ssl.SSLContexts;
|
||||
import org.apache.hc.core5.ssl.TrustStrategy;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
class HttpAsyncClientLiveTest extends GetRequestMockServer {
|
||||
@@ -56,12 +54,10 @@ class HttpAsyncClientLiveTest extends GetRequestMockServer {
|
||||
|
||||
@Test
|
||||
void whenUseHttpAsyncClient_thenCorrect() throws InterruptedException, ExecutionException, IOException {
|
||||
final HttpHost target = new HttpHost(HOST_WITH_COOKIE);
|
||||
final SimpleHttpRequest request = SimpleRequestBuilder.get()
|
||||
.setHttpHost(target)
|
||||
final SimpleHttpRequest request = SimpleRequestBuilder.get(HOST_WITH_COOKIE)
|
||||
.build();
|
||||
final CloseableHttpAsyncClient client = HttpAsyncClients.custom()
|
||||
.build();
|
||||
|
||||
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().build();
|
||||
client.start();
|
||||
|
||||
|
||||
@@ -126,6 +122,7 @@ class HttpAsyncClientLiveTest extends GetRequestMockServer {
|
||||
.build();
|
||||
|
||||
final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create()
|
||||
.setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
|
||||
.setSslContext(sslContext)
|
||||
.build();
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.httpclient;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
public final class ClientUtil {
|
||||
|
||||
private ClientUtil(){}
|
||||
|
||||
public static void closeClient(CloseableHttpClient client) throws IOException {
|
||||
if (client == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
+184
@@ -0,0 +1,184 @@
|
||||
package com.baeldung.httpclient;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import static org.hamcrest.Matchers.emptyArray;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpHeaders;
|
||||
import org.apache.http.NameValuePair;
|
||||
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
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.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
class HttpClientCookBookV4LiveTest {
|
||||
|
||||
private static final String SAMPLE_GET_URL = "http://www.google.com";
|
||||
private static final String SAMPLE_POST_URL = "http://www.github.com";
|
||||
|
||||
private CloseableHttpClient client;
|
||||
|
||||
private CloseableHttpResponse response;
|
||||
|
||||
@BeforeEach
|
||||
public final void before() {
|
||||
client = HttpClientBuilder.create().build();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public final void after() throws IllegalStateException, IOException {
|
||||
ResponseUtil.closeResponse(response);
|
||||
ClientUtil.closeClient(client);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenGetRequestExecuted_thenCorrectStatusCode() throws IOException {
|
||||
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
|
||||
response = client.execute(httpGet);
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenGetRequestExecuted_thenCorrectContentMimeType() throws IOException {
|
||||
CloseableHttpResponse response = client.execute(new HttpGet(SAMPLE_GET_URL));
|
||||
String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();
|
||||
assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenGetRequestExecuted_thenCorrectResponse() throws IOException {
|
||||
CloseableHttpResponse response = client.execute(new HttpGet(SAMPLE_GET_URL));
|
||||
String bodyAsString = EntityUtils.toString(response.getEntity());
|
||||
assertThat(bodyAsString, notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenLowSocketTimeOut_whenExecutingRequestWithTimeout_thenException() throws IOException {
|
||||
RequestConfig requestConfig = RequestConfig.custom()
|
||||
.setConnectionRequestTimeout(1000)
|
||||
.setConnectTimeout(1000)
|
||||
.setSocketTimeout(20)
|
||||
.build();
|
||||
HttpGet request = new HttpGet(SAMPLE_POST_URL);
|
||||
request.setConfig(requestConfig);
|
||||
|
||||
assertThrows(SocketTimeoutException.class, () -> {
|
||||
response = client.execute(request);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenLowSocketTimeOut_whenSettingTimeoutOnTheClient_thenException(){
|
||||
RequestConfig requestConfig = RequestConfig.custom()
|
||||
.setConnectionRequestTimeout(1000)
|
||||
.setConnectTimeout(1000)
|
||||
.setSocketTimeout(20).build();
|
||||
HttpClientBuilder builder = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig);
|
||||
|
||||
client = builder.build();
|
||||
HttpGet request = new HttpGet(SAMPLE_GET_URL);
|
||||
|
||||
assertThrows(SocketTimeoutException.class, () -> {
|
||||
response = client.execute(request);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenExecutingPostRequest_thenNoExceptions() throws IOException {
|
||||
response = client.execute(new HttpPost(SAMPLE_POST_URL));
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenParametersAddedToRequest_thenCorrect() throws IOException {
|
||||
final HttpPost httpPost = new HttpPost(SAMPLE_POST_URL);
|
||||
final List<NameValuePair> params = new ArrayList<NameValuePair>();
|
||||
params.add(new BasicNameValuePair("key1", "value1"));
|
||||
params.add(new BasicNameValuePair("key2", "value2"));
|
||||
httpPost.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
|
||||
|
||||
response = client.execute(new HttpPost(SAMPLE_POST_URL));
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() throws IOException {
|
||||
CloseableHttpClient client = HttpClientBuilder.create()
|
||||
.disableRedirectHandling()
|
||||
.build();
|
||||
CloseableHttpResponse response = client.execute(new HttpGet("http://t.co/I5YYd9tddw"));
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenHeadersAddedToRequest_thenCorrect() throws IOException {
|
||||
HttpGet request = new HttpGet(SAMPLE_GET_URL);
|
||||
request.addHeader(HttpHeaders.ACCEPT, "application/xml");
|
||||
response = client.execute(request);
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCorrect() throws IOException {
|
||||
CloseableHttpResponse response = client.execute(new HttpGet(SAMPLE_GET_URL));
|
||||
Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
|
||||
assertThat(headers, not(emptyArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStreamIsClosed_thenCloseResponse() throws IOException {
|
||||
response = client.execute(new HttpGet(SAMPLE_GET_URL));
|
||||
try {
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
InputStream instream = entity.getContent();
|
||||
instream.close();
|
||||
}
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAutoClosableClient_thenCorrect() throws IOException {
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
||||
// handle response;
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
try (InputStream instream = entity.getContent()) {
|
||||
// Process the input stream if needed
|
||||
}
|
||||
}
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,3 +11,4 @@ You can build the project from the command line using: *mvn clean install*, or i
|
||||
- [Get Last N Messages in Apache Kafka Topic](https://www.baeldung.com/java-apache-kafka-get-last-n-messages)
|
||||
- [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)
|
||||
- [Get Partition Count for a Topic in Kafka](https://www.baeldung.com/java-kafka-partition-count-topic)
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@Testcontainers
|
||||
public class KafaConsumeLastNMessages {
|
||||
public class KafaConsumeLastNMessagesLiveTest {
|
||||
|
||||
private static String TOPIC1 = "baeldung-github";
|
||||
private static String TOPIC2 = "baeldung-blog";
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.baeldung.kafka;
|
||||
|
||||
import org.apache.kafka.clients.admin.AdminClient;
|
||||
import org.apache.kafka.clients.admin.DescribeTopicsResult;
|
||||
import org.apache.kafka.clients.admin.TopicDescription;
|
||||
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||
import org.apache.kafka.clients.producer.ProducerConfig;
|
||||
import org.apache.kafka.common.KafkaFuture;
|
||||
import org.apache.kafka.common.PartitionInfo;
|
||||
import org.apache.kafka.common.serialization.StringSerializer;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.containers.KafkaContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
@Testcontainers
|
||||
public class KafkaCountPartitionsLiveTest {
|
||||
@Container
|
||||
private static final KafkaContainer KAFKA_CONTAINER = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:latest"));
|
||||
private final static String TOPIC = "baeldung-kafka-github";
|
||||
private final static Integer PARTITION_NUMBER = 3;
|
||||
private static KafkaProducer<String, String> producer;
|
||||
|
||||
@BeforeAll
|
||||
static void setup() throws IOException, InterruptedException {
|
||||
KAFKA_CONTAINER.addExposedPort(9092);
|
||||
|
||||
KAFKA_CONTAINER.execInContainer(
|
||||
"/bin/sh",
|
||||
"-c",
|
||||
"/usr/bin/kafka-topics " +
|
||||
"--bootstrap-server localhost:9092 " +
|
||||
"--create " +
|
||||
"--replication-factor 1 " +
|
||||
"--partitions " + PARTITION_NUMBER + " " +
|
||||
"--topic " + TOPIC
|
||||
);
|
||||
|
||||
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());
|
||||
|
||||
producer = new KafkaProducer<>(producerProperties);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void destroy() {
|
||||
KAFKA_CONTAINER.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPartitionsForTopic_isEqualToActualNumberAssignedDuringCreation() {
|
||||
List<PartitionInfo> info = producer.partitionsFor(TOPIC);
|
||||
Assertions.assertEquals(PARTITION_NUMBER, info.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTopicPartitionDescription_isEqualToActualNumberAssignedDuringCreation() {
|
||||
Properties props = new Properties();
|
||||
props.put("bootstrap.servers", KAFKA_CONTAINER.getBootstrapServers());
|
||||
props.put("client.id","java-admin-client");
|
||||
props.put("request.timeout.ms", 3000);
|
||||
props.put("connections.max.idle.ms", 5000);
|
||||
try(AdminClient client = AdminClient.create(props)){
|
||||
DescribeTopicsResult describeTopicsResult = client.describeTopics(Collections.singletonList(TOPIC));
|
||||
Map<String, KafkaFuture<TopicDescription>> values = describeTopicsResult.values();
|
||||
KafkaFuture<TopicDescription> topicDescription = values.get(TOPIC);
|
||||
Assertions.assertEquals(PARTITION_NUMBER, topicDescription.get().partitions().size());
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
## Relevant Articles
|
||||
- [Listing All AWS S3 Objects in a Bucket Using Java](https://www.baeldung.com/java-aws-s3-list-bucket-objects)
|
||||
@@ -0,0 +1,50 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.s3</groupId>
|
||||
<artifactId>aws-s3-v2</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>aws-s3-v2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>aws-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<aws.java.sdk.version>2.20.52</aws.java.sdk.version>
|
||||
<maven.compiler.plugin.version>3.6.1</maven.compiler.plugin.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>s3</artifactId>
|
||||
<version>${aws.java.sdk.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>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.s3.listobjects;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class File {
|
||||
private String name;
|
||||
private ByteBuffer content;
|
||||
|
||||
public File(String name, ByteBuffer content) {
|
||||
this.name = name;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public ByteBuffer getContent() {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.s3.listobjects;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class FileGenerator {
|
||||
|
||||
public static List<File> generateFiles(int fileCount, int fileSize) {
|
||||
List<File> files = new ArrayList<>();
|
||||
for (int i = 0; i < fileCount; i++) {
|
||||
String fileName = "file_" + i + ".txt";
|
||||
ByteBuffer fileContent = generateRandomBytes(fileSize);
|
||||
files.add(new File(fileName, fileContent));
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
private static ByteBuffer generateRandomBytes(int size) {
|
||||
byte[] array = new byte[size];
|
||||
new Random().nextBytes(array);
|
||||
return ByteBuffer.wrap(array);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.s3.listobjects;
|
||||
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
|
||||
public class Main {
|
||||
|
||||
private static String AWS_BUCKET = "baeldung-tutorial-s3";
|
||||
public static Region AWS_REGION = Region.EU_CENTRAL_1;
|
||||
|
||||
public static void main(String[] args) {
|
||||
S3Service s3Service = new S3Service(AWS_REGION);
|
||||
|
||||
s3Service.putObject(AWS_BUCKET, FileGenerator.generateFiles(1000, 1));
|
||||
s3Service.listBuckets();
|
||||
s3Service.listObjectsInBucket(AWS_BUCKET);
|
||||
s3Service.listAllObjectsInBucket(AWS_BUCKET);
|
||||
s3Service.listAllObjectsInBucketPaginated(AWS_BUCKET, 500);
|
||||
s3Service.listAllObjectsInBucketPaginatedWithPrefix(AWS_BUCKET, 500, "backup/");
|
||||
|
||||
s3Service.cleanup();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package com.baeldung.s3.listobjects;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.*;
|
||||
import software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable;
|
||||
|
||||
class S3Service {
|
||||
|
||||
private S3Client s3Client;
|
||||
|
||||
public S3Service(Region awsRegion) {
|
||||
init(awsRegion);
|
||||
}
|
||||
|
||||
public S3Service(S3Client s3Client) {
|
||||
this.s3Client = s3Client;
|
||||
}
|
||||
|
||||
public void init(Region awsRegion) {
|
||||
this.s3Client = S3Client.builder()
|
||||
.region(awsRegion)
|
||||
.credentialsProvider(ProfileCredentialsProvider.create("default"))
|
||||
.build();
|
||||
}
|
||||
|
||||
public void listObjectsInBucket(String bucketName) {
|
||||
ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder()
|
||||
.bucket(bucketName)
|
||||
.build();
|
||||
ListObjectsV2Response listObjectsV2Response = s3Client.listObjectsV2(listObjectsV2Request);
|
||||
|
||||
List<S3Object> contents = listObjectsV2Response.contents();
|
||||
|
||||
System.out.println("Number of objects in the bucket: " + contents.stream().count());
|
||||
contents.stream().forEach(System.out::println);
|
||||
}
|
||||
|
||||
public void listAllObjectsInBucket(String bucketName) {
|
||||
String nextContinuationToken = null;
|
||||
long totalObjects = 0;
|
||||
|
||||
do {
|
||||
ListObjectsV2Request.Builder requestBuilder = ListObjectsV2Request.builder()
|
||||
.bucket(bucketName)
|
||||
.continuationToken(nextContinuationToken);
|
||||
|
||||
ListObjectsV2Response response = s3Client.listObjectsV2(requestBuilder.build());
|
||||
nextContinuationToken = response.nextContinuationToken();
|
||||
|
||||
totalObjects += response.contents().stream()
|
||||
.peek(System.out::println)
|
||||
.reduce(0, (subtotal, element) -> subtotal + 1, Integer::sum);
|
||||
} while (nextContinuationToken != null);
|
||||
System.out.println("Number of objects in the bucket: " + totalObjects);
|
||||
}
|
||||
|
||||
public void listAllObjectsInBucketPaginated(String bucketName, int pageSize) {
|
||||
ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder()
|
||||
.bucket(bucketName)
|
||||
.maxKeys(pageSize) // Set the maxKeys parameter to control the page size
|
||||
.build();
|
||||
|
||||
ListObjectsV2Iterable listObjectsV2Iterable = s3Client.listObjectsV2Paginator(listObjectsV2Request);
|
||||
long totalObjects = 0;
|
||||
|
||||
for (ListObjectsV2Response page : listObjectsV2Iterable) {
|
||||
long retrievedPageSize = page.contents().stream()
|
||||
.peek(System.out::println)
|
||||
.reduce(0, (subtotal, element) -> subtotal + 1, Integer::sum);
|
||||
totalObjects += retrievedPageSize;
|
||||
System.out.println("Page size: " + retrievedPageSize);
|
||||
}
|
||||
System.out.println("Total objects in the bucket: " + totalObjects);
|
||||
}
|
||||
|
||||
public void listAllObjectsInBucketPaginatedWithPrefix(String bucketName, int pageSize, String prefix) {
|
||||
ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder()
|
||||
.bucket(bucketName)
|
||||
.maxKeys(pageSize) // Set the maxKeys parameter to control the page size
|
||||
.prefix(prefix)
|
||||
.build();
|
||||
|
||||
ListObjectsV2Iterable listObjectsV2Iterable = s3Client.listObjectsV2Paginator(listObjectsV2Request);
|
||||
long totalObjects = 0;
|
||||
|
||||
for (ListObjectsV2Response page : listObjectsV2Iterable) {
|
||||
long retrievedPageSize = page.contents().stream()
|
||||
.peek(System.out::println)
|
||||
.reduce(0, (subtotal, element) -> subtotal + 1, Integer::sum);
|
||||
totalObjects += retrievedPageSize;
|
||||
System.out.println("Page size: " + retrievedPageSize);
|
||||
}
|
||||
System.out.println("Total objects in the bucket: " + totalObjects);
|
||||
}
|
||||
|
||||
public void listBuckets() {
|
||||
// List all buckets
|
||||
ListBucketsResponse listBucketsResponse = s3Client.listBuckets();
|
||||
|
||||
// Display the bucket names
|
||||
List<Bucket> buckets = listBucketsResponse.buckets();
|
||||
System.out.println("Buckets:");
|
||||
for (Bucket bucket : buckets) {
|
||||
System.out.println(bucket.name());
|
||||
}
|
||||
}
|
||||
|
||||
public void putObject(String bucketName, List<File> files) {
|
||||
try {
|
||||
files.stream().forEach(file -> {
|
||||
s3Client.putObject(PutObjectRequest.builder().bucket(bucketName).key(file.getName()).build(),
|
||||
RequestBody.fromByteBuffer(file.getContent()));
|
||||
System.out.println("Uploaded file: " + file.getName());
|
||||
});
|
||||
} catch (S3Exception e) {
|
||||
System.err.println("Upload failed");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
this.s3Client.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
<!-- Change the log level for software.amazon.awssdk -->
|
||||
<logger name="software.amazon.awssdk" level="info" />
|
||||
</configuration>
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
package com.baeldung.s3.listobjects;
|
||||
|
||||
import com.baeldung.s3.listobjects.S3Service;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
|
||||
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
|
||||
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
|
||||
import software.amazon.awssdk.services.s3.model.S3Object;
|
||||
import software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class S3ServiceLiveTest {
|
||||
|
||||
@Mock
|
||||
private S3Client s3Client;
|
||||
|
||||
private S3Service s3Service;
|
||||
|
||||
private String AWS_BUCKET = "baeldung-tutorial-s3";
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
s3Service = new S3Service(s3Client);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void cleanup() {
|
||||
s3Service.cleanup();
|
||||
Mockito.verify(s3Client).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBucketName_whenListObjectsInBucket_thenReturnList() {
|
||||
ListObjectsV2Request request = ListObjectsV2Request.builder().bucket(AWS_BUCKET).build();
|
||||
ListObjectsV2Response response = ListObjectsV2Response.builder().contents(Collections.emptyList()).build();
|
||||
|
||||
when(s3Client.listObjectsV2(request)).thenReturn(response);
|
||||
|
||||
s3Service.listObjectsInBucket(AWS_BUCKET);
|
||||
Mockito.verify(s3Client).listObjectsV2(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBucketName_whenListAllObjectsInBucket_thenReturnList() {
|
||||
ListObjectsV2Request request = ListObjectsV2Request.builder().bucket(AWS_BUCKET).build();
|
||||
ListObjectsV2Response response = ListObjectsV2Response.builder().contents(Collections.emptyList()).build();
|
||||
|
||||
when(s3Client.listObjectsV2(request)).thenReturn(response);
|
||||
|
||||
s3Service.listAllObjectsInBucket(AWS_BUCKET);
|
||||
Mockito.verify(s3Client).listObjectsV2(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBucketNameAndPageSize_whenListAllObjectsInBucketPaginated_thenReturnPaginatedList() {
|
||||
int pageSize = 10;
|
||||
ListObjectsV2Request request = ListObjectsV2Request.builder().bucket(AWS_BUCKET).maxKeys(pageSize).build();
|
||||
|
||||
ListObjectsV2Iterable mockIterable = Mockito.mock(ListObjectsV2Iterable.class);
|
||||
|
||||
S3Object s3Object1 = S3Object.builder().key("object1").build();
|
||||
S3Object s3Object2 = S3Object.builder().key("object2").build();
|
||||
ListObjectsV2Response response = ListObjectsV2Response.builder().contents(s3Object1, s3Object2).build();
|
||||
|
||||
when(s3Client.listObjectsV2Paginator(request)).thenReturn(mockIterable);
|
||||
when(mockIterable.iterator()).thenReturn(Arrays.asList(response).iterator());
|
||||
|
||||
s3Service.listAllObjectsInBucketPaginated(AWS_BUCKET, pageSize);
|
||||
Mockito.verify(s3Client).listObjectsV2Paginator(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBucketNamePageSizeAndPrefix_whenListAllObjectsInBucketPaginatedWithPrefix_thenReturnPaginatedList() {
|
||||
int pageSize = 1;
|
||||
String prefix = "folder/";
|
||||
ListObjectsV2Request request = ListObjectsV2Request.builder().bucket(AWS_BUCKET).maxKeys(pageSize).prefix(prefix).build();
|
||||
|
||||
ListObjectsV2Iterable mockIterable = Mockito.mock(ListObjectsV2Iterable.class);
|
||||
|
||||
S3Object s3Object1 = S3Object.builder().key("folder/object1").build();
|
||||
S3Object s3Object2 = S3Object.builder().key("folder/object2").build();
|
||||
ListObjectsV2Response response = ListObjectsV2Response.builder().contents(s3Object1, s3Object2).build();
|
||||
|
||||
when(s3Client.listObjectsV2Paginator(request)).thenReturn(mockIterable);
|
||||
when(mockIterable.iterator()).thenReturn(Arrays.asList(response).iterator());
|
||||
|
||||
s3Service.listAllObjectsInBucketPaginatedWithPrefix(AWS_BUCKET, pageSize, prefix);
|
||||
Mockito.verify(s3Client).listObjectsV2Paginator(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenListBuckets_thenReturnBucketList() {
|
||||
when(s3Client.listBuckets()).thenReturn(ListBucketsResponse.builder().buckets(Collections.emptyList()).build());
|
||||
|
||||
s3Service.listBuckets();
|
||||
Mockito.verify(s3Client).listBuckets();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.baeldung.s3;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
@@ -49,6 +50,11 @@ public class AWSS3Service {
|
||||
public PutObjectResult putObject(String bucketName, String key, File file) {
|
||||
return s3client.putObject(bucketName, key, file);
|
||||
}
|
||||
//uploading object and getting url
|
||||
public URL getObjectURL(String bucketName, String key, File file) {
|
||||
s3client.putObject(bucketName, key, file);
|
||||
return s3client.getUrl(bucketName, key);
|
||||
}
|
||||
|
||||
//listing objects
|
||||
public ObjectListing listObjects(String bucketName) {
|
||||
|
||||
@@ -66,6 +66,9 @@ public class S3Application {
|
||||
new File("/Users/user/Document/hello.txt")
|
||||
);
|
||||
|
||||
//uploading object and getting url
|
||||
awsService.getObjectURL(bucketName, "Document/hello.txt", new File("/Users/user/Document/hello.txt"));
|
||||
|
||||
//listing objects
|
||||
ObjectListing objectListing = awsService.listObjects(bucketName);
|
||||
for(S3ObjectSummary os : objectListing.getObjectSummaries()) {
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
<module>aws-miscellaneous</module>
|
||||
<module>aws-reactive</module>
|
||||
<module>aws-s3</module>
|
||||
<module>aws-s3-v2</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -2,3 +2,4 @@
|
||||
- [Record Patterns in Java 19](https://www.baeldung.com/java-19-record-patterns)
|
||||
- [Structured Concurrency in Java 19](https://www.baeldung.com/java-structured-concurrency)
|
||||
- [Possible Root Causes for High CPU Usage in Java](https://www.baeldung.com/java-high-cpu-usage-causes)
|
||||
- [The Vector API in Java 19](https://www.baeldung.com/java-vector-api)
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
<source>19</source>
|
||||
<target>19</target>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
<compilerArgs>--add-modules=jdk.incubator.vector</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package com.baeldung.vectors;
|
||||
|
||||
import jdk.incubator.vector.FloatVector;
|
||||
import jdk.incubator.vector.IntVector;
|
||||
import jdk.incubator.vector.VectorOperators;
|
||||
import jdk.incubator.vector.VectorSpecies;
|
||||
|
||||
/**
|
||||
* This class contains examples of the Vector API in Java, more specifically the fourth incubator to the Vector API.
|
||||
* There are scalar and vector methods written here and the method names will specify the type.
|
||||
* Note that, the code is written without any benchmarking done and without any assumption of it being run on
|
||||
* a SIMD processor, or any specific compiler.
|
||||
*/
|
||||
public class VectorAPIExamples {
|
||||
|
||||
static final VectorSpecies<Integer> SPECIES = IntVector.SPECIES_PREFERRED;
|
||||
static final VectorSpecies<Float> PREFERRED_SPECIES = FloatVector.SPECIES_PREFERRED;
|
||||
|
||||
public int[] addTwoScalarArrays(int[] arr1, int[] arr2) {
|
||||
int[] result = new int[arr1.length];
|
||||
for (int i = 0; i < arr1.length; i++) {
|
||||
result[i] = arr1[i] + arr2[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public int[] addTwoVectorArrays(int[] arr1, int[] arr2) {
|
||||
var v1 = IntVector.fromArray(SPECIES, arr1, 0);
|
||||
var v2 = IntVector.fromArray(SPECIES, arr2, 0);
|
||||
var result = v1.add(v2);
|
||||
return result.toArray();
|
||||
}
|
||||
|
||||
public int[] addTwoVectorsWIthHigherShape(int[] arr1, int[] arr2) {
|
||||
int[] finalResult = new int[arr1.length];
|
||||
for (int i = 0; i < arr1.length; i += SPECIES.length()) {
|
||||
var v1 = IntVector.fromArray(SPECIES, arr1, i);
|
||||
var v2 = IntVector.fromArray(SPECIES, arr2, i);
|
||||
var result = v1.add(v2);
|
||||
result.intoArray(finalResult, i);
|
||||
}
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
public int[] addTwoVectorsWithMasks(int[] arr1, int[] arr2) {
|
||||
int[] finalResult = new int[arr1.length];
|
||||
int i = 0;
|
||||
for (; i < SPECIES.loopBound(arr1.length); i += SPECIES.length()) {
|
||||
var mask = SPECIES.indexInRange(i, arr1.length);
|
||||
var v1 = IntVector.fromArray(SPECIES, arr1, i, mask);
|
||||
var v2 = IntVector.fromArray(SPECIES, arr2, i, mask);
|
||||
var result = v1.add(v2, mask);
|
||||
result.intoArray(finalResult, i, mask);
|
||||
}
|
||||
|
||||
// tail cleanup loop
|
||||
for (; i < arr1.length; i++) {
|
||||
finalResult[i] = arr1[i] + arr2[i];
|
||||
}
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
public float[] scalarNormOfTwoArrays(float[] arr1, float[] arr2) {
|
||||
float[] finalResult = new float[arr1.length];
|
||||
for (int i = 0; i < arr1.length; i++) {
|
||||
finalResult[i] = (arr1[i] * arr1[i] + arr2[i] * arr2[i]) * -1.0f;
|
||||
}
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
public float[] vectorNormalForm(float[] arr1, float[] arr2) {
|
||||
float[] finalResult = new float[arr1.length];
|
||||
int i = 0;
|
||||
int upperBound = SPECIES.loopBound(arr1.length);
|
||||
for (; i < upperBound; i += SPECIES.length()) {
|
||||
var va = FloatVector.fromArray(PREFERRED_SPECIES, arr1, i);
|
||||
var vb = FloatVector.fromArray(PREFERRED_SPECIES, arr2, i);
|
||||
var vc = va.mul(va)
|
||||
.add(vb.mul(vb))
|
||||
.neg();
|
||||
vc.intoArray(finalResult, i);
|
||||
}
|
||||
|
||||
// tail cleanup
|
||||
for (; i < arr1.length; i++) {
|
||||
finalResult[i] = (arr1[i] * arr1[i] + arr2[i] * arr2[i]) * -1.0f;
|
||||
}
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
public double averageOfaVector(int[] arr) {
|
||||
double sum = 0;
|
||||
for (int i = 0; i < arr.length; i += SPECIES.length()) {
|
||||
var mask = SPECIES.indexInRange(i, arr.length);
|
||||
var V = IntVector.fromArray(SPECIES, arr, i, mask);
|
||||
sum += V.reduceLanes(VectorOperators.ADD, mask);
|
||||
}
|
||||
return sum / arr.length;
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.vectors;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
||||
public class VectorAPIUnitTest {
|
||||
VectorAPIExamples vector = new VectorAPIExamples();
|
||||
|
||||
@Test
|
||||
public void whenTwoArraysProvided_thenVerifyScalarAdd() {
|
||||
int[] arr1 = { 1, 2, 3, 4 };
|
||||
int[] arr2 = { 100, 200, 300, 400 };
|
||||
int[] result = { 101, 202, 303, 404 };
|
||||
assertArrayEquals(result, vector.addTwoScalarArrays(arr1, arr2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTwoArraysProvided_thenVerifyVectorAdd() {
|
||||
int[] arr1 = { 1, 2, 3, 4 };
|
||||
int[] arr2 = { 100, 200, 300, 400 };
|
||||
int[] result = { 101, 202, 303, 404 };
|
||||
assertArrayEquals(result, vector.addTwoVectorsWithMasks(arr1, arr2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTwoValuesProvided_thenComputeScalarNorm() {
|
||||
float[] arr1 = { 1, 2.3f };
|
||||
float[] arr2 = { 1.3f, 2.0f };
|
||||
float[] result = { -2.6899998f, -9.29f };
|
||||
Assertions.assertArrayEquals(result, vector.scalarNormOfTwoArrays(arr1, arr2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTwoValuesProvided_thenComputeVectorNorm() {
|
||||
float[] arr1 = { 1, 2.3f };
|
||||
float[] arr2 = { 1.3f, 2.0f };
|
||||
float[] result = { -2.6899998f, -9.29f };
|
||||
Assertions.assertArrayEquals(result, vector.vectorNormalForm(arr1, arr2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenArrayProvided_thenComputeVectorAverage() {
|
||||
int[] arr = { 100, 200, 300, 400 };
|
||||
Assertions.assertEquals(250.0, vector.averageOfaVector(arr));
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.zipentries;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Scanner;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
public class ZipEntryReader {
|
||||
|
||||
public static void readZipEntries(String filePath) throws IOException {
|
||||
try (ZipFile zipFile = new ZipFile(filePath)) {
|
||||
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = entries.nextElement();
|
||||
// Check if entry is a directory
|
||||
if (!entry.isDirectory()) {
|
||||
// Read and process the entry contents using the inputStream
|
||||
try (InputStream inputStream = zipFile.getInputStream(entry);
|
||||
Scanner scanner = new Scanner(inputStream);) {
|
||||
while (scanner.hasNextLine()) {
|
||||
String line = scanner.nextLine();
|
||||
System.out.println(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.zipentries;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class ZipEntryReaderUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenZipFile_thenReadEntriesAndValidateContent() throws URISyntaxException, IOException {
|
||||
Path zipFilePath = Paths.get(getClass().getClassLoader().getResource("zipFile.zip").toURI());
|
||||
ZipEntryReader.readZipEntries(zipFilePath.toString());
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -3,4 +3,6 @@
|
||||
- [Generating Random Dates in Java](https://www.baeldung.com/java-random-dates)
|
||||
- [Creating a LocalDate with Values in Java](https://www.baeldung.com/java-creating-localdate-with-values)
|
||||
- [Parsing Date Strings with Varying Formats](https://www.baeldung.com/java-parsing-dates-many-formats)
|
||||
- [How Many Days Are There in a Particular Month of a Given Year?](https://www.baeldung.com/days-particular-month-given-year)
|
||||
- [Difference Between Instant and LocalDateTime](https://www.baeldung.com/java-instant-vs-localdatetime)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-datetime-java8-1)
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.instantvslocaldatetime;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class InstantAndLocalDateTimeUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoInstants_whenComparing_thenDifferent() {
|
||||
|
||||
// Instant arithmetic
|
||||
Instant.now().plus(12, ChronoUnit.HOURS);
|
||||
Instant oneWeekAgo = Instant.now().minus(7, ChronoUnit.DAYS);
|
||||
|
||||
// Comparing 2 Instants
|
||||
Instant instant1 = Instant.now();
|
||||
Instant instant2 = instant1.plus(1, ChronoUnit.SECONDS);
|
||||
assertTrue(instant1.isBefore(instant2));
|
||||
assertFalse(instant1.isAfter(instant2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLocalDateTimes_whenComparing_thenDifferent() {
|
||||
|
||||
// LocalDateTime arithmetic
|
||||
LocalDateTime tomorrow = LocalDateTime.now().plus(1, ChronoUnit.DAYS);
|
||||
LocalDateTime oneYearAgo = LocalDateTime.now().minus(1, ChronoUnit.YEARS);
|
||||
|
||||
// Comparing 2 LocalDateTimes
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
LocalDateTime y2k = LocalDateTime.of(2000, 1, 1, 0, 0);
|
||||
assertTrue(now.isAfter(y2k));
|
||||
assertTrue(y2k.isBefore(now));
|
||||
}
|
||||
}
|
||||
@@ -7,3 +7,4 @@ This module contains articles about arrays conversion in Java
|
||||
- [Converting Between Stream and Array in Java](https://www.baeldung.com/java-stream-to-array)
|
||||
- [Convert a Byte Array to a Numeric Representation in Java](https://www.baeldung.com/java-byte-array-to-number)
|
||||
- [Converting a String Array Into an int Array in Java](https://www.baeldung.com/java-convert-string-array-to-int-array)
|
||||
- [Convert Java Array to Iterable](https://www.baeldung.com/java-array-convert-to-iterable)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user