PlanConsumer.java 1.19 KB
package com.yanzuoguang.redis.mq;

import com.alibaba.fastjson.TypeReference;
import com.rabbitmq.client.Channel;
import com.yanzuoguang.mq.service.MqService;
import com.yanzuoguang.redis.PlanInfo;
import com.yanzuoguang.redis.service.PlanService;
import com.yanzuoguang.util.helper.JsonHelper;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;

/**
 * 任务消费
 *
 * @author 颜佐光
 */
@Component
public class PlanConsumer {
    private final MqService mqService;
    private final PlanService planService;

    public PlanConsumer(MqService mqService, PlanService planService) {
        this.mqService = mqService;
        this.planService = planService;
    }

    /**
     * 消费任务
     *
     * @param json    消息队列内容
     * @param message 消息体
     * @param channel 连接频道
     */
    public void plan(String json, Message message, Channel channel) {
        mqService.basicHandle(message,channel,json,content->{
            PlanInfo<String> timeNew = JsonHelper.deserialize(json, new TypeReference<PlanInfo<String>>() {
            });
            // 获取运行的任务
            planService.runPlan(timeNew);
        });
    }
}