Search in sources :

Example 1 with TopicSubscription

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);
            }
        }
    }
}
Also used : Properties(java.util.Properties) ClientProperties(io.zeebe.client.ClientProperties) Scanner(java.util.Scanner) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) TopicSubscription(io.zeebe.client.event.TopicSubscription) ZeebeClient(io.zeebe.client.ZeebeClient) EventMetadata(io.zeebe.client.event.EventMetadata) Scanner(java.util.Scanner) TopicSubscription(io.zeebe.client.event.TopicSubscription) ZeebeClient(io.zeebe.client.ZeebeClient) Properties(java.util.Properties) ClientProperties(io.zeebe.client.ClientProperties) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) EventMetadata(io.zeebe.client.event.EventMetadata)

Example 2 with TopicSubscription

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);
}
Also used : TopicSubscription(io.zeebe.client.event.TopicSubscription) Test(org.junit.Test) BrokerRestartTest(io.zeebe.broker.it.startup.BrokerRestartTest)

Example 3 with TopicSubscription

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);
}
Also used : TopicSubscription(io.zeebe.client.event.TopicSubscription) ServerTransport(io.zeebe.transport.ServerTransport) Test(org.junit.Test)

Example 4 with TopicSubscription

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);
}
Also used : TopicSubscription(io.zeebe.client.event.TopicSubscription) ClientTransport(io.zeebe.transport.ClientTransport) Test(org.junit.Test)

Aggregations

TopicSubscription (io.zeebe.client.event.TopicSubscription)4 Test (org.junit.Test)3 BrokerRestartTest (io.zeebe.broker.it.startup.BrokerRestartTest)1 ClientProperties (io.zeebe.client.ClientProperties)1 ZeebeClient (io.zeebe.client.ZeebeClient)1 EventMetadata (io.zeebe.client.event.EventMetadata)1 ZeebeClientImpl (io.zeebe.client.impl.ZeebeClientImpl)1 ClientTransport (io.zeebe.transport.ClientTransport)1 ServerTransport (io.zeebe.transport.ServerTransport)1 Properties (java.util.Properties)1 Scanner (java.util.Scanner)1