use of io.zeebe.client.event.TopicSubscription in project zeebe by zeebe-io.
the class EventLogger method main.
public static void main(String[] args) {
final String brokerContactPoint = "127.0.0.1:51015";
final Properties clientProperties = new Properties();
clientProperties.put(ClientProperties.BROKER_CONTACTPOINT, brokerContactPoint);
final ZeebeClient zeebeClient = new ZeebeClientImpl(clientProperties);
System.out.println(String.format("> Connecting to %s", brokerContactPoint));
final String topicName = "default-topic";
System.out.println(String.format("> Open event subscription from topic '%s'", topicName));
final TopicSubscription subscription = zeebeClient.topics().newSubscription(topicName).startAtHeadOfTopic().forcedStart().name("logger").handler(event -> {
final EventMetadata metadata = event.getMetadata();
System.out.println(String.format(">>> [topic: %d, position: %d, key: %d, type: %s]\n%s\n===", metadata.getPartitionId(), metadata.getPosition(), metadata.getKey(), metadata.getType(), event.getJson()));
}).open();
System.out.println("> Opened.");
// wait for events
try (Scanner scanner = new Scanner(System.in)) {
while (scanner.hasNextLine()) {
final String nextLine = scanner.nextLine();
if (nextLine.contains("exit")) {
System.out.println("> Closing...");
subscription.close();
zeebeClient.close();
System.out.println("> Closed.");
System.exit(0);
}
}
}
}
use of io.zeebe.client.event.TopicSubscription in project zeebe by zeebe-io.
the class PersistentTopicSubscriptionTest method shouldResumeSubscriptionOnRestart.
@Test
public void shouldResumeSubscriptionOnRestart() {
// given a first task
clientRule.tasks().create(clientRule.getDefaultTopic(), "foo").addCustomHeader("key", "value").payload("{}").execute();
final String subscriptionName = "foo";
final TopicSubscription subscription = clientRule.topics().newSubscription(clientRule.getDefaultTopic()).handler(recordingHandler).name(subscriptionName).startAtHeadOfTopic().open();
// that was received by the subscription
TestUtil.waitUntil(() -> recordingHandler.numRecordedTaskEvents() == 2);
subscription.close();
final long lastEventPosition = recordingHandler.getRecordedEvents().get(recordingHandler.numRecordedEvents() - 1).getMetadata().getPosition();
recordingHandler.reset();
// and a second not-yet-received task
clientRule.tasks().create(clientRule.getDefaultTopic(), "foo").addCustomHeader("key", "value").payload("{}").execute();
// when
restartBroker();
clientRule.topics().newSubscription(clientRule.getDefaultTopic()).handler(recordingHandler).name(subscriptionName).startAtHeadOfTopic().open();
// then
TestUtil.waitUntil(() -> recordingHandler.numRecordedEvents() > 0);
final long firstEventPositionAfterReopen = recordingHandler.getRecordedEvents().get(0).getMetadata().getPosition();
assertThat(firstEventPositionAfterReopen).isGreaterThan(lastEventPosition);
}
use of io.zeebe.client.event.TopicSubscription in project zeebe by zeebe-io.
the class ZeebeClientTest method shouldCloseAllConnectionsOnClose.
@Test
public void shouldCloseAllConnectionsOnClose() throws Exception {
// given
final ServerTransport serverTransport = broker.getTransport();
final TopicSubscription subscription = openSubscription();
final LoggingChannelListener channelListener = new LoggingChannelListener();
serverTransport.registerChannelListener(channelListener).join();
// when
client.close();
// then
assertThat(subscription.isClosed()).isTrue();
// listener invocation on close is asynchronous
waitUntil(() -> channelListener.connectionState.size() == 1);
assertThat(channelListener.connectionState).containsExactly(ConnectionState.CLOSED);
}
use of io.zeebe.client.event.TopicSubscription in project zeebe by zeebe-io.
the class ZeebeClientTest method shouldEstablishNewConnectionsAfterDisconnect.
@Test
public void shouldEstablishNewConnectionsAfterDisconnect() {
// given
final ClientTransport clientTransport = client.getTransport();
// ensuring an open connection
client.requestTopology().execute();
final LoggingChannelListener channelListener = new LoggingChannelListener();
clientTransport.registerChannelListener(channelListener).join();
// when
broker.closeTransport();
System.out.println("Broker transport closed");
broker.bindTransport();
System.out.println("Broker transport bound");
// then
final TopicSubscription newSubscription = openSubscription();
assertThat(newSubscription.isOpen()).isTrue();
// listener invocation is asynchronous
waitUntil(() -> channelListener.connectionState.contains(ConnectionState.CONNECTED));
assertThat(channelListener.connectionState).last().isSameAs(ConnectionState.CONNECTED);
}
Aggregations