feat():监听上下线事件
parent
50b8f4aa5e
commit
27c8cd1ae9
|
@ -0,0 +1,14 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="AliAccessStaticViaInstance" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliArrayNamingShouldHaveBracket" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliControlFlowStatementWithoutBraces" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliDeprecation" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliEqualsAvoidNull" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliLongLiteralsEndingWithLowercaseL" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliMissingOverrideAnnotation" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliWrapperTypeEquality" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="MapOrSetKeyShouldOverrideHashCodeEquals" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
</profile>
|
||||
</component>
|
39
pom.xml
39
pom.xml
|
@ -55,6 +55,11 @@
|
|||
<version>2.0.46</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.83</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
|
@ -74,6 +79,40 @@
|
|||
<version>5.1.8</version>
|
||||
</dependency>
|
||||
|
||||
<!-- excel工具 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
<version>4.1.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
<version>1.2.5</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>3.5.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
|
||||
<version>2.5.8</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.33</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package com.mobai.controller;
|
||||
|
||||
import com.mobai.service.StayTimeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @ClassName StayTimeController
|
||||
* @Description 描述
|
||||
* @Author SaiSai.Liu
|
||||
* @Date 2024/6/4 9:54
|
||||
*/
|
||||
@RestController
|
||||
public class StayTimeController {
|
||||
@Autowired
|
||||
private StayTimeService stayTimeService;
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.mobai.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @ClassName StayTime
|
||||
* @Description 描述
|
||||
* @Author SaiSai.Liu
|
||||
* @Date 2024/6/4 9:28
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class StayTime {
|
||||
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
private String vin;
|
||||
private String ip;
|
||||
private Long upTime;
|
||||
private Long downTime;
|
||||
private String remark;
|
||||
private String createBy;
|
||||
private Date createTime;
|
||||
private String updateBy;
|
||||
private Date updateTime;
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.mobai.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.mobai.domain.StayTime;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @ClassName StayTimeMapper
|
||||
* @Description 描述
|
||||
* @Author SaiSai.Liu
|
||||
* @Date 2024/6/4 9:57
|
||||
*/
|
||||
@Mapper
|
||||
public interface StayTimeMapper extends BaseMapper<StayTime> {
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
package com.mobai.mq.rabbitmq;
|
||||
|
||||
import com.rabbitmq.client.Channel;
|
||||
import org.springframework.amqp.core.ExchangeTypes;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.annotation.Exchange;
|
||||
import org.springframework.amqp.rabbit.annotation.Queue;
|
||||
import org.springframework.amqp.rabbit.annotation.QueueBinding;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @ClassName Custom
|
||||
* @Description 描述
|
||||
* @Author SaiSai.Liu
|
||||
* @Date 2024/5/26 15:25
|
||||
*/
|
||||
@Component
|
||||
public class Custom {
|
||||
|
||||
// @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "send_sms_queue"),
|
||||
// exchange = @Exchange(value = "null",type = ExchangeTypes.DIRECT)))
|
||||
// public void mqCustom(Object data, Message message, Channel channel){
|
||||
// System.out.println(data.toString());
|
||||
// try {
|
||||
// channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
|
||||
// } catch (IOException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.mobai.mq.rabbitmq.api;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
|
||||
public class MqttPublishSample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String topic = "MQTT Examples";
|
||||
String content = "Message from MqttPublishSample";
|
||||
int qos = 2;
|
||||
String broker = "tcp://iot.eclipse.org:1883";
|
||||
String clientId = "JavaSample";
|
||||
MemoryPersistence persistence = new MemoryPersistence();
|
||||
|
||||
try {
|
||||
MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
|
||||
MqttConnectOptions connOpts = new MqttConnectOptions();
|
||||
connOpts.setCleanSession(true);
|
||||
System.out.println("Connecting to broker: "+broker);
|
||||
sampleClient.connect(connOpts);
|
||||
System.out.println("Connected");
|
||||
System.out.println("Publishing message: "+content);
|
||||
MqttMessage message = new MqttMessage(content.getBytes());
|
||||
message.setQos(qos);
|
||||
sampleClient.publish(topic, message);
|
||||
System.out.println("Message published");
|
||||
sampleClient.disconnect();
|
||||
System.out.println("Disconnected");
|
||||
System.exit(0);
|
||||
} catch(MqttException me) {
|
||||
System.out.println("reason "+me.getReasonCode());
|
||||
System.out.println("msg "+me.getMessage());
|
||||
System.out.println("loc "+me.getLocalizedMessage());
|
||||
System.out.println("cause "+me.getCause());
|
||||
System.out.println("excep "+me);
|
||||
me.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.mobai.mq.rabbitmq.api;
|
||||
|
||||
import com.mobai.mq.rabbitmq.cofig.MqttFactory;
|
||||
import com.mobai.mq.rabbitmq.cofig.MqttProperties;
|
||||
import com.mobai.mq.rabbitmq.domian.GetOptions;
|
||||
import com.mobai.mq.rabbitmq.domian.MqttCallBackServiceImpl;
|
||||
import org.eclipse.paho.client.mqttv3.*;
|
||||
|
||||
public class SubscribeSample {
|
||||
public static void main(String[] args) {
|
||||
MqttProperties mqttProperties = MqttProperties.configBuild("39.98.69.92", "topic0");
|
||||
// MqttProperties mqttProperties = new MqttProperties();
|
||||
// mqttProperties.setBroker("tcp://39.98.69.92:1883");
|
||||
// mqttProperties.setTopic("mqtt/test");
|
||||
mqttProperties.setUsername("emqx");
|
||||
mqttProperties.setPassword("public");
|
||||
// mqttProperties.setClientid("subscribe_client");
|
||||
|
||||
int qos = 0;
|
||||
try {
|
||||
MqttClient client = new MqttFactory(new MqttCallBackServiceImpl()).buildOptions(mqttProperties);
|
||||
// 连接参数
|
||||
MqttConnectOptions options = GetOptions.getMqttOptionas(mqttProperties);
|
||||
// 设置回调
|
||||
client.setCallback(new MqttCallBackServiceImpl());
|
||||
// 进行连接
|
||||
client.connect(options);
|
||||
client.subscribe(mqttProperties.getTopic(), qos);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.mobai.mq.rabbitmq.cofig;
|
||||
|
||||
import com.mobai.mq.rabbitmq.domian.GetOptions;
|
||||
import com.mobai.mq.rabbitmq.domian.MqttCallBackServiceImpl;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @ClassName MqttFactory
|
||||
* @Description 描述
|
||||
* @Author SaiSai.Liu
|
||||
* @Date 2024/5/31 11:35
|
||||
*/
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class MqttFactory {
|
||||
|
||||
private final MqttCallBackServiceImpl mqttCallBackService;
|
||||
|
||||
public MqttClient buildOptions( MqttProperties mqttProperties) {
|
||||
MqttClient client = null;
|
||||
try {
|
||||
client = new MqttClient(
|
||||
mqttProperties.getBroker(),
|
||||
mqttProperties.getClientid(),
|
||||
new MemoryPersistence());
|
||||
MqttConnectOptions options = GetOptions.getMqttOptionas(mqttProperties);
|
||||
client.connect(options);
|
||||
client.setCallback(mqttCallBackService);
|
||||
// 事件订阅
|
||||
client.subscribe(mqttProperties.getTopic(), 0);
|
||||
|
||||
} catch (MqttException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.mobai.mq.rabbitmq.cofig;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @ClassName MqttProperties
|
||||
* @Description 描述
|
||||
* @Author SaiSai.Liu
|
||||
* @Date 2024/5/30 20:05
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class MqttProperties {
|
||||
private String broker ;
|
||||
private String topic ;
|
||||
private String username;
|
||||
private String password;
|
||||
private String clientid;
|
||||
|
||||
public static MqttProperties configBuild(String ip,String topic){
|
||||
// "tcp://39.98.69.92:1883"
|
||||
return MqttProperties.builder()
|
||||
.broker("tcp://"+ip+":1883")
|
||||
.topic(topic)
|
||||
.clientid("mobai-mq")
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.mobai.mq.rabbitmq.cofig;
|
||||
|
||||
import org.springframework.amqp.core.Binding;
|
||||
import org.springframework.amqp.core.BindingBuilder;
|
||||
import org.springframework.amqp.core.DirectExchange;
|
||||
import org.springframework.amqp.core.Queue;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @ClassName RabbitConfig
|
||||
* @Description 描述
|
||||
* @Author SaiSai.Liu
|
||||
* @Date 2024/5/31 21:47
|
||||
*/
|
||||
@Configuration
|
||||
public class RabbitConfig {
|
||||
|
||||
@Bean
|
||||
public Queue initQueue(){
|
||||
return new Queue("create.topic",true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建主题队列
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public DirectExchange direct() {
|
||||
return new DirectExchange("mobai-mq");
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定交换机
|
||||
* @param direct
|
||||
* @param initQueue
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public Binding binding1a(DirectExchange direct,
|
||||
Queue initQueue) {
|
||||
return BindingBuilder.bind(initQueue)
|
||||
.to(direct)
|
||||
.with("mobai-mq");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.mobai.mq.rabbitmq.domian;
|
||||
|
||||
import com.mobai.mq.rabbitmq.cofig.MqttProperties;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||
|
||||
/**
|
||||
* @ClassName GetOptions
|
||||
* @Description 描述
|
||||
* @Author SaiSai.Liu
|
||||
* @Date 2024/5/31 14:26
|
||||
*/
|
||||
|
||||
public class GetOptions {
|
||||
|
||||
public static MqttConnectOptions getMqttOptionas(MqttProperties mqttProperties){
|
||||
// 连接参数
|
||||
MqttConnectOptions options = new MqttConnectOptions();
|
||||
if (!StringUtils.isAllBlank(mqttProperties.getUsername(),
|
||||
mqttProperties.getPassword())){
|
||||
options.setUserName(mqttProperties.getUsername());
|
||||
options.setPassword(mqttProperties.getPassword().toCharArray());
|
||||
}
|
||||
options.setConnectionTimeout(60);
|
||||
options.setKeepAliveInterval(60);
|
||||
return options;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.mobai.mq.rabbitmq.domian;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
|
||||
import org.eclipse.paho.client.mqttv3.MqttCallback;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 回执消息类
|
||||
* @ClassName MqttCallBackServiceImpl
|
||||
* @Description 回执消息类
|
||||
* @Author SaiSai.Liu
|
||||
* @Date 2024/5/30 20:02
|
||||
*/
|
||||
@Service
|
||||
public class MqttCallBackServiceImpl implements MqttCallback {
|
||||
@Override
|
||||
public void connectionLost(Throwable cause) {
|
||||
System.out.println("connectionLost: " + cause.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageArrived(String topic, MqttMessage message) {
|
||||
System.out.println("topic: " + topic);
|
||||
System.out.println("Qos: " + message.getQos());
|
||||
System.out.println("message content: " + new String(message.getPayload()));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deliveryComplete(IMqttDeliveryToken token) {
|
||||
System.out.println("deliveryComplete---------" + token.isComplete());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,163 @@
|
|||
package com.mobai.mq.rabbitmq.rabbitMq;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.mobai.domain.StayTime;
|
||||
import com.mobai.mq.rabbitmq.cofig.MqttFactory;
|
||||
import com.mobai.mq.rabbitmq.cofig.MqttProperties;
|
||||
import com.mobai.service.StayTimeService;
|
||||
import com.mobai.util.RedisService;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 消费者:消息处理器
|
||||
*
|
||||
* @ClassName MessageHandler
|
||||
* @Description 描述
|
||||
* @Author SaiSai.Liu
|
||||
* @Date 2024/5/31 14:37
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class MessageHandler {
|
||||
|
||||
@Autowired
|
||||
private MqttFactory mqttFactory;
|
||||
|
||||
@Autowired
|
||||
private StayTimeService stayTimeService;
|
||||
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
@RabbitListener(queues = {"create.topic"})
|
||||
private void message(String msg) {
|
||||
log.info("消息内容:{}", msg);
|
||||
MqttProperties topic0 = MqttProperties.configBuild(
|
||||
"39.98.69.92",
|
||||
"topic0");
|
||||
log.info("接收到消息:{}", topic0);
|
||||
MqttClient client = mqttFactory.buildOptions(topic0);
|
||||
log.info("client创建:{}", client);
|
||||
log.info("clientID创建:{}", client.getClientId());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 上线事件
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
@RabbitListener(queues = {"event"})
|
||||
private void trainUp(String msg, Message message, Channel channel) {
|
||||
log.info("event:{}", msg);
|
||||
// 链接事件
|
||||
// event:{
|
||||
// "protocol":"MQTT",
|
||||
// "clientIp":"39.144.107.165",
|
||||
// "nodeIp":"127.0.0.1",
|
||||
// "clientId":"VIN123456789DIJE4",
|
||||
// "version":"MQTT_3_1_1",
|
||||
// "keepalive":20,
|
||||
// "cleanSession":true,
|
||||
// "timestamp":1717466764797,
|
||||
// "auth":{
|
||||
// "username":"6D7A546314155D43A339EE4C0410613D86C821299316ADECDB871E08",
|
||||
// "password":"VklOMTIzNDU2Nzg5RElKRTQxNzE3NDY2NzY1MDg3NTgyNDI4QThEQjA0RkU2OTkzNTM5NDIyNTQ2ODIwQzFFNzc3NDUzQTA4NzIzRTU4NUQyNDRBNjY="
|
||||
// },
|
||||
// "messageId":0}
|
||||
|
||||
JSONObject jsonObject = JSON.parseObject(msg);
|
||||
String vin = JSON.to(String.class, jsonObject.get("clientId"));
|
||||
long timestamp = JSON.to(Long.class, jsonObject.get("timestamp"));
|
||||
if (jsonObject.get("auth") != null) {
|
||||
try {
|
||||
log.info("上线事件");
|
||||
String ip = redisService.getValue(vin);
|
||||
log.info("上线车辆vin:{}\n\t上线时ip:{}", vin, ip);
|
||||
if (ip == null) {
|
||||
throw new ServletException("上线时ip为空");
|
||||
}
|
||||
// 方法内有判断,有则自增,无则创建
|
||||
redisService.increment("onlineCar-" + ip, 1);
|
||||
boolean save = stayTimeService.save(new StayTime() {{
|
||||
setIp(ip);
|
||||
setVin(vin);
|
||||
setUpTime(timestamp);
|
||||
}});
|
||||
log.info(save ? vin + "上线记录成功" : vin + "上线记录失败");
|
||||
// 消息消费成功则确认
|
||||
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
|
||||
} catch (IOException | ServletException e) {
|
||||
log.error("上线失败");
|
||||
try {
|
||||
channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} else {
|
||||
// event:{
|
||||
// "protocol":"MQTT",
|
||||
// "messageId":0,
|
||||
// "timestamp":1717466777367,
|
||||
// "reason":"normal",
|
||||
// "clientId":"VIN123456789DIJE4",
|
||||
// "nodeIp":"127.0.0.1",
|
||||
// "id":354,
|
||||
// "clientIp":"39.144.107.165"
|
||||
// }
|
||||
log.info("下线事件");
|
||||
String ip = redisService.getValue(vin);
|
||||
try {
|
||||
log.info("下线车辆vin:{}\n\t下线时ip:{}", vin, ip);
|
||||
if (ip == null) {
|
||||
throw new ServletException("下线时ip为空");
|
||||
}
|
||||
// 方法内有判断,有则自减,无则创建
|
||||
redisService.decrement("onlineCar-" + ip, 1);
|
||||
StayTime vinStayTime = stayTimeService.getOne(new LambdaQueryWrapper<>() {{
|
||||
eq(StayTime::getVin, vin);
|
||||
eq(StayTime::getDownTime, null);
|
||||
}});
|
||||
vinStayTime.setDownTime(timestamp);
|
||||
boolean update = stayTimeService.update(vinStayTime, new LambdaUpdateWrapper<>() {{
|
||||
eq(StayTime::getVin, vin);
|
||||
eq(StayTime::getDownTime, null);
|
||||
}});
|
||||
|
||||
// 输出在线时长
|
||||
log.info("车辆在线时长为:{}", new SimpleDateFormat("HH时mm分ss秒").format(new Date(vinStayTime.getUpTime() - timestamp)));
|
||||
log.info(update ? vin + "上线记录成功" : vin + "上线记录失败");
|
||||
|
||||
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
|
||||
} catch (ServletException | IOException e) {
|
||||
log.error("下线失败");
|
||||
try {
|
||||
channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -6,9 +6,9 @@ import com.alibaba.fastjson2.JSON;
|
|||
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.mobai.domain.flux.ApifoxModel;
|
||||
import com.mobai.domain.MqttServerModel;
|
||||
import com.mobai.domain.Result;
|
||||
import com.mobai.domain.flux.ApifoxModel;
|
||||
import com.mobai.service.FluxGetInfoService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
@ -21,6 +21,7 @@ import org.springframework.stereotype.Component;
|
|||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Log4j2
|
||||
@Component
|
||||
|
@ -71,7 +72,7 @@ public class SelectInstances {
|
|||
|
||||
|
||||
//10分钟
|
||||
// @Scheduled(cron = "0 0/10 * * * ? ")
|
||||
// @Scheduled(cron = "0 0/5 * * * ? ")
|
||||
//10秒
|
||||
@Scheduled(cron = "0/10 * * * * ? ")
|
||||
public void saveIps() throws Exception {
|
||||
|
@ -124,7 +125,8 @@ public class SelectInstances {
|
|||
for (String ip : ips) {
|
||||
Result<ApifoxModel> info = fluxGetInfoService.getInfo(ip);
|
||||
//连接总数
|
||||
long connectSize = Long.parseLong(redisTemplate.opsForValue().get("onlineCar-"+ip));
|
||||
String string = redisTemplate.opsForValue().get("onlineCar-" + ip);
|
||||
long connectSize = Long.parseLong(string == null ? "0" : string);
|
||||
log.info("{}::{}", ip, connectSize);
|
||||
//添加到一个容器
|
||||
nodes.add(new SmallNode(ip, connectSize));
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package com.mobai.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.mobai.domain.StayTime;
|
||||
import com.mobai.mapper.StayTimeMapper;
|
||||
|
||||
/**
|
||||
* @ClassName StayTimeService
|
||||
* @Description 描述
|
||||
* @Author SaiSai.Liu
|
||||
* @Date 2024/6/4 9:54
|
||||
*/
|
||||
public interface StayTimeService extends IService<StayTime> {
|
||||
}
|
|
@ -77,6 +77,7 @@ public class FluxGetInfoServiceImpl implements FluxGetInfoService {
|
|||
String string = redis.opsForList().range("fluxMq", 0, -1).get(0);
|
||||
List<MqttServerModel> mqtts = JSON.parseArray(string, MqttServerModel.class);
|
||||
log.info("集合:{}",mqtts);
|
||||
// tcp://192.168.1.1:1883
|
||||
if (redis.hasKey("fluxMqIndex")) {
|
||||
redis.opsForValue().increment("fluxMqIndex", 1);
|
||||
} else {
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package com.mobai.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.mobai.domain.StayTime;
|
||||
import com.mobai.mapper.StayTimeMapper;
|
||||
import com.mobai.service.StayTimeService;
|
||||
import com.mobai.util.RedisService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @ClassName StayTimeServiceImpl
|
||||
* @Description 描述
|
||||
* @Author SaiSai.Liu
|
||||
* @Date 2024/6/4 9:54
|
||||
*/
|
||||
|
||||
@Service
|
||||
public class StayTimeServiceImpl extends ServiceImpl<StayTimeMapper, StayTime> implements StayTimeService{
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,297 @@
|
|||
package com.mobai.util;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import org.apache.poi.ss.formula.functions.T;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.data.redis.core.BoundSetOperations;
|
||||
import org.springframework.data.redis.core.HashOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* spring redis 工具类
|
||||
*
|
||||
* @author muyu
|
||||
**/
|
||||
@SuppressWarnings(value = {"unchecked", "rawtypes"})
|
||||
@Component
|
||||
@Lazy
|
||||
public class RedisService {
|
||||
@Autowired
|
||||
public RedisTemplate redisTemplate;
|
||||
|
||||
/**
|
||||
* 缓存基本的对象,Integer、String、实体类等
|
||||
*
|
||||
* @param key 缓存的键值
|
||||
* @param value 缓存的值
|
||||
*/
|
||||
public <T> void setCacheObject(final String key, final T value) {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存基本的对象,Integer、String、实体类等
|
||||
*
|
||||
* @param key 缓存的键值
|
||||
* @param value 缓存的值
|
||||
* @param timeout 时间
|
||||
* @param timeUnit 时间颗粒度
|
||||
*/
|
||||
public <T> void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit) {
|
||||
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置有效时间
|
||||
*
|
||||
* @param key Redis键
|
||||
* @param timeout 超时时间
|
||||
* @return true=设置成功;false=设置失败
|
||||
*/
|
||||
public boolean expire(final String key, final long timeout) {
|
||||
return expire(key, timeout, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置有效时间
|
||||
*
|
||||
* @param key Redis键
|
||||
* @param timeout 超时时间
|
||||
* @param unit 时间单位
|
||||
* @return true=设置成功;false=设置失败
|
||||
*/
|
||||
public boolean expire(final String key, final long timeout, final TimeUnit unit) {
|
||||
return redisTemplate.expire(key, timeout, unit);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取有效时间
|
||||
*
|
||||
* @param key Redis键
|
||||
* @return 有效时间
|
||||
*/
|
||||
public long getExpire(final String key) {
|
||||
return redisTemplate.getExpire(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断 key是否存在
|
||||
*
|
||||
* @param key 键
|
||||
* @return true 存在 false不存在
|
||||
*/
|
||||
public Boolean hasKey(String key) {
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得缓存的基本对象。
|
||||
*
|
||||
* @param key 缓存键值
|
||||
* @return 缓存键值对应的数据
|
||||
*/
|
||||
public <T> T getCacheObject(final String key) {
|
||||
ValueOperations<String, T> operation = redisTemplate.opsForValue();
|
||||
return operation.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除单个对象
|
||||
*
|
||||
* @param key
|
||||
*/
|
||||
public boolean deleteObject(final String key) {
|
||||
return redisTemplate.delete(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除集合对象
|
||||
*
|
||||
* @param collection 多个对象
|
||||
* @return
|
||||
*/
|
||||
public boolean deleteObject(final Collection collection) {
|
||||
return redisTemplate.delete(collection) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存List数据
|
||||
*
|
||||
* @param key 缓存的键值
|
||||
* @param dataList 待缓存的List数据
|
||||
* @return 缓存的对象
|
||||
*/
|
||||
public <T> long setCacheList(final String key, final List<T> dataList) {
|
||||
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
|
||||
return count == null ? 0 : count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得缓存的list对象
|
||||
*
|
||||
* @param key 缓存的键值
|
||||
* @return 缓存键值对应的数据
|
||||
*/
|
||||
public <T> List<T> getCacheList(final String key) {
|
||||
return redisTemplate.opsForList().range(key, 0, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存Set
|
||||
*
|
||||
* @param key 缓存键值
|
||||
* @param dataSet 缓存的数据
|
||||
* @return 缓存数据的对象
|
||||
*/
|
||||
public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet) {
|
||||
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
|
||||
Iterator<T> it = dataSet.iterator();
|
||||
while (it.hasNext()) {
|
||||
setOperation.add(it.next());
|
||||
}
|
||||
return setOperation;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得缓存的set
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public <T> Set<T> getCacheSet(final String key) {
|
||||
return redisTemplate.opsForSet().members(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存Map
|
||||
*
|
||||
* @param key
|
||||
* @param dataMap
|
||||
*/
|
||||
public <T> void setCacheMap(final String key, final Map<String, T> dataMap) {
|
||||
if (dataMap != null) {
|
||||
redisTemplate.opsForHash().putAll(key, dataMap);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得缓存的Map
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public <T> Map<String, T> getCacheMap(final String key) {
|
||||
return redisTemplate.opsForHash().entries(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 往Hash中存入数据
|
||||
*
|
||||
* @param key Redis键
|
||||
* @param hKey Hash键
|
||||
* @param value 值
|
||||
*/
|
||||
public <T> void setCacheMapValue(final String key, final String hKey, final T value) {
|
||||
redisTemplate.opsForHash().put(key, hKey, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Hash中的数据
|
||||
*
|
||||
* @param key Redis键
|
||||
* @param hKey Hash键
|
||||
* @return Hash中的对象
|
||||
*/
|
||||
public <T> T getCacheMapValue(final String key, final String hKey) {
|
||||
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
|
||||
return opsForHash.get(key, hKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取多个Hash中的数据
|
||||
*
|
||||
* @param key Redis键
|
||||
* @param hKeys Hash键集合
|
||||
* @return Hash对象集合
|
||||
*/
|
||||
public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys) {
|
||||
return redisTemplate.opsForHash().multiGet(key, hKeys);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Hash中的某条数据
|
||||
*
|
||||
* @param key Redis键
|
||||
* @param hKey Hash键
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean deleteCacheMapValue(final String key, final String hKey) {
|
||||
return redisTemplate.opsForHash().delete(key, hKey) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得缓存的基本对象列表
|
||||
*
|
||||
* @param pattern 字符串前缀
|
||||
* @return 对象列表
|
||||
*/
|
||||
public Collection<String> keys(final String pattern) {
|
||||
return redisTemplate.keys(pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* 存入一个集合
|
||||
* @param key
|
||||
* @param t
|
||||
* @param <V>
|
||||
*/
|
||||
public <V extends List<?>> void setCacheList(String key, T t) {
|
||||
redisTemplate.opsForList().leftPush(String.valueOf(key),t);
|
||||
}
|
||||
|
||||
public <T> long setCacheList(final String key, final T dataList) {
|
||||
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
|
||||
return count == null ? 0 : count;
|
||||
}
|
||||
|
||||
public void increment(String s) {
|
||||
if (Boolean.TRUE.equals(redisTemplate.hasKey(s))){
|
||||
redisTemplate.opsForValue().increment(s,1);
|
||||
}else {
|
||||
redisTemplate.opsForValue().set(s,1);
|
||||
}
|
||||
}
|
||||
|
||||
public void increment(String s,int i) {
|
||||
if (Boolean.TRUE.equals(redisTemplate.hasKey(s))){
|
||||
redisTemplate.opsForValue().increment(s,i);
|
||||
}else {
|
||||
redisTemplate.opsForValue().set(s,i);
|
||||
}
|
||||
}
|
||||
public void decrement(String s) {
|
||||
if (Boolean.TRUE.equals(redisTemplate.hasKey(s))){
|
||||
redisTemplate.opsForValue().decrement(s,1);
|
||||
}else {
|
||||
redisTemplate.opsForValue().set(s,0);
|
||||
}
|
||||
}
|
||||
|
||||
public void decrement(String s,int i) {
|
||||
if (Boolean.TRUE.equals(redisTemplate.hasKey(s))){
|
||||
redisTemplate.opsForValue().decrement(s,i);
|
||||
}else {
|
||||
redisTemplate.opsForValue().set(s,0);
|
||||
}
|
||||
}
|
||||
|
||||
public String getValue(String vin) {
|
||||
return (String) redisTemplate.opsForValue().get(vin);
|
||||
}
|
||||
}
|
|
@ -5,3 +5,32 @@ spring:
|
|||
host: 43.142.100.73
|
||||
port: 6379
|
||||
password:
|
||||
rabbitmq:
|
||||
host: 43.142.100.73
|
||||
stream:
|
||||
username: guest
|
||||
password: guest
|
||||
datasource:
|
||||
dynamic:
|
||||
primary: mybatis #设置默认的数据源或者数据源组,默认值即为master
|
||||
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
|
||||
datasource:
|
||||
# gmall:
|
||||
# url: jdbc:mysql://hadoop104:3306/gmall?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
# username: root
|
||||
# password: "000000"
|
||||
# driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
mybatis:
|
||||
url: jdbc:mysql://127.0.0.1:3306/train_working?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
username: root
|
||||
password: 1234
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
# mybatis配置
|
||||
mybatis:
|
||||
# 搜索指定包别名
|
||||
typeAliasesPackage: com.mobai
|
||||
# 配置mapper的扫描,找到所有的mapper.xml映射文件
|
||||
mapperLocations: classpath:mapper/**/*.xml
|
||||
logging:
|
||||
level:
|
||||
com.mobai.mapper: DEBUG
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.mobai.mapper.StayTimeMapper">
|
||||
|
||||
</mapper>
|
Loading…
Reference in New Issue