Skip to content

Commit 3596d3d

Browse files
committed
Added project for Apache pulsar examples
1 parent 44a3d36 commit 3596d3d

10 files changed

Lines changed: 536 additions & 0 deletions

File tree

‎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/build.gradle‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apply plugin: 'java'
2+
ext{
3+
pulsarVersion = '2.1.1-incubating'
4+
}
5+
6+
repositories {
7+
jcenter()
8+
mavenCentral()
9+
mavenLocal()
10+
}
11+
12+
sourceCompatibility = 1.8
13+
targetCompatibility = 1.8
14+
15+
group = 'com.baeldung.pulsar'
16+
archivesBaseName = 'pulsar-java-example'
17+
version = '0.0.1'
18+
19+
20+
21+
22+
dependencies {
23+
compile group: 'org.apache.pulsar', name: 'pulsar-client', version: pulsarVersion
24+
25+
}
26+
53.1 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

‎apache-pulsar/gradlew‎

Lines changed: 172 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎apache-pulsar/gradlew.bat‎

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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+
}

0 commit comments

Comments
 (0)