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")));
}
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();
}
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);
}
}
}
}
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();
}
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));
}
Aggregations