Skip to content

Commit b3d4ed5

Browse files
committed
Merge remote-tracking branch 'eugenp/master'
2 parents 38e9149 + 5140bd6 commit b3d4ed5

487 files changed

Lines changed: 3563 additions & 1228 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,13 @@ To do the full build, do: `mvn install -Pdefault -Dgib.enabled=false`
2626
Building a single module
2727
====================
2828
To build a specific module run the command: `mvn clean install -Dgib.enabled=false` in the module directory
29+
30+
31+
Running a Spring Boot module
32+
====================
33+
To run a Spring Boot module run the command: `mvn spring-boot:run -Dgib.enabled=false` in the module directory
34+
35+
36+
37+
38+

‎algorithms/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@
3131
- [Round Up to the Nearest Hundred](https://www.baeldung.com/java-round-up-nearest-hundred)
3232
- [Merge Sort in Java](https://www.baeldung.com/java-merge-sort)
3333
- [Calculate Percentage in Java](https://www.baeldung.com/java-calculate-percentage)
34+
- [Quicksort Algorithm Implementation in Java](https://www.baeldung.com/java-quicksort)
35+
- [Insertion Sort in Java](https://www.baeldung.com/java-insertion-sort)
36+
- [Converting Between Byte Arrays and Hexadecimal Strings in Java](https://www.baeldung.com/java-byte-arrays-hex-strings)

‎algorithms/src/main/java/com/baeldung/algorithms/bubblesort/BubbleSort.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class BubbleSort {
77
void bubbleSort(Integer[] arr) {
88
int n = arr.length;
99
IntStream.range(0, n - 1)
10-
.flatMap(i -> IntStream.range(i + 1, n - i))
10+
.flatMap(i -> IntStream.range(1, n - i))
1111
.forEach(j -> {
1212
if (arr[j - 1] > arr[j]) {
1313
int temp = arr[j];

‎algorithms/src/test/java/com/baeldung/algorithms/bubblesort/BubbleSortUnitTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.baeldung.algorithms.bubblesort;
22

3-
import static org.junit.Assert.*;
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
44

5-
import org.junit.Test;
5+
import org.junit.jupiter.api.Test;
66

77
public class BubbleSortUnitTest {
88

‎apache-geode/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Relevant Articles:
2+
3+
- [A Quick Guide to Apache Geode](https://www.baeldung.com/apache-geode)

‎apache-pulsar/.gitignore‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.classpath
2+
.project
3+
.settings
4+
target
5+
.idea
6+
*.iml
7+
.gradle/
8+
build/

‎apache-pulsar/pom.xml‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung.pulsar</groupId>
6+
<artifactId>pulsar-java</artifactId>
7+
<version>0.0.1</version>
8+
9+
<dependencies>
10+
<dependency>
11+
<groupId>org.apache.pulsar</groupId>
12+
<artifactId>pulsar-client</artifactId>
13+
<version>2.1.1-incubating</version>
14+
<scope>compile</scope>
15+
</dependency>
16+
</dependencies>
17+
<properties>
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
<maven.compiler.source>1.8</maven.compiler.source>
20+
</properties>
21+
</project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung;
2+
3+
import java.io.IOException;
4+
5+
import org.apache.pulsar.client.api.Consumer;
6+
import org.apache.pulsar.client.api.Message;
7+
import org.apache.pulsar.client.api.PulsarClient;
8+
import org.apache.pulsar.client.api.SubscriptionType;
9+
10+
public class ConsumerTest {
11+
12+
private static final String SERVICE_URL = "pulsar://localhost:6650";
13+
private static final String TOPIC_NAME = "test-topic";
14+
private static final String SUBSCRIPTION_NAME = "test-subscription";
15+
16+
public static void main(String[] args) throws IOException {
17+
// Create a Pulsar client instance. A single instance can be shared across many
18+
// producers and consumer within the same application
19+
PulsarClient client = PulsarClient.builder()
20+
.serviceUrl(SERVICE_URL)
21+
.build();
22+
23+
//Configure consumer specific settings.
24+
Consumer<byte[]> consumer = client.newConsumer()
25+
.topic(TOPIC_NAME)
26+
// Allow multiple consumers to attach to the same subscription
27+
// and get messages dispatched as a queue
28+
.subscriptionType(SubscriptionType.Shared)
29+
.subscriptionName(SUBSCRIPTION_NAME)
30+
.subscribe();
31+
32+
33+
// Once the consumer is created, it can be used for the entire application lifecycle
34+
System.out.println("Created consumer for the topic "+ TOPIC_NAME);
35+
36+
do {
37+
// Wait until a message is available
38+
Message<byte[]> msg = consumer.receive();
39+
40+
// Extract the message as a printable string and then log
41+
String content = new String(msg.getData());
42+
System.out.println("Received message '"+content+"' with ID "+msg.getMessageId());
43+
44+
// Acknowledge processing of the message so that it can be deleted
45+
consumer.acknowledge(msg);
46+
} while (true);
47+
}
48+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung;
2+
3+
import org.apache.pulsar.client.api.CompressionType;
4+
import org.apache.pulsar.client.api.Message;
5+
import org.apache.pulsar.client.api.MessageBuilder;
6+
import org.apache.pulsar.client.api.MessageId;
7+
import org.apache.pulsar.client.api.Producer;
8+
import org.apache.pulsar.client.api.PulsarClient;
9+
import org.apache.pulsar.client.api.PulsarClientException;
10+
11+
import java.io.IOException;
12+
import java.util.stream.IntStream;
13+
14+
public class ProducerTest {
15+
16+
private static final String SERVICE_URL = "pulsar://localhost:6650";
17+
private static final String TOPIC_NAME = "test-topic";
18+
19+
public static void main(String[] args) throws IOException {
20+
// Create a Pulsar client instance. A single instance can be shared across many
21+
// producers and consumer within the same application
22+
PulsarClient client = PulsarClient.builder()
23+
.serviceUrl(SERVICE_URL)
24+
.build();
25+
26+
// Configure producer specific settings
27+
Producer<byte[]> producer = client.newProducer()
28+
// Set the topic
29+
.topic(TOPIC_NAME)
30+
// Enable compression
31+
.compressionType(CompressionType.LZ4)
32+
.create();
33+
34+
// Once the producer is created, it can be used for the entire application life-cycle
35+
System.out.println("Created producer for the topic "+TOPIC_NAME);
36+
37+
// Send 5 test messages
38+
IntStream.range(1, 5).forEach(i -> {
39+
String content = String.format("hi-pulsar-%d", i);
40+
41+
// Build a message object
42+
Message<byte[]> msg = MessageBuilder.create()
43+
.setContent(content.getBytes())
44+
.build();
45+
46+
// Send each message and log message content and ID when successfully received
47+
try {
48+
MessageId msgId = producer.send(msg);
49+
50+
System.out.println("Published message '"+content+"' with the ID "+msgId);
51+
} catch (PulsarClientException e) {
52+
System.out.println(e.getMessage());
53+
}
54+
});
55+
56+
client.close();
57+
}
58+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.subscriptions;
2+
3+
import org.apache.pulsar.client.api.ConsumerBuilder;
4+
import org.apache.pulsar.client.api.Message;
5+
import org.apache.pulsar.client.api.MessageBuilder;
6+
import org.apache.pulsar.client.api.Producer;
7+
import org.apache.pulsar.client.api.PulsarClient;
8+
import org.apache.pulsar.client.api.PulsarClientException;
9+
import org.apache.pulsar.client.api.SubscriptionType;
10+
11+
import java.util.stream.IntStream;
12+
13+
public class ExclusiveSubscriptionTest {
14+
private static final String SERVICE_URL = "pulsar://localhost:6650";
15+
private static final String TOPIC_NAME = "test-topic";
16+
private static final String SUBSCRIPTION_NAME = "test-subscription";
17+
private static final SubscriptionType SUBSCRIPTION_TYPE = SubscriptionType.Exclusive;
18+
19+
public static void main(String[] args) throws PulsarClientException {
20+
PulsarClient client = PulsarClient.builder()
21+
.serviceUrl(SERVICE_URL)
22+
.build();
23+
24+
Producer<byte[]> producer = client.newProducer()
25+
.topic(TOPIC_NAME)
26+
.create();
27+
28+
ConsumerBuilder<byte[]> consumer1 = client.newConsumer()
29+
.topic(TOPIC_NAME)
30+
.subscriptionName(SUBSCRIPTION_NAME)
31+
.subscriptionType(SUBSCRIPTION_TYPE);
32+
33+
ConsumerBuilder<byte[]> consumer2 = client.newConsumer()
34+
.topic(TOPIC_NAME)
35+
.subscriptionName(SUBSCRIPTION_NAME)
36+
.subscriptionType(SUBSCRIPTION_TYPE);
37+
38+
IntStream.range(0, 999).forEach(i -> {
39+
Message<byte[]> msg = MessageBuilder.create()
40+
.setContent(String.format("message-%d", i).getBytes())
41+
.build();
42+
try {
43+
producer.send(msg);
44+
} catch (PulsarClientException e) {
45+
System.out.println(e.getMessage());
46+
}
47+
});
48+
49+
// Consumer 1 can subscribe to the topic
50+
consumer1.subscribe();
51+
52+
// Consumer 2 cannot due to the exclusive subscription held by consumer 1
53+
consumer2.subscribeAsync()
54+
.handle((consumer, exception) -> {
55+
System.out.println(exception.getMessage());
56+
return null;
57+
});
58+
}
59+
}

0 commit comments

Comments
 (0)