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