Search in sources :

Example 1 with OkapiConnectionParams

use of org.folio.rest.util.OkapiConnectionParams in project mod-inventory by folio-org.

the class QuickMarcKafkaHandler method handle.

@Override
public Future<String> handle(KafkaConsumerRecord<String, String> record) {
    var params = new OkapiConnectionParams(kafkaHeadersToMap(record.headers()), vertx);
    var context = constructContext(params.getTenantId(), params.getToken(), params.getOkapiUrl());
    Event event = Json.decodeValue(record.value(), Event.class);
    LOGGER.info("Quick marc event payload has been received with event type: {}", event.getEventType());
    return getEventPayload(event).compose(eventPayload -> processPayload(eventPayload, context).compose(recordType -> sendEvent(eventPayload, getReplyEventType(recordType), params)).recover(throwable -> sendErrorEvent(params, eventPayload, throwable)).map(ar -> record.key()), th -> {
        LOGGER.error("Update record state was failed while handle event, {}", th.getMessage());
        return Future.failedFuture(th.getMessage());
    });
}
Also used : QM_INVENTORY_AUTHORITY_UPDATED(org.folio.inventory.dataimport.handlers.QMEventTypes.QM_INVENTORY_AUTHORITY_UPDATED) Json(io.vertx.core.json.Json) Context(org.folio.inventory.common.Context) AbstractQuickMarcEventHandler(org.folio.inventory.dataimport.handlers.quickmarc.AbstractQuickMarcEventHandler) EventMetadata(org.folio.rest.jaxrs.model.EventMetadata) KafkaTopicNameHelper.getDefaultNameSpace(org.folio.kafka.KafkaTopicNameHelper.getDefaultNameSpace) KafkaHeaderUtils.kafkaHeadersFromMap(org.folio.kafka.KafkaHeaderUtils.kafkaHeadersFromMap) HashMap(java.util.HashMap) KafkaProducer(io.vertx.kafka.client.producer.KafkaProducer) EventHandlingUtil.constructContext(org.folio.inventory.dataimport.handlers.matching.util.EventHandlingUtil.constructContext) UpdateHoldingsQuickMarcEventHandler(org.folio.inventory.dataimport.handlers.quickmarc.UpdateHoldingsQuickMarcEventHandler) HoldingsUpdateDelegate(org.folio.inventory.dataimport.handlers.actions.HoldingsUpdateDelegate) Map(java.util.Map) QM_INVENTORY_HOLDINGS_UPDATED(org.folio.inventory.dataimport.handlers.QMEventTypes.QM_INVENTORY_HOLDINGS_UPDATED) KafkaHeaderUtils.kafkaHeadersToMap(org.folio.kafka.KafkaHeaderUtils.kafkaHeadersToMap) QMEventTypes(org.folio.inventory.dataimport.handlers.QMEventTypes) Event(org.folio.rest.jaxrs.model.Event) PrecedingSucceedingTitlesHelper(org.folio.inventory.dataimport.handlers.actions.PrecedingSucceedingTitlesHelper) KafkaTopicNameHelper.formatTopicName(org.folio.kafka.KafkaTopicNameHelper.formatTopicName) Record(org.folio.rest.jaxrs.model.Record) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) UpdateInstanceQuickMarcEventHandler(org.folio.inventory.dataimport.handlers.quickmarc.UpdateInstanceQuickMarcEventHandler) UUID(java.util.UUID) UpdateAuthorityQuickMarcEventHandler(org.folio.inventory.dataimport.handlers.quickmarc.UpdateAuthorityQuickMarcEventHandler) AsyncRecordHandler(org.folio.kafka.AsyncRecordHandler) QM_INVENTORY_INSTANCE_UPDATED(org.folio.inventory.dataimport.handlers.QMEventTypes.QM_INVENTORY_INSTANCE_UPDATED) Future(io.vertx.core.Future) ParsedRecordDto(org.folio.rest.jaxrs.model.ParsedRecordDto) OkapiConnectionParams(org.folio.rest.util.OkapiConnectionParams) Storage(org.folio.inventory.storage.Storage) InstanceUpdateDelegate(org.folio.inventory.dataimport.handlers.actions.InstanceUpdateDelegate) AtomicLong(java.util.concurrent.atomic.AtomicLong) Logger(org.apache.logging.log4j.Logger) EventProcessingException(org.folio.processing.exceptions.EventProcessingException) KafkaConsumerRecord(io.vertx.kafka.client.consumer.KafkaConsumerRecord) AuthorityUpdateDelegate(org.folio.inventory.dataimport.handlers.actions.AuthorityUpdateDelegate) QM_ERROR(org.folio.inventory.dataimport.handlers.QMEventTypes.QM_ERROR) KafkaProducerRecord(io.vertx.kafka.client.producer.KafkaProducerRecord) LogManager(org.apache.logging.log4j.LogManager) ConsumerWrapperUtil(org.folio.inventory.dataimport.util.ConsumerWrapperUtil) KafkaConfig(org.folio.kafka.KafkaConfig) KafkaProducer.createShared(io.vertx.kafka.client.producer.KafkaProducer.createShared) Event(org.folio.rest.jaxrs.model.Event) OkapiConnectionParams(org.folio.rest.util.OkapiConnectionParams)

Example 2 with OkapiConnectionParams

use of org.folio.rest.util.OkapiConnectionParams 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 OkapiConnectionParams

use of org.folio.rest.util.OkapiConnectionParams 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)

Aggregations

Vertx (io.vertx.core.Vertx)3 Map (java.util.Map)3 UUID (java.util.UUID)3 LogManager (org.apache.logging.log4j.LogManager)3 Logger (org.apache.logging.log4j.Logger)3 Event (org.folio.rest.jaxrs.model.Event)3 EventMetadata (org.folio.rest.jaxrs.model.EventMetadata)3 Context (io.vertx.core.Context)2 CompletableFuture (java.util.concurrent.CompletableFuture)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 Future (io.vertx.core.Future)1 Promise (io.vertx.core.Promise)1 Json (io.vertx.core.json.Json)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 HttpResponse (io.vertx.ext.web.client.HttpResponse)1