All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.enodeframework.queue.publishableexceptions.DefaultPublishableExceptionMessageHandler Maven / Gradle / Ivy

There is a newer version: 1.1.10
Show newest version
package org.enodeframework.queue.publishableexceptions;

import org.enodeframework.common.exception.MessageInstanceCreateException;
import org.enodeframework.common.serializing.ISerializeService;
import org.enodeframework.domain.IDomainException;
import org.enodeframework.infrastructure.ITypeNameProvider;
import org.enodeframework.messaging.IMessageDispatcher;
import org.enodeframework.queue.IMessageContext;
import org.enodeframework.queue.IMessageHandler;
import org.enodeframework.queue.QueueMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultPublishableExceptionMessageHandler implements IMessageHandler {

    private static final Logger logger = LoggerFactory.getLogger(DefaultPublishableExceptionMessageHandler.class);

    private final ITypeNameProvider typeNameProvider;

    private final IMessageDispatcher messageDispatcher;

    private final ISerializeService serializeService;

    public DefaultPublishableExceptionMessageHandler(ITypeNameProvider typeNameProvider, IMessageDispatcher messageDispatcher, ISerializeService serializeService) {
        this.typeNameProvider = typeNameProvider;
        this.messageDispatcher = messageDispatcher;
        this.serializeService = serializeService;
    }

    @Override
    public void handle(QueueMessage queueMessage, IMessageContext context) {
        logger.info("Received domain exception message: {}", serializeService.serialize(queueMessage));
        PublishableExceptionMessage exceptionMessage = serializeService.deserialize(queueMessage.getBody(), PublishableExceptionMessage.class);
        Class exceptionType = typeNameProvider.getType(exceptionMessage.getExceptionType());
        IDomainException exception;
        try {
            exception = (IDomainException) exceptionType.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            throw new MessageInstanceCreateException(e);
        }
        exception.setId(exceptionMessage.getUniqueId());
        exception.setTimestamp(exceptionMessage.getTimestamp());
        exception.setItems(exceptionMessage.getItems());
        exception.restoreFrom(exceptionMessage.getSerializableInfo());
        messageDispatcher.dispatchMessageAsync(exception).whenComplete((x, y) -> {
            context.onMessageHandled(queueMessage);
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy