Search in sources :

Example 11 with ZeebeClient

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

the class TaskSubscriptionTest method shouldReceiveTasksFromMultiplePartitions.

@Test
public void shouldReceiveTasksFromMultiplePartitions() {
    // given
    final String topicName = "gyros";
    final int numPartitions = 2;
    final ZeebeClient client = clientRule.getClient();
    client.topics().create(topicName, numPartitions).execute();
    final Topics topics = client.topics().getTopics().execute();
    final Topic topic = topics.getTopics().stream().filter(t -> t.getName().equals(topicName)).findFirst().get();
    final Integer[] partitionIds = topic.getPartitions().stream().mapToInt(p -> p.getId()).boxed().toArray(Integer[]::new);
    final String taskType = "foooo";
    final RecordingTaskHandler handler = new RecordingTaskHandler();
    createTaskOnPartition(client, topicName, partitionIds[0], taskType);
    createTaskOnPartition(client, topicName, partitionIds[1], taskType);
    // when
    client.tasks().newTaskSubscription(topicName).handler(handler).lockOwner("foo").lockTime(Duration.ofSeconds(30)).taskType(taskType).open();
    // then
    waitUntil(() -> handler.getHandledTasks().size() == 2);
    final Integer[] receivedPartitionIds = handler.getHandledTasks().stream().map(t -> t.getMetadata().getPartitionId()).toArray(Integer[]::new);
    assertThat(receivedPartitionIds).containsExactlyInAnyOrder(partitionIds);
}
Also used : TaskEvent(io.zeebe.client.event.TaskEvent) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) RecordingTaskHandler(io.zeebe.broker.it.util.RecordingTaskHandler) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TaskSubscription(io.zeebe.client.task.TaskSubscription) Test(org.junit.Test) TopicEventRecorder.taskEvent(io.zeebe.broker.it.util.TopicEventRecorder.taskEvent) Instant(java.time.Instant) TestUtil.doRepeatedly(io.zeebe.test.util.TestUtil.doRepeatedly) TopicEventRecorder(io.zeebe.broker.it.util.TopicEventRecorder) ZeebeClient(io.zeebe.client.ZeebeClient) List(java.util.List) ClientRule(io.zeebe.broker.it.ClientRule) org.junit.rules(org.junit.rules) TopicEventRecorder.taskRetries(io.zeebe.broker.it.util.TopicEventRecorder.taskRetries) Rule(org.junit.Rule) Duration(java.time.Duration) CreateTaskCommandImpl(io.zeebe.client.task.impl.CreateTaskCommandImpl) Topic(io.zeebe.client.topic.Topic) EmbeddedBrokerRule(io.zeebe.broker.it.EmbeddedBrokerRule) Topics(io.zeebe.client.topic.Topics) TestUtil(io.zeebe.test.util.TestUtil) Topics(io.zeebe.client.topic.Topics) RecordingTaskHandler(io.zeebe.broker.it.util.RecordingTaskHandler) ZeebeClient(io.zeebe.client.ZeebeClient) Topic(io.zeebe.client.topic.Topic) Test(org.junit.Test)

Example 12 with ZeebeClient

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

the class TopologyViewer method main.

public static void main(final String[] args) {
    final String[] brokers = new String[] { "localhost:51015", "localhost:41015", "localhost:31015" };
    for (final String broker : brokers) {
        final Properties clientProperties = new Properties();
        clientProperties.put(ClientProperties.BROKER_CONTACTPOINT, broker);
        try (ZeebeClient zeebeClient = ZeebeClient.create(clientProperties)) {
            final TopologyResponse topology = zeebeClient.requestTopology().execute();
            System.out.println("Requesting topology with initial contact point " + broker);
            System.out.println("  Topology:");
            topology.getBrokers().forEach(b -> {
                System.out.println("    " + b.getSocketAddress());
                b.getPartitions().forEach(p -> System.out.println("      " + p.getTopicName() + "." + p.getPartitionId() + " - " + p.getState()));
            });
        } catch (final Exception e) {
            System.out.println("Broker " + broker + " not available");
        }
    }
}
Also used : ZeebeClient(io.zeebe.client.ZeebeClient) TopologyResponse(io.zeebe.client.clustering.impl.TopologyResponse) Properties(java.util.Properties) ClientProperties(io.zeebe.client.ClientProperties)

Example 13 with ZeebeClient

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

the class WorkflowInstanceStarter method main.

public static void main(String[] args) {
    final String brokerContactPoint = "127.0.0.1:51015";
    final String bpmnProcessId = "demoProcess";
    final String topicName = "default-topic";
    final int partitionId = 0;
    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("> Deploying workflow to topic '%s' and partition '%d'", topicName, partitionId));
    final DeploymentEvent deploymentResult = zeebeClient.workflows().deploy(topicName).addResourceFromClasspath("demoProcess.bpmn").execute();
    try {
        final String deployedWorkflows = deploymentResult.getDeployedWorkflows().stream().map(wf -> String.format("<%s:%d>", wf.getBpmnProcessId(), wf.getVersion())).collect(Collectors.joining(","));
        System.out.println(String.format("> Deployed: %s", deployedWorkflows));
        System.out.println(String.format("> Create workflow instance for workflow: %s", bpmnProcessId));
        zeebeClient.workflows().create(topicName).bpmnProcessId(bpmnProcessId).payload("{\"a\": \"b\"}").execute();
        System.out.println("> Created.");
    } catch (ClientCommandRejectedException exception) {
        System.out.println(String.format("> Fail to deploy: %s", exception.getMessage()));
    }
    System.out.println("> Closing...");
    zeebeClient.close();
    System.out.println("> Closed.");
}
Also used : Properties(java.util.Properties) ClientProperties(io.zeebe.client.ClientProperties) ClientCommandRejectedException(io.zeebe.client.cmd.ClientCommandRejectedException) DeploymentEvent(io.zeebe.client.event.DeploymentEvent) Collectors(java.util.stream.Collectors) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) ZeebeClient(io.zeebe.client.ZeebeClient) ZeebeClient(io.zeebe.client.ZeebeClient) ClientCommandRejectedException(io.zeebe.client.cmd.ClientCommandRejectedException) Properties(java.util.Properties) ClientProperties(io.zeebe.client.ClientProperties) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) DeploymentEvent(io.zeebe.client.event.DeploymentEvent)

Example 14 with ZeebeClient

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

the class RequestTopicsTest method shouldRequestTopics.

@Test
public void shouldRequestTopics() {
    // given
    final List<Map<String, Object>> partitions = new ArrayList<>();
    partitions.add(buildPartition(1, "foo"));
    partitions.add(buildPartition(2, "foo"));
    partitions.add(buildPartition(3, "bar"));
    brokerRule.onControlMessageRequest(r -> r.messageType() == ControlMessageType.REQUEST_PARTITIONS && r.partitionId() == Protocol.SYSTEM_PARTITION).respondWith().data().put("partitions", partitions).done().register();
    final ZeebeClient client = clientRule.getClient();
    // when
    final Topics result = client.topics().getTopics().execute();
    // then
    final List<Topic> returnedTopics = result.getTopics();
    assertThat(returnedTopics).hasSize(2);
    final Map<String, List<Partition>> topicsByName = returnedTopics.stream().collect(Collectors.toMap(Topic::getName, Topic::getPartitions));
    assertThat(topicsByName.get("foo")).hasSize(2).areExactly(1, matching(1, "foo")).areExactly(1, matching(2, "foo"));
    assertThat(topicsByName.get("bar")).hasSize(1).areExactly(1, matching(3, "bar"));
    final List<ControlMessageRequest> partitionRequests = brokerRule.getReceivedControlMessageRequests().stream().filter(r -> r.messageType() == ControlMessageType.REQUEST_PARTITIONS).collect(Collectors.toList());
    assertThat(partitionRequests).hasSize(1);
    final ControlMessageRequest request = partitionRequests.get(0);
    assertThat(request.partitionId()).isEqualTo(Protocol.SYSTEM_PARTITION);
}
Also used : Predicate(java.util.function.Predicate) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) HashMap(java.util.HashMap) Test(org.junit.Test) Protocol(io.zeebe.protocol.Protocol) Collectors(java.util.stream.Collectors) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) ArrayList(java.util.ArrayList) ZeebeClient(io.zeebe.client.ZeebeClient) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) RuleChain(org.junit.rules.RuleChain) List(java.util.List) Rule(org.junit.Rule) ClientRule(io.zeebe.client.util.ClientRule) Map(java.util.Map) Condition(org.assertj.core.api.Condition) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) ArrayList(java.util.ArrayList) ZeebeClient(io.zeebe.client.ZeebeClient) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 15 with ZeebeClient

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

the class ClientConfigurationTest method shouldConfigureKeepAlive.

@Test
public void shouldConfigureKeepAlive() {
    // given
    final Properties props = new Properties();
    props.put(ClientProperties.CLIENT_TCP_CHANNEL_KEEP_ALIVE_PERIOD, Long.toString(KEEP_ALIVE_TIMEOUT));
    final Duration expectedTimeout = Duration.ofMillis(KEEP_ALIVE_TIMEOUT);
    // when
    final ZeebeClient client = ZeebeClient.create(props);
    // then
    final ClientTransport transport = ((ZeebeClientImpl) client).getTransport();
    assertThat(transport.getChannelKeepAlivePeriod()).isEqualTo(expectedTimeout);
}
Also used : ZeebeClient(io.zeebe.client.ZeebeClient) Duration(java.time.Duration) Properties(java.util.Properties) ClientProperties(io.zeebe.client.ClientProperties) ClientTransport(io.zeebe.transport.ClientTransport) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) Test(org.junit.Test)

Aggregations

ZeebeClient (io.zeebe.client.ZeebeClient)22 Test (org.junit.Test)15 ClientProperties (io.zeebe.client.ClientProperties)8 Properties (java.util.Properties)7 ZeebeClientImpl (io.zeebe.client.impl.ZeebeClientImpl)5 TaskEvent (io.zeebe.client.event.TaskEvent)4 TopologyBroker (io.zeebe.client.clustering.impl.TopologyBroker)3 TopologyResponse (io.zeebe.client.clustering.impl.TopologyResponse)3 Duration (java.time.Duration)3 Scanner (java.util.Scanner)3 BrokerPartitionState (io.zeebe.client.clustering.impl.BrokerPartitionState)2 ClientCommandRejectedException (io.zeebe.client.cmd.ClientCommandRejectedException)2 TaskSubscription (io.zeebe.client.task.TaskSubscription)2 Topic (io.zeebe.client.topic.Topic)2 Topics (io.zeebe.client.topic.Topics)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Rule (org.junit.Rule)2 ClientRule (io.zeebe.broker.it.ClientRule)1