Search in sources :

Example 1 with EventMetadata

use of org.folio.rest.jaxrs.model.EventMetadata in project mod-source-record-storage by folio-org.

the class ParsedRecordChunksKafkaHandler method sendBackRecordsBatchResponse.

private Future<String> sendBackRecordsBatchResponse(RecordsBatchResponse recordsBatchResponse, List<KafkaHeader> kafkaHeaders, String tenantId, int chunkNumber, String eventType, KafkaConsumerRecord<String, String> commonRecord) {
    Event event;
    event = new Event().withId(UUID.randomUUID().toString()).withEventType(DI_PARSED_RECORDS_CHUNK_SAVED.value()).withEventPayload(Json.encode(normalize(recordsBatchResponse))).withEventMetadata(new EventMetadata().withTenantId(tenantId).withEventTTL(1).withPublishedBy(constructModuleName()));
    String key = String.valueOf(indexer.incrementAndGet() % maxDistributionNum);
    String topicName = KafkaTopicNameHelper.formatTopicName(kafkaConfig.getEnvId(), KafkaTopicNameHelper.getDefaultNameSpace(), tenantId, DI_PARSED_RECORDS_CHUNK_SAVED.value());
    KafkaProducerRecord<String, String> targetRecord = KafkaProducerRecord.create(topicName, key, Json.encode(event));
    targetRecord.addHeaders(kafkaHeaders);
    Promise<String> writePromise = Promise.promise();
    String producerName = DI_PARSED_RECORDS_CHUNK_SAVED + "_Producer";
    KafkaProducer<String, String> producer = KafkaProducer.createShared(Vertx.currentContext().owner(), producerName, kafkaConfig.getProducerProps());
    producer.write(targetRecord, war -> {
        producer.end(ear -> producer.close());
        if (war.succeeded()) {
            String recordId = extractValueFromHeaders(commonRecord.headers(), RECORD_ID_HEADER);
            String chunkId = extractValueFromHeaders(commonRecord.headers(), CHUNK_ID_HEADER);
            LOGGER.debug("RecordCollection processing has been completed with response sent... event: '{}', chunkId: '{}', chunkNumber '{}'-'{}' with recordId: '{}'", eventType, chunkId, chunkNumber, targetRecord.key(), recordId);
            writePromise.complete(targetRecord.key());
        } else {
            Throwable cause = war.cause();
            LOGGER.error("{} write error {}", producerName, cause);
            writePromise.fail(cause);
        }
    });
    return writePromise.future();
}
Also used : Event(org.folio.rest.jaxrs.model.Event) EventMetadata(org.folio.rest.jaxrs.model.EventMetadata)

Example 2 with EventMetadata

use of org.folio.rest.jaxrs.model.EventMetadata in project mod-circulation-storage by folio-org.

the class PubSubPublishingService method publishEvent.

public CompletableFuture<Boolean> publishEvent(String eventType, String payload) {
    Event event = new Event().withId(UUID.randomUUID().toString()).withEventType(eventType).withEventPayload(payload).withEventMetadata(new EventMetadata().withPublishedBy(PubSubClientUtils.getModuleId()).withTenantId(okapiHeaders.get(OKAPI_TENANT_HEADER)).withEventTTL(1));
    final CompletableFuture<Boolean> publishResult = new CompletableFuture<>();
    OkapiConnectionParams params = new OkapiConnectionParams();
    params.setOkapiUrl(okapiHeaders.get(OKAPI_URL_HEADER));
    params.setTenantId(okapiHeaders.get(OKAPI_TENANT_HEADER));
    params.setToken(okapiHeaders.get(OKAPI_TOKEN_HEADER));
    context.runOnContext(v -> PubSubClientUtils.sendEventMessage(event, params).whenComplete((result, throwable) -> {
        if (Boolean.TRUE.equals(result)) {
            logger.debug("Event published successfully. ID: {}, type: {}, payload: {}", event.getId(), event.getEventType(), event.getEventPayload());
            publishResult.complete(true);
        } else {
            logger.error("Failed to publish event. ID: {}, type: {}, payload: {}", throwable, event.getId(), event.getEventType(), event.getEventPayload());
            if (throwable == null) {
                publishResult.complete(false);
            } else {
                publishResult.completeExceptionally(throwable);
            }
        }
    }));
    return publishResult;
}
Also used : Event(org.folio.rest.jaxrs.model.Event) OKAPI_TENANT_HEADER(org.folio.rest.util.OkapiConnectionParams.OKAPI_TENANT_HEADER) EventMetadata(org.folio.rest.jaxrs.model.EventMetadata) Vertx(io.vertx.core.Vertx) CompletableFuture(java.util.concurrent.CompletableFuture) OKAPI_URL_HEADER(org.folio.rest.util.OkapiConnectionParams.OKAPI_URL_HEADER) UUID(java.util.UUID) OkapiConnectionParams(org.folio.rest.util.OkapiConnectionParams) Context(io.vertx.core.Context) Logger(org.apache.logging.log4j.Logger) Map(java.util.Map) OKAPI_TOKEN_HEADER(org.folio.rest.util.OkapiConnectionParams.OKAPI_TOKEN_HEADER) PubSubClientUtils(org.folio.util.pubsub.PubSubClientUtils) LogManager(org.apache.logging.log4j.LogManager) CompletableFuture(java.util.concurrent.CompletableFuture) Event(org.folio.rest.jaxrs.model.Event) OkapiConnectionParams(org.folio.rest.util.OkapiConnectionParams) EventMetadata(org.folio.rest.jaxrs.model.EventMetadata)

Example 3 with EventMetadata

use of org.folio.rest.jaxrs.model.EventMetadata in project mod-circulation by folio-org.

the class PubSubPublishingService method publishEvent.

public CompletableFuture<Boolean> publishEvent(String eventType, String payload) {
    Event event = new Event().withId(UUID.randomUUID().toString()).withEventType(eventType).withEventPayload(payload).withEventMetadata(new EventMetadata().withPublishedBy(PubSubClientUtils.getModuleId()).withTenantId(okapiHeaders.get(OKAPI_TENANT_HEADER)).withEventTTL(1));
    final CompletableFuture<Boolean> publishResult = new CompletableFuture<>();
    OkapiConnectionParams params = new OkapiConnectionParams();
    params.setOkapiUrl(okapiHeaders.get(OKAPI_URL_HEADER));
    params.setTenantId(okapiHeaders.get(OKAPI_TENANT_HEADER));
    params.setToken(okapiHeaders.get(OKAPI_TOKEN_HEADER));
    vertxContext.runOnContext(v -> PubSubClientUtils.sendEventMessage(event, params).whenComplete((result, throwable) -> {
        if (Boolean.TRUE.equals(result)) {
            logger.info("Event published successfully. ID: {}, type: {}, payload: {}", event.getId(), event.getEventType(), event.getEventPayload());
            publishResult.complete(true);
        } else {
            logger.error("Failed to publish event. ID: {}, type: {}, payload: {}, cause: {}", event.getId(), event.getEventType(), event.getEventPayload(), throwable);
            if (throwable == null) {
                publishResult.complete(false);
            } else {
                publishResult.completeExceptionally(throwable);
            }
        }
    }));
    return publishResult;
}
Also used : Event(org.folio.rest.jaxrs.model.Event) ClientErrorResponse(org.folio.circulation.support.http.server.ClientErrorResponse) OKAPI_TENANT_HEADER(org.folio.rest.util.OkapiConnectionParams.OKAPI_TENANT_HEADER) HttpResponse(io.vertx.ext.web.client.HttpResponse) EventMetadata(org.folio.rest.jaxrs.model.EventMetadata) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure) WebContext(org.folio.circulation.support.http.server.WebContext) CompleteFuture(io.netty.util.concurrent.CompleteFuture) Vertx(io.vertx.core.Vertx) CompletableFuture(java.util.concurrent.CompletableFuture) OKAPI_URL_HEADER(org.folio.rest.util.OkapiConnectionParams.OKAPI_URL_HEADER) UUID(java.util.UUID) RoutingContext(io.vertx.ext.web.RoutingContext) ValidationError(org.folio.circulation.support.http.server.ValidationError) Result(org.folio.circulation.support.results.Result) OkapiConnectionParams(org.folio.rest.util.OkapiConnectionParams) Context(io.vertx.core.Context) Logger(org.apache.logging.log4j.Logger) Map(java.util.Map) OKAPI_TOKEN_HEADER(org.folio.rest.util.OkapiConnectionParams.OKAPI_TOKEN_HEADER) EventSendingException(org.folio.util.pubsub.exceptions.EventSendingException) PubSubClientUtils(org.folio.util.pubsub.PubSubClientUtils) LogManager(org.apache.logging.log4j.LogManager) CompletableFuture(java.util.concurrent.CompletableFuture) Event(org.folio.rest.jaxrs.model.Event) OkapiConnectionParams(org.folio.rest.util.OkapiConnectionParams) EventMetadata(org.folio.rest.jaxrs.model.EventMetadata)

Example 4 with EventMetadata

use of org.folio.rest.jaxrs.model.EventMetadata in project mod-inventory by folio-org.

the class QuickMarcKafkaHandler method createRecord.

private KafkaProducerRecord<String, String> createRecord(String eventPayload, String eventType, String key, OkapiConnectionParams params) {
    Event event = new Event().withId(UUID.randomUUID().toString()).withEventType(eventType).withEventPayload(eventPayload).withEventMetadata(new EventMetadata().withTenantId(params.getTenantId()).withEventTTL(1).withPublishedBy(ConsumerWrapperUtil.constructModuleName()));
    String topicName = formatTopicName(kafkaConfig.getEnvId(), getDefaultNameSpace(), params.getTenantId(), eventType);
    var record = KafkaProducerRecord.create(topicName, key, Json.encode(event));
    record.addHeaders(kafkaHeadersFromMap(params.getHeaders()));
    return record;
}
Also used : Event(org.folio.rest.jaxrs.model.Event) EventMetadata(org.folio.rest.jaxrs.model.EventMetadata)

Example 5 with EventMetadata

use of org.folio.rest.jaxrs.model.EventMetadata in project mod-source-record-storage by folio-org.

the class EventHandlingUtil method createProducerRecord.

public static KafkaProducerRecord<String, String> createProducerRecord(String eventPayload, String eventType, String key, String tenantId, List<KafkaHeader> kafkaHeaders, KafkaConfig kafkaConfig) {
    Event event = new Event().withId(UUID.randomUUID().toString()).withEventType(eventType).withEventPayload(eventPayload).withEventMetadata(new EventMetadata().withTenantId(tenantId).withEventTTL(1).withPublishedBy(constructModuleName()));
    String topicName = createTopicName(eventType, tenantId, kafkaConfig);
    KafkaProducerRecord<String, String> record = KafkaProducerRecord.create(topicName, key, Json.encode(event));
    record.addHeaders(kafkaHeaders);
    return record;
}
Also used : Event(org.folio.rest.jaxrs.model.Event) EventMetadata(org.folio.rest.jaxrs.model.EventMetadata)

Aggregations

Event (org.folio.rest.jaxrs.model.Event)5 EventMetadata (org.folio.rest.jaxrs.model.EventMetadata)5 Context (io.vertx.core.Context)2 Vertx (io.vertx.core.Vertx)2 Map (java.util.Map)2 UUID (java.util.UUID)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 OkapiConnectionParams (org.folio.rest.util.OkapiConnectionParams)2 OKAPI_TENANT_HEADER (org.folio.rest.util.OkapiConnectionParams.OKAPI_TENANT_HEADER)2 OKAPI_TOKEN_HEADER (org.folio.rest.util.OkapiConnectionParams.OKAPI_TOKEN_HEADER)2 OKAPI_URL_HEADER (org.folio.rest.util.OkapiConnectionParams.OKAPI_URL_HEADER)2 PubSubClientUtils (org.folio.util.pubsub.PubSubClientUtils)2 CompleteFuture (io.netty.util.concurrent.CompleteFuture)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 HttpResponse (io.vertx.ext.web.client.HttpResponse)1 ValidationErrorFailure (org.folio.circulation.support.ValidationErrorFailure)1 ClientErrorResponse (org.folio.circulation.support.http.server.ClientErrorResponse)1 ValidationError (org.folio.circulation.support.http.server.ValidationError)1