SpringBoot根据yml配置信息动态生成bean并加入Spring容器
@Component
public class CarInitTask implements CommandLineRunner,WebSocketHandler {
Logger logger= LoggerFactory.getLogger(CarInitTask.class);
@Value("${}")
private String webSocketUrl;
@Value("${}")
private String zone;//全局范围内车辆
@Autowired
ISjfzCarService carService;
@Autowired
ISjfzInformationboardService informationboardService;
@Autowired
CarMesageHandler carMesageHandler;
@Autowired
EmqClient emqClient;
Map<String,String> carsMap=new HashMap();
int count=0;
@Override
public void run(String... args) throws Exception {
try {
String timestap = DateUtils.dateTimeNow("yyyy-MM-dd");
String tablename="`sjfz_car_" + timestap+"`";
String Informationboardtablename = "`sjfz_informationboard_" + timestap+"`";
carService.createDayCarsTable(tablename);
informationboardService.createDayInformationboardTable(Informationboardtablename);
}catch (Exception e){
e.printStackTrace();
}
logger.error("全局连接车辆websocket...........");
System.out.println(zone);
WsWebSocketContainer wsWebSocketContainer = new WsWebSocketContainer();
wsWebSocketContainer.setDefaultMaxBinaryMessageBufferSize(5120000);
wsWebSocketContainer.setDefaultMaxTextMessageBufferSize(5120000);
// (300000);
StandardWebSocketClient client = new StandardWebSocketClient(wsWebSocketContainer);
WebSocketHandler webSocketHandler=this;
String uriTemplate = webSocketUrl;
Object uriVars = null;
ListenableFuture<WebSocketSession> future =client.doHandshake(webSocketHandler, uriTemplate, uriVars);
try {
WebSocketSession session = future.get();
System.out.println(session);
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void afterConnectionEstablished(WebSocketSession webSocketSession) throws Exception {
TextMessage message=new TextMessage(zone.getBytes());
webSocketSession.sendMessage(message);
}
@Override
public void handleMessage(WebSocketSession webSocketSession, WebSocketMessage<?> webSocketMessage) throws Exception {
count++;
Map map = JSONObject.fromObject(webSocketMessage.getPayload());
Map map1=JSONObject.fromObject( map.get("result"));
JSONArray jsonArray = JSONArray.fromObject(map1.get("perList"));
List<Map> perList = JSONArray.toList(jsonArray, Map.class);
for (int i=0;i<perList.size();i++){
JSONArray dataArray= JSONArray.fromObject(perList.get(i).get("data"));
List<Map> dataList = JSONArray.toList(dataArray,Map.class);
for(int j=0;j<dataList.size();j++){
String confidence=dataList.get(j).get("confidence").toString();
String devId=dataList.get(j).get("devId").toString();
String gpsTime=dataList.get(j).get("gpsTime").toString();
Date date = new Date();
date.setTime(Long.valueOf(gpsTime));
String temp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(date);
String heading=dataList.get(j).get("heading").toString();
String latitude=dataList.get(j).get("latitude").toString();
String longitude=dataList.get(j).get("longitude").toString();
String objColor=dataList.get(j).get("objColor").toString();
String plateNo=dataList.get(j).get("plateNo").toString();
String speed=dataList.get(j).get("speed").toString();
String targetType=dataList.get(j).get("targetType").toString();
String vehicleId=dataList.get(j).get("vehicleId").toString();
try {
// (plateNo, vehicleId);
}catch (Exception e){
e.printStackTrace();
}
String tablename="`sjfz_car_" +DateUtils.parseDateToStr("yyyy-MM-dd",DateUtils.parseDate(temp,"yyyy-MM-dd HH:mm:ss SSS"))+"`";
SjfzCar car=new SjfzCar();
car.setConfidence(confidence);
car.setDevId(devId);
car.setGpsTime(temp);
car.setHeading(heading);
car.setLatitude(latitude);
car.setLongitude(longitude);
car.setObjColor(objColor);
car.setPlateNo(plateNo);
car.setSpeed(speed);
car.setTargetType(targetType);
car.setVehicleId(vehicleId);
car.setTablename(tablename);
String time=carsMap.get(plateNo);
if(StringUtils.isEmpty(time)){
carsMap.put(plateNo,temp);
carService.insertSjfzCar(car);
emqClient.publish("jd/12", JSON.toJSONString(car), QosEnum.Qos0,false);
}else{
if(DateUtils.parseDate(temp,"yyyy-MM-dd HH:mm:ss SSS").getTime()-DateUtils.parseDate(time,"yyyy-MM-dd HH:mm:ss SSS").getTime()>=500){
carsMap.put(plateNo,temp);
carService.insertSjfzCar(car);
emqClient.publish("jd/12", JSON.toJSONString(car), QosEnum.Qos0,false);
}
}
}
}
if(count==100){
carsMap.clear();
}
}
@Override
public void handleTransportError(WebSocketSession webSocketSession, Throwable throwable) throws Exception {
}
@Override
public void afterConnectionClosed(WebSocketSession webSocketSession, CloseStatus closeStatus) throws Exception {
logger.info("断开了websocket连接:"+closeStatus.toString());
reconnect();
}
@Override
public boolean supportsPartialMessages() {
return false;
}
public void reconnect(){
WsWebSocketContainer wsWebSocketContainer = new WsWebSocketContainer();
wsWebSocketContainer.setDefaultMaxBinaryMessageBufferSize(5120000);
wsWebSocketContainer.setDefaultMaxTextMessageBufferSize(5120000);
// (300000);
StandardWebSocketClient client = new StandardWebSocketClient(wsWebSocketContainer);
WebSocketHandler webSocketHandler=this;
String uriTemplate = webSocketUrl;
Object uriVars = null;
ListenableFuture<WebSocketSession> future =client.doHandshake(webSocketHandler, uriTemplate, uriVars);
try {
WebSocketSession session = future.get();
System.out.println(session);
}catch (Exception e){
e.printStackTrace();
reconnect();
}
}
public String getZone() {
return zone;
}
public void setZone(String zone) {
this.zone = zone;
}
}