Search in sources :

Example 1 with TasksClient

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

the class NonBlockingTaskCreator method main.

public static void main(String[] args) {
    final int tasks = 1_000_000;
    final Properties properties = System.getProperties();
    ClientProperties.setDefaults(properties);
    properties.putIfAbsent(SAMPLE_NUMBER_OF_REQUESTS, String.valueOf(tasks));
    properties.putIfAbsent(SAMPLE_MAX_CONCURRENT_REQUESTS, "128");
    properties.put(ClientProperties.CLIENT_MAXREQUESTS, "128");
    printProperties(properties);
    final int numOfRequests = Integer.parseInt(properties.getProperty(SAMPLE_NUMBER_OF_REQUESTS));
    final int maxConcurrentRequests = Integer.parseInt(properties.getProperty(SAMPLE_MAX_CONCURRENT_REQUESTS));
    final String topicName = "default-topic";
    try (ZeebeClient client = ZeebeClient.create(properties)) {
        try {
            // try to create default topic if it not exists already
            client.topics().create(topicName, 4).execute();
        } catch (final ClientCommandRejectedException e) {
        // topic already exists
        }
        final TasksClient asyncTaskService = client.tasks();
        final String payload = "{}";
        final long time = System.currentTimeMillis();
        long tasksCreated = 0;
        final List<Future<TaskEvent>> inFlightRequests = new LinkedList<>();
        while (tasksCreated < numOfRequests) {
            if (inFlightRequests.size() < maxConcurrentRequests) {
                final CreateTaskCommand cmd = asyncTaskService.create(topicName, "greeting").addCustomHeader("some", "value").payload(payload);
                inFlightRequests.add(cmd.executeAsync());
                tasksCreated++;
            }
            poll(inFlightRequests);
        }
        awaitAll(inFlightRequests);
        System.out.println("Took: " + (System.currentTimeMillis() - time));
    }
}
Also used : TasksClient(io.zeebe.client.TasksClient) ZeebeClient(io.zeebe.client.ZeebeClient) CreateTaskCommand(io.zeebe.client.task.cmd.CreateTaskCommand) ClientCommandRejectedException(io.zeebe.client.cmd.ClientCommandRejectedException) Future(java.util.concurrent.Future) ClientProperties(io.zeebe.client.ClientProperties)

Aggregations

ClientProperties (io.zeebe.client.ClientProperties)1 TasksClient (io.zeebe.client.TasksClient)1 ZeebeClient (io.zeebe.client.ZeebeClient)1 ClientCommandRejectedException (io.zeebe.client.cmd.ClientCommandRejectedException)1 CreateTaskCommand (io.zeebe.client.task.cmd.CreateTaskCommand)1 Future (java.util.concurrent.Future)1