Search in sources :

Example 1 with TaskSubscription

use of io.zeebe.client.task.TaskSubscription in project zeebe by zeebe-io.

the class BrokerRestartTest method shouldReceiveLockExpiredTasksAfterRestart.

@Test
public void shouldReceiveLockExpiredTasksAfterRestart() {
    // given
    clientRule.tasks().create(clientRule.getDefaultTopic(), "foo").execute();
    final RecordingTaskHandler recordingTaskHandler = new RecordingTaskHandler();
    final TaskSubscription subscription = clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopic()).taskType("foo").lockTime(Duration.ofSeconds(5)).lockOwner("test").handler(recordingTaskHandler).open();
    waitUntil(() -> !recordingTaskHandler.getHandledTasks().isEmpty());
    subscription.close();
    // when
    restartBroker();
    doRepeatedly(() -> {
        // retriggers lock expiration check in broker
        brokerRule.getClock().addTime(Duration.ofSeconds(60));
        return null;
    }).until(t -> eventRecorder.hasTaskEvent(taskEvent("LOCK_EXPIRED")));
    recordingTaskHandler.clear();
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopic()).taskType("foo").lockTime(Duration.ofMinutes(10L)).lockOwner("test").handler(recordingTaskHandler).open();
    // then
    waitUntil(() -> !recordingTaskHandler.getHandledTasks().isEmpty());
    final TaskEvent task = recordingTaskHandler.getHandledTasks().get(0);
    clientRule.tasks().complete(task).execute();
    waitUntil(() -> eventRecorder.hasTaskEvent(taskEvent("COMPLETED")));
}
Also used : RecordingTaskHandler(io.zeebe.broker.it.util.RecordingTaskHandler) TaskSubscription(io.zeebe.client.task.TaskSubscription) TaskEvent(io.zeebe.client.event.TaskEvent) Test(org.junit.Test)

Example 2 with TaskSubscription

use of io.zeebe.client.task.TaskSubscription in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldCloseSubscriptionOnChannelClose.

@Test
public void shouldCloseSubscriptionOnChannelClose() throws InterruptedException {
    // given
    broker.stubTaskSubscriptionApi(123L);
    final TaskSubscription subscription = clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler((c, t) -> c.complete(t).withoutPayload().execute()).lockOwner("owner").lockTime(1000L).taskFetchSize(5).taskType("foo").open();
    // when
    broker.closeTransport();
    // let subscriber attempt reopening
    Thread.sleep(500L);
    // make request time out immediately
    clientRule.getClock().addTime(Duration.ofSeconds(60));
    // then
    TestUtil.waitUntil(() -> subscription.isClosed());
    assertThat(subscription.isClosed()).isTrue();
}
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) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) TaskSubscription(io.zeebe.client.task.TaskSubscription) Test(org.junit.Test)

Example 3 with TaskSubscription

use of io.zeebe.client.task.TaskSubscription 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)

Example 4 with TaskSubscription

use of io.zeebe.client.task.TaskSubscription in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldCloseSubscription.

@Test
public void shouldCloseSubscription() throws InterruptedException {
    // given
    eventRecorder.startRecordingEvents();
    final RecordingTaskHandler taskHandler = new RecordingTaskHandler();
    final TaskSubscription subscription = clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopic()).handler(taskHandler).taskType("foo").lockTime(Duration.ofMinutes(5)).lockOwner("test").open();
    // when
    subscription.close();
    // then
    clientRule.tasks().create(clientRule.getDefaultTopic(), "foo").execute();
    waitUntil(() -> eventRecorder.hasTaskEvent(taskEvent("CREATED")));
    assertThat(taskHandler.getHandledTasks()).isEmpty();
    assertThat(eventRecorder.hasTaskEvent(taskEvent("LOCK"))).isFalse();
}
Also used : RecordingTaskHandler(io.zeebe.broker.it.util.RecordingTaskHandler) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) TaskSubscription(io.zeebe.client.task.TaskSubscription) Test(org.junit.Test)

Example 5 with TaskSubscription

use of io.zeebe.client.task.TaskSubscription in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldCloseSubscription.

@Test
public void shouldCloseSubscription() {
    // given
    broker.stubTaskSubscriptionApi(123L);
    final TaskSubscription subscription = clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(DO_NOTHING).lockOwner("foo").lockTime(10000L).taskType("bar").open();
    // when
    subscription.close();
    // then
    assertThat(subscription.isClosed()).isTrue();
    assertThat(subscription.isOpen()).isFalse();
    final ControlMessageRequest subscriptionRequest = getUnsubscribeRequests().findFirst().get();
    assertThat(subscriptionRequest.messageType()).isEqualByComparingTo(ControlMessageType.REMOVE_TASK_SUBSCRIPTION);
    assertThat(subscriptionRequest.partitionId()).isEqualTo(clientRule.getDefaultPartitionId());
    assertThat(subscriptionRequest.getData()).contains(entry("subscriberKey", 123));
}
Also used : ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) TaskSubscription(io.zeebe.client.task.TaskSubscription) Test(org.junit.Test)

Aggregations

TaskSubscription (io.zeebe.client.task.TaskSubscription)7 Test (org.junit.Test)6 PollableTaskSubscription (io.zeebe.client.task.PollableTaskSubscription)5 ControlMessageRequest (io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest)4 RecordingTaskHandler (io.zeebe.broker.it.util.RecordingTaskHandler)2 ClientProperties (io.zeebe.client.ClientProperties)2 ZeebeClient (io.zeebe.client.ZeebeClient)2 TaskEvent (io.zeebe.client.event.TaskEvent)2 ZeebeClientImpl (io.zeebe.client.impl.ZeebeClientImpl)2 Duration (java.time.Duration)2 Properties (java.util.Properties)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 TasksClientImpl (io.zeebe.client.impl.TasksClientImpl)1 MsgPackConverter (io.zeebe.client.impl.data.MsgPackConverter)1 TaskHandler (io.zeebe.client.task.TaskHandler)1 Subscriber (io.zeebe.client.task.impl.subscription.Subscriber)1 ClientRule (io.zeebe.client.util.ClientRule)1