MessageHandler.java
1.8 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
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.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-mainsys-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-mainsys-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();
}
}
}