Search in sources :

Example 1 with ClientProperties

use of io.zeebe.client.ClientProperties 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 ClientProperties

use of io.zeebe.client.ClientProperties in project zeebe by zeebe-io.

the class CreateTopic method main.

public static void main(final String[] args) {
    final String broker = "localhost:51015";
    final String topic = "default-topic";
    final int partitions = 1;
    final Properties clientProperties = new Properties();
    clientProperties.put(ClientProperties.BROKER_CONTACTPOINT, broker);
    try (ZeebeClient client = ZeebeClient.create(clientProperties)) {
        System.out.println("Creating topic " + topic + " with " + partitions + " partition(s) with contact point " + broker);
        System.out.println(client.topics().create(topic, partitions).execute().getState());
    }
}
Also used : ZeebeClient(io.zeebe.client.ZeebeClient) Properties(java.util.Properties) ClientProperties(io.zeebe.client.ClientProperties)

Example 3 with ClientProperties

use of io.zeebe.client.ClientProperties in project zeebe by zeebe-io.

the class TypedEventLogger 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 Consumer<Event> logger = e -> {
        System.out.println(e.getMetadata());
        System.out.println(e);
        System.out.println();
    };
    final TopicSubscription subscription = zeebeClient.topics().newSubscription(topicName).name("logger").startAtHeadOfTopic().forcedStart().workflowEventHandler(logger::accept).workflowInstanceEventHandler(logger::accept).taskEventHandler(logger::accept).incidentEventHandler(logger::accept).open();
    // 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 : Consumer(java.util.function.Consumer) Properties(java.util.Properties) ClientProperties(io.zeebe.client.ClientProperties) Scanner(java.util.Scanner) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) io.zeebe.client.event(io.zeebe.client.event) ZeebeClient(io.zeebe.client.ZeebeClient) Scanner(java.util.Scanner) ZeebeClient(io.zeebe.client.ZeebeClient) Properties(java.util.Properties) ClientProperties(io.zeebe.client.ClientProperties) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl)

Example 4 with ClientProperties

use of io.zeebe.client.ClientProperties in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldRetryWithMoreTasksThanSubscriptionCapacity.

/**
 * This tests a case that should not occur under normal circumstances, but might occur
 * in case of inconsistencies between broker and client state (e.g. due to bugs in either of them)
 */
@Test
public void shouldRetryWithMoreTasksThanSubscriptionCapacity() throws InterruptedException {
    // given
    broker.stubTaskSubscriptionApi(123L);
    broker.onExecuteCommandRequest(EventType.TASK_EVENT, "COMPLETE").respondWith().key((r) -> r.key()).event().allOf((r) -> r.getCommand()).put("state", "COMPLETED").done().register();
    final WaitingTaskHandler handler = new WaitingTaskHandler();
    final Properties clientProperties = ((ZeebeClientImpl) client).getInitializationProperties();
    final int numExecutionThreads = Integer.parseInt(clientProperties.getProperty(ClientProperties.CLIENT_SUBSCRIPTION_EXECUTION_THREADS));
    final int taskCapacity = 4;
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(handler).lockOwner("owner").lockTime(1000L).taskFetchSize(taskCapacity).taskType("foo").open();
    final RemoteAddress clientAddress = broker.getReceivedControlMessageRequests().get(0).getSource();
    for (int i = 0; i < taskCapacity + numExecutionThreads; i++) {
        broker.pushLockedTask(clientAddress, 123L, i, i, "owner", "foo");
    }
    TestUtil.waitUntil(() -> handler.numWaitingThreads.get() > 0);
    // pushing one more event, exceeding client capacity
    broker.pushLockedTask(clientAddress, 123L, Integer.MAX_VALUE, Integer.MAX_VALUE, "owner", "foo");
    // waiting for the client to receive all pending tasks
    Thread.sleep(500L);
    // when
    handler.shouldWait = false;
    continueTaskHandlingThreads();
    // then the additional event is handled nevertheless (i.e. client applies backpressure)
    TestUtil.waitUntil(() -> handler.numHandledEvents.get() == taskCapacity + numExecutionThreads + 1);
}
Also used : TasksClient(io.zeebe.client.TasksClient) TaskEvent(io.zeebe.client.event.TaskEvent) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) HashMap(java.util.HashMap) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) ZeebeClient(io.zeebe.client.ZeebeClient) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) ErrorCode(io.zeebe.protocol.clientapi.ErrorCode) RemoteAddress(io.zeebe.transport.RemoteAddress) ClientRule(io.zeebe.client.util.ClientRule) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) ExpectedException(org.junit.rules.ExpectedException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) MsgPackConverter(io.zeebe.client.impl.data.MsgPackConverter) Before(org.junit.Before) MsgPackHelper(io.zeebe.test.broker.protocol.MsgPackHelper) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Properties(java.util.Properties) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Test(org.junit.Test) TaskSubscription(io.zeebe.client.task.TaskSubscription) Instant(java.time.Instant) Assertions.entry(org.assertj.core.api.Assertions.entry) Collectors(java.util.stream.Collectors) TasksClientImpl(io.zeebe.client.impl.TasksClientImpl) SubscriptionType(io.zeebe.protocol.clientapi.SubscriptionType) TimeUnit(java.util.concurrent.TimeUnit) RuleChain(org.junit.rules.RuleChain) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) EventType(io.zeebe.protocol.clientapi.EventType) ClientProperties(io.zeebe.client.ClientProperties) TaskHandler(io.zeebe.client.task.TaskHandler) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Subscriber(io.zeebe.client.task.impl.subscription.Subscriber) TestUtil(io.zeebe.test.util.TestUtil) RemoteAddress(io.zeebe.transport.RemoteAddress) Properties(java.util.Properties) ClientProperties(io.zeebe.client.ClientProperties) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) Test(org.junit.Test)

Example 5 with ClientProperties

use of io.zeebe.client.ClientProperties in project zeebe by zeebe-io.

the class WorkflowInstanceWorker method main.

public static void main(String[] args) {
    final String brokerContactPoint = "127.0.0.1:51015";
    final String topicName = "default-topic";
    final int partitionId = 0;
    final String taskType = "foo";
    final String lockOwner = "worker-1";
    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));
    System.out.println(String.format("> Open task subscription for topic '%s', partition '%d' and type '%s'", topicName, partitionId, taskType));
    final TaskSubscription subscription = zeebeClient.tasks().newTaskSubscription(topicName).taskType(taskType).lockOwner(lockOwner).lockTime(Duration.ofSeconds(10)).handler((client, task) -> {
        System.out.println(String.format(">>> [type: %s, key: %s, lockExpirationTime: %s]\n[headers: %s]\n[payload: %s]\n===", task.getType(), task.getMetadata().getKey(), task.getLockExpirationTime().toString(), task.getHeaders(), task.getPayload()));
        client.complete(task).withoutPayload().execute();
    }).open();
    System.out.println("> Opened.");
    // wait for tasks
    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) Duration(java.time.Duration) ClientProperties(io.zeebe.client.ClientProperties) Scanner(java.util.Scanner) TaskSubscription(io.zeebe.client.task.TaskSubscription) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) ZeebeClient(io.zeebe.client.ZeebeClient) Scanner(java.util.Scanner) TaskSubscription(io.zeebe.client.task.TaskSubscription) ZeebeClient(io.zeebe.client.ZeebeClient) Properties(java.util.Properties) ClientProperties(io.zeebe.client.ClientProperties) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl)

Aggregations

ClientProperties (io.zeebe.client.ClientProperties)7 ZeebeClient (io.zeebe.client.ZeebeClient)7 Properties (java.util.Properties)7 ZeebeClientImpl (io.zeebe.client.impl.ZeebeClientImpl)5 Scanner (java.util.Scanner)3 TaskSubscription (io.zeebe.client.task.TaskSubscription)2 Duration (java.time.Duration)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 TasksClient (io.zeebe.client.TasksClient)1 TopologyResponse (io.zeebe.client.clustering.impl.TopologyResponse)1 ClientCommandRejectedException (io.zeebe.client.cmd.ClientCommandRejectedException)1 io.zeebe.client.event (io.zeebe.client.event)1 DeploymentEvent (io.zeebe.client.event.DeploymentEvent)1 EventMetadata (io.zeebe.client.event.EventMetadata)1 TaskEvent (io.zeebe.client.event.TaskEvent)1 TopicSubscription (io.zeebe.client.event.TopicSubscription)1 TasksClientImpl (io.zeebe.client.impl.TasksClientImpl)1 MsgPackConverter (io.zeebe.client.impl.data.MsgPackConverter)1