package com.td.boss.config.delay;

import com.alibaba.fastjson.JSONObject;
import com.riven.redisson.annotation.EnableRedisson;
import com.riven.redisson.annotation.RedissonListener;
import com.riven.redisson.config.RedissonQueue;
import com.riven.redisson.exception.MessageConversionException;
import com.riven.redisson.message.*;
import com.td.boss.config.delay.pojo.PlayerGoods;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;

import java.util.Map;

/**
 * redisson延迟任务
 *
 * @author why
 * @date 2022/03/22
 */
@Configuration
@EnableRedisson
public class RedissonDelayTask {
    @Bean
    public RedissonQueue redissonQueue() {
        return new RedissonQueue("playerGoods", true, null, playerGoodsMessageConverter());
    }
    @Bean("playerGoodsMessageConverter")
    public MessageConverter playerGoodsMessageConverter() {
        return new MessageConverter() {
            @Override
            public QueueMessage<?> toMessage(Object object, Map<String, Object> headers) throws MessageConversionException {
                //do something you want, eg:
                headers.put("my_header", "my_header_value");
                return QueueMessageBuilder.withPayload(object).headers(headers).build();
            }

            @Override
            public Object fromMessage(RedissonMessage redissonMessage) throws MessageConversionException {
                Object payload = redissonMessage.getPayload();
                return JSONObject.parseObject(payload.toString(), PlayerGoods.class);
            }
        };
    }
}
