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

in.hocg.boot.message.autoconfigure.service.TransactionalMessageQueueBervice Maven / Gradle / Ivy

package in.hocg.boot.message.autoconfigure.service;

import cn.hutool.core.util.IdUtil;
import in.hocg.boot.message.autoconfigure.core.TransactionalMessageContext;
import in.hocg.boot.message.autoconfigure.core.message.Message;

import java.util.List;

/**
 * Created by hocgin on 2021/4/9
 * email: [email protected]
 *
 * @author hocgin
 */
public interface TransactionalMessageQueueBervice {
    boolean syncSend(M message);

    boolean asyncSend(M message);

    default void prepare(M message) {
        getMessageContext().add(message);
    }

    default void publish() {
        this.publish(true);
    }

    default void publish(boolean isAsync) {
        final List messages = getMessageContext().getAndClear();
        if (messages.isEmpty()) {
            return;
        }

        final String messageGroupSn = getMessageGroupSn();
        for (M message : messages) {
            if (message instanceof Message) {
                ((Message) message).setGroupSn(messageGroupSn);
            }
            this.saveMessage(message);
            if (isAsync) {
                this.asyncSend(message);
            } else {
                this.asyncSend(message);
            }
        }
    }

    default void clear() {
        getMessageContext().clear();
    }

    default String getMessageGroupSn() {
        return IdUtil.fastSimpleUUID();
    }

    TransactionalMessageContext getMessageContext();

    default void saveMessage(M message) {
        //
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy