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

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

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

import java.util.Collections;
import java.util.List;

import com.github.lontime.base.commonj.result.ProcessResult;
import com.github.lontime.base.commonj.utils.CollectionHelper;
import com.github.lontime.base.commonj.utils.UuidHelper;
import com.github.lontime.base.serial.codec.MsgCodec;
import com.github.lontime.base.serial.codec.ObjectMsgCodec;
import com.github.lontime.base.serial.model.Message;
import com.github.lontime.extredisson.configuration.OptionResolver;
import com.github.lontime.extredisson.configuration.ProducerOption;
import com.github.lontime.extredisson.listener.HandlerInterface;
import com.github.lontime.shaded.com.twitter.serial.serializer.Serializer;

/**
 * BatchProvider.
 *
 * @author lontime
 * @since 1.0
 */
public interface Provider extends BatchProvider {

    String DEFAULT_METHOD_NAME = "index";

    /**
     * call for object.
     *
     * @param serviceName serviceName
     * @param request     request
     * @param methodName  methodName
     * @return response
     */
    default Object callForObject(String serviceName, String methodName, Object request) {
        return call(serviceName, methodName, request, ObjectMsgCodec.INSTANCE);
    }

    /**
     * call for object.
     *
     * @param serviceName serviceName
     * @param request     request
     * @return response
     */
    default Object callForObject(String serviceName, Object request) {
        return call(serviceName, request, ObjectMsgCodec.INSTANCE);
    }

    /**
     * call.
     *
     * @param serviceName serviceName
     * @param codec       codec
     * @param request     request
     * @param        request
     * @param        res
     * @return response
     */
    default  RES call(String serviceName, REQ request, MsgCodec codec) {
        return call(serviceName, DEFAULT_METHOD_NAME, request, codec);
    }

    /**
     * call.
     *
     * @param serviceName serviceName
     * @param serializer  serializer
     * @param request     request
     * @param        request
     */
    default  void oneway(String serviceName, REQ request, Serializer serializer) {
        oneway(serviceName, DEFAULT_METHOD_NAME, request, serializer);
    }

    /**
     * call.
     *
     * @param serviceName serviceName
     * @param serializer  serializer
     * @param request     request
     * @param        request
     * @param methodName  methodName
     */
    default  void oneway(String serviceName, String methodName, REQ request, Serializer serializer) {
        oneway(serviceName, methodName, request, serializer, ctx -> ProcessResult.createSuccess());
    }

    /**
     * call.
     *
     * @param serviceName serviceName
     * @param methodName  methodName
     * @param codec       codec
     * @param request     request
     * @param        request
     * @param        res
     * @return response
     */
    default  RES call(String serviceName, String methodName, REQ request, MsgCodec codec) {
        return call(serviceName, methodName, request, codec, ctx -> ProcessResult.createSuccess());
    }

    /**
     * call.
     *
     * @param serviceName serviceName
     * @param methodName  methodName
     * @param codec       codec
     * @param request     request
     * @param handler     handler
     * @param        request
     * @param        res
     * @return response
     */
    default  RES call(String serviceName, String methodName, REQ request, MsgCodec codec, HandlerInterface handler) {
        final Message message = Message.builder().serviceName(serviceName)
                .replyTo(UuidHelper.fastUUID())
                .body(request, codec.getIndata())
                .methodName(methodName).build();
        final ReplyOutput output = call(message, handler);
        if (output == null) {
            return null;
        }
        return output.resolveResponse(codec.getOutdata());
    }

    /**
     * oneway.
     *
     * @param serviceName serviceName
     * @param methodName  methodName
     * @param serializer  codec
     * @param handler     handler
     * @param request     request
     * @param        request
     */
    default  void oneway(String serviceName, String methodName, REQ request, Serializer serializer, HandlerInterface handler) {
        final Message message = Message.builder().serviceName(serviceName)
                .body(request, serializer)
                .methodName(methodName).build();
        call(message, handler);
    }

    /**
     * call.
     *
     * @param message message
     * @return output
     */
    default ReplyOutput call(Message message) {
        return call(message, ctx -> ProcessResult.createSuccess());
    }

    /**
     * call.
     *
     * @param message message
     * @param option  option
     * @return output
     */
    default ReplyOutput call(ProducerOption option, Message message) {
        return call(option, message, ctx -> ProcessResult.createSuccess());
    }

    /**
     * call.
     *
     * @param message    message
     * @param optionName optionName
     * @return list
     */
    default ReplyOutput call(String optionName, Message message) {
        return call(optionName, message, ctx -> ProcessResult.createSuccess());
    }

    /**
     * call.
     *
     * @param message message
     * @param handler handler
     * @return output
     */
    default ReplyOutput call(Message message, HandlerInterface handler) {
        return call(ProducerOption.topic(), message, handler);
    }

    /**
     * call.
     *
     * @param name name
     * @param message    message
     * @param handler    handler
     * @return output
     */
    default ReplyOutput call(String name, Message message, HandlerInterface handler) {
        return call(OptionResolver.getInstance().getProducer(name), message, handler);
    }

    /**
     * call.
     *
     * @param option  option
     * @param message message
     * @param handler handler
     * @return output
     */
    default ReplyOutput call(ProducerOption option, Message message, HandlerInterface handler) {
        final List outputs = call(option, Collections.singletonList(message), handler);
        if (CollectionHelper.isEmpty(outputs)) {
            return ReplyOutput.fail("output is null");
        }
        return outputs.get(0);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy