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

com.github.lontime.extredisson.service.ReplyOutput Maven / Gradle / Ivy

The newest version!
package com.github.lontime.extredisson.service;

import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import com.github.lontime.base.commonj.result.ProcessResult;
import com.github.lontime.base.serial.MsgpackSerial;
import com.github.lontime.base.serial.model.Message;
import com.github.lontime.extredisson.common.RedissonExtHelper;
import com.github.lontime.shaded.com.twitter.serial.serializer.Serializer;
import lombok.Getter;
import lombok.Setter;

/**
 * ReplyOutput.
 * @author lontime
 * @since 1.0
 */
@Getter
@Setter
public class ReplyOutput {

    private String msgId;

    private Message message;

    private Duration timeout;

    private ProcessResult result;
    /**
     * reply to many. key -> tag
     */
    private Map> responses;

    /**
     * reply to one.
     */
    private CompletableFuture response;

    public ReplyOutput(String msgId, Message message, Duration timeout) {
        this.msgId = msgId;
        this.message = message;
        this.timeout = timeout;
    }

    public ReplyOutput(ProcessResult result) {
        this.result = result;
    }

    public  T resolveRequest(Serializer serializer) {
        if (message.getBody() == null) {
            return null;
        }
        return MsgpackSerial.INSTANCE.fromByteArrayUnchecked(message.getBody(), serializer);
    }

    public  Map resolveResponses(Serializer serializer) {
        if (responses == null || responses.size() == 0) {
            return Collections.emptyMap();
        }
        final int size = responses.size();
        final Map responseMap = new HashMap<>(size);
        for (Map.Entry> entry : responses.entrySet()) {
            final byte[] bytes = RedissonExtHelper.block(entry.getValue(), timeout, size);
            if (bytes != null) {
                responseMap.put(entry.getKey(), MsgpackSerial.INSTANCE.fromByteArrayUnchecked(bytes, serializer));
            }
        }
        return responseMap;
    }

    public  T resolveResponse(Serializer serializer) {
        final byte[] bytes = (response == null) ? null : RedissonExtHelper.block(response, timeout);
        if (bytes == null) {
            return null;
        }
        return MsgpackSerial.INSTANCE.fromByteArrayUnchecked(bytes, serializer);
    }

    public static ReplyOutput fail(String message) {
        return new ReplyOutput(ProcessResult.createFail(message));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy