/*
 * Decompiled with CFR 0.152.
 *
 * Could not load the following classes:
 *  com.rabbitmq.client.Channel
 *  org.jeecg.common.config.mqtoken.UserTokenContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package org.jeecg.boot.starter.rabbitmq.core;

import com.rabbitmq.client.Channel;
import org.jeecg.boot.starter.rabbitmq.listenter.MqListener;
import org.jeecg.common.config.mqtoken.UserTokenContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class BaseRabbiMqHandler<T> {
    private static final Logger log = LoggerFactory.getLogger(BaseRabbiMqHandler.class);
    private final String token = UserTokenContext.getToken();

    public void onMessage(T t, Long deliveryTag, Channel channel, MqListener mqListener) {
        try {
            UserTokenContext.setToken(this.token);
            mqListener.handler(t, channel);
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            log.info("接收消息失败,重新放回队列");
            try {
                channel.basicNack(deliveryTag, false, true);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}