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

se.laz.casual.jca.queue.CasualQueueCaller Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
/*
 * Copyright (c) 2017 - 2018, The casual project. All rights reserved.
 *
 * This software is licensed under the MIT license, https://opensource.org/licenses/MIT
 */

package se.laz.casual.jca.queue;

import se.laz.casual.api.CasualQueueApi;
import se.laz.casual.api.flags.ErrorState;
import se.laz.casual.api.network.protocol.messages.CasualNWMessage;
import se.laz.casual.api.queue.DequeueReturn;
import se.laz.casual.api.queue.EnqueueReturn;
import se.laz.casual.api.queue.MessageSelector;
import se.laz.casual.api.queue.QueueInfo;
import se.laz.casual.api.queue.QueueMessage;
import se.laz.casual.config.ConfigurationService;
import se.laz.casual.config.Domain;
import se.laz.casual.jca.CasualManagedConnection;
import se.laz.casual.network.connection.CasualConnectionException;
import se.laz.casual.network.protocol.messages.CasualNWMessageImpl;
import se.laz.casual.network.protocol.messages.domain.CasualDomainDiscoveryReplyMessage;
import se.laz.casual.network.protocol.messages.domain.CasualDomainDiscoveryRequestMessage;
import se.laz.casual.network.protocol.messages.domain.Queue;
import se.laz.casual.network.protocol.messages.queue.CasualDequeueReplyMessage;
import se.laz.casual.network.protocol.messages.queue.CasualDequeueRequestMessage;
import se.laz.casual.network.protocol.messages.queue.CasualEnqueueReplyMessage;
import se.laz.casual.network.protocol.messages.queue.CasualEnqueueRequestMessage;
import se.laz.casual.network.protocol.messages.queue.EnqueueMessage;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public class CasualQueueCaller implements CasualQueueApi
{
    private CasualManagedConnection connection;

    private CasualQueueCaller(final CasualManagedConnection connection)
    {
        this.connection = connection;
    }

    public static CasualQueueCaller of(final CasualManagedConnection connection)
    {
        Objects.requireNonNull(connection);
        return new CasualQueueCaller(connection);
    }

    @Override
    public EnqueueReturn enqueue(QueueInfo qinfo, QueueMessage msg)
    {
        try
        {
            // Always setting error state OK for now. In the future when error state is handled in the casual queue
            // protocol any error state supplied from casual should be used (same with dequeue)
            return EnqueueReturn.createBuilder().withErrorState(ErrorState.OK).withId(makeEnqueueCall(UUID.randomUUID(), qinfo, msg)).build();
        }
        catch(Exception e)
        {
            throw new CasualConnectionException(e);
        }
    }

    @Override
    public DequeueReturn dequeue(QueueInfo qinfo, MessageSelector selector)
    {
        try
        {
            // Always setting error state OK for now. In the future when error state is handled in the casual queue
            // protocol any error state supplied from casual should be used (same with enqueue)
            return DequeueReturn.createBuilder().withErrorState(ErrorState.OK).withQueueMessage(makeDequeueCall(UUID.randomUUID(), qinfo, selector)).build();
        }
        catch(Exception e)
        {
            throw new CasualConnectionException(e);
        }
    }

    @Override
    public boolean queueExists(QueueInfo qinfo)
    {
        try
        {
            return queueExists(UUID.randomUUID(), qinfo.getQueueName());
        }
        catch(Exception e)
        {
            throw new CasualConnectionException(e);
        }
    }

    private UUID makeEnqueueCall(UUID corrid, QueueInfo qinfo, QueueMessage msg)
    {
        CasualEnqueueRequestMessage requestMessage = CasualEnqueueRequestMessage.createBuilder()
                                                                                .withExecution(UUID.randomUUID())
                                                                                .withXid(connection.getCurrentXid())
                                                                                .withQueueName(qinfo.getQueueName())
                                                                                .withMessage(EnqueueMessage.of(msg))
                                                                                .build();
        CasualNWMessage networkRequestMessage = CasualNWMessageImpl.of(corrid, requestMessage);
        CompletableFuture> networkReplyMessageFuture = connection.getNetworkConnection().request(networkRequestMessage);

        CasualNWMessage networkReplyMessage = networkReplyMessageFuture.join();
        CasualEnqueueReplyMessage replyMessage = networkReplyMessage.getMessage();
        return replyMessage.getId();
    }

    private QueueMessage makeDequeueCall(UUID corrid, QueueInfo qinfo, MessageSelector selector)
    {
        CasualDequeueRequestMessage requestMessage = CasualDequeueRequestMessage.createBuilder()
                                                                                .withExecution(UUID.randomUUID())
                                                                                .withXid(connection.getCurrentXid())
                                                                                .withQueueName(qinfo.getQueueName())
                                                                                .withSelectorProperties(selector.getSelector())
                                                                                .withSelectorUUID(selector.getSelectorId())
                                                                                .withBlock(qinfo.getOptions().isBlocking())
                                                                                .build();
        CasualNWMessage networkRequestMessage = CasualNWMessageImpl.of(corrid, requestMessage);
        CompletableFuture> networkReplyMessageFuture = connection.getNetworkConnection().request(networkRequestMessage);

        CasualNWMessage networkReplyMessage = networkReplyMessageFuture.join();
        CasualDequeueReplyMessage replyMessage = networkReplyMessage.getMessage();
        List messages = Transformer.transform(replyMessage.getMessages());
        return messages.isEmpty() ? null : messages.get(0);
    }

    private boolean queueExists( UUID corrid, String queueName)
    {
        Domain domain = ConfigurationService.getInstance().getConfiguration().getDomain();
        CasualDomainDiscoveryRequestMessage requestMsg = CasualDomainDiscoveryRequestMessage.createBuilder()
                                                                                            .setExecution(UUID.randomUUID())
                                                                                            .setDomainId(domain.getId())
                                                                                            .setDomainName(domain.getName())
                                                                                            .setQueueNames(Arrays.asList(queueName))
                                                                                            .build();
        CasualNWMessage msg = CasualNWMessageImpl.of(corrid, requestMsg);
        CompletableFuture> replyMsgFuture = connection.getNetworkConnection().request(msg);

        CasualNWMessage replyMsg = replyMsgFuture.join();
        return replyMsg.getMessage().getQueues().stream()
                .map(Queue::getName)
                .anyMatch(v -> v.equals(queueName));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy