MessageHandler.java 2.32 KB
package com.bsth.message.handler;

import com.bsth.message.buffer.CarEnergyBuffer;
import com.bsth.message.entity.CarEnergy;
import com.bsth.message.entity.CarErrorStop;
import com.bsth.message.entity.InoutPark;
import com.bsth.websocket.handler.SendUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * @author Hill
 */
@Component
@ConditionalOnProperty("kafka.use")
public class MessageHandler {

    @Autowired
    private SendUtils sendUtils;

    @Autowired
    private ObjectMapper mapper;

    @KafkaListener(topics="schedule-main-carerrorstop")
    public void receivedCarErrorStop(Message<String> message) {
        try {
            List<CarErrorStop> carErrorStopList = mapper.readValue(message.getPayload(), mapper.getTypeFactory().constructParametricType(List.class, CarErrorStop.class));
            for (CarErrorStop carErrorStop : carErrorStopList) {
                sendUtils.sendCarErrorStop(carErrorStop);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @KafkaListener(topics="schedule-main-carenergy")
    public void receivedCarEnergy(Message<String> message) {
        try {
            List<CarEnergy> carEnergyList = mapper.readValue(message.getPayload(), mapper.getTypeFactory().constructParametricType(List.class, CarEnergy.class));
            for (CarEnergy carEnergy : carEnergyList) {
                CarEnergyBuffer.put(carEnergy);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @KafkaListener(topics="schedule-main-inoutpark")
    public void receivedInoutPark(Message<String> message) {
        try {
            List<InoutPark> inoutParkList = mapper.readValue(message.getPayload(), mapper.getTypeFactory().constructParametricType(List.class, InoutPark.class));
            for (InoutPark inoutPark : inoutParkList) {
                sendUtils.sendInoutPark(inoutPark);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}