Search in sources :

Example 1 with EventMetadata

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

use of io.zeebe.client.event.EventMetadata in project zeebe by zeebe-io.

the class RecordingEventHandler method assertTaskEvent.

public void assertTaskEvent(int index, long taskKey, String eventType) throws IOException {
    final List<GeneralEvent> taskEvents = events.stream().filter(e -> e.getMetadata().getType() == TopicEventType.TASK).collect(Collectors.toList());
    final GeneralEvent taskEvent = taskEvents.get(index);
    final EventMetadata eventMetadata = taskEvent.getMetadata();
    assertThat(eventMetadata.getType()).isEqualTo(TopicEventType.TASK);
    assertThat(eventMetadata.getKey()).isEqualTo(taskKey);
    final JsonNode event = objectMapper.readTree(taskEvent.getJson());
    assertThat(event.get("state").asText()).isEqualTo(eventType);
}
Also used : UniversalEventHandler(io.zeebe.client.event.UniversalEventHandler) List(java.util.List) GeneralEvent(io.zeebe.client.event.GeneralEvent) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) JsonNode(com.fasterxml.jackson.databind.JsonNode) Collectors(java.util.stream.Collectors) TopicEventType(io.zeebe.client.event.TopicEventType) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) EventMetadata(io.zeebe.client.event.EventMetadata) JsonNode(com.fasterxml.jackson.databind.JsonNode) GeneralEvent(io.zeebe.client.event.GeneralEvent) EventMetadata(io.zeebe.client.event.EventMetadata)

Example 3 with EventMetadata

use of io.zeebe.client.event.EventMetadata in project zeebe by zeebe-io.

the class CommandRequestHandler method serialize.

protected void serialize(EventImpl event) {
    int offset = 0;
    headerEncoder.wrap(serializedCommand, offset).blockLength(encoder.sbeBlockLength()).schemaId(encoder.sbeSchemaId()).templateId(encoder.sbeTemplateId()).version(encoder.sbeSchemaVersion());
    offset += headerEncoder.encodedLength();
    encoder.wrap(serializedCommand, offset);
    final EventMetadata metadata = event.getMetadata();
    if (metadata.getKey() < 0) {
        encoder.key(ExecuteCommandRequestEncoder.keyNullValue());
    } else {
        encoder.key(metadata.getKey());
    }
    encoder.partitionId(metadata.getPartitionId()).eventType(EventTypeMapping.mapEventType(metadata.getType())).position(metadata.getPosition());
    offset = encoder.limit();
    final int commandHeaderOffset = offset;
    final int serializedCommandOffset = commandHeaderOffset + commandHeaderLength();
    final ExpandableDirectBufferOutputStream out = new ExpandableDirectBufferOutputStream(serializedCommand, serializedCommandOffset);
    try {
        objectMapper.writeValue(out, event);
    } catch (final Throwable e) {
        throw new RuntimeException("Failed to serialize command", e);
    }
    // can only write the header after we have written the command, as we don't know the length beforehand
    final short commandLength = (short) out.position();
    serializedCommand.putShort(commandHeaderOffset, commandLength, java.nio.ByteOrder.LITTLE_ENDIAN);
    serializedCommandLength = serializedCommandOffset + out.position();
}
Also used : ExpandableDirectBufferOutputStream(org.agrona.io.ExpandableDirectBufferOutputStream) EventMetadata(io.zeebe.client.event.EventMetadata)

Example 4 with EventMetadata

use of io.zeebe.client.event.EventMetadata in project zeebe by zeebe-io.

the class SubscriptionManager method onEvent.

@Override
public boolean onEvent(SubscriptionType type, long subscriberKey, GeneralEventImpl event) {
    final EventMetadata eventMetadata = event.getMetadata();
    final EventSubscribers subscribers;
    if (type == SubscriptionType.TASK_SUBSCRIPTION) {
        subscribers = taskSubscribers;
    } else if (type == SubscriptionType.TOPIC_SUBSCRIPTION) {
        subscribers = topicSubscribers;
    } else {
        subscribers = null;
    }
    Subscriber subscriber = null;
    if (subscribers != null) {
        final int partitionId = eventMetadata.getPartitionId();
        subscriber = subscribers.getSubscriber(partitionId, subscriberKey);
        if (subscriber == null) {
            if (subscribers.isAnySubscriberOpeningOn(partitionId)) {
                // In this case, we postpone the event.
                return false;
            }
        }
    }
    if (subscriber != null && subscriber.isOpen()) {
        event.setTopicName(subscriber.getTopicName());
        return subscriber.addEvent(event);
    } else {
        LOGGER.debug("Ignoring event event {} for subscription [type={}, partition={}, key={}]", event, type, event.getMetadata().getPartitionId(), subscriberKey);
        // ignoring the event is success; don't want to retry it later
        return true;
    }
}
Also used : TopicSubscriber(io.zeebe.client.event.impl.TopicSubscriber) EventMetadata(io.zeebe.client.event.EventMetadata)

Aggregations

EventMetadata (io.zeebe.client.event.EventMetadata)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ClientProperties (io.zeebe.client.ClientProperties)1 ZeebeClient (io.zeebe.client.ZeebeClient)1 GeneralEvent (io.zeebe.client.event.GeneralEvent)1 TopicEventType (io.zeebe.client.event.TopicEventType)1 TopicSubscription (io.zeebe.client.event.TopicSubscription)1 UniversalEventHandler (io.zeebe.client.event.UniversalEventHandler)1 TopicSubscriber (io.zeebe.client.event.impl.TopicSubscriber)1 ZeebeClientImpl (io.zeebe.client.impl.ZeebeClientImpl)1 IOException (java.io.IOException)1 List (java.util.List)1 Properties (java.util.Properties)1 Scanner (java.util.Scanner)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Collectors (java.util.stream.Collectors)1 ExpandableDirectBufferOutputStream (org.agrona.io.ExpandableDirectBufferOutputStream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1