KafkaMessageHandler.java
2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.bsth.message.handler;
import com.bsth.data.gpsdata_v2.GpsRealData;
import com.bsth.message.buffer.CarEnergyBuffer;
import com.bsth.message.entity.*;
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 KafkaMessageHandler {
@Autowired
private SendUtils sendUtils;
@Autowired
private ObjectMapper mapper;
@Autowired
GpsRealData gpsRealData;
@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();
}
}
}