diff --git a/logs/vehicle.log.2024-06-22.0.gz b/logs/vehicle.log.2024-06-22.0.gz new file mode 100644 index 0000000..1124201 Binary files /dev/null and b/logs/vehicle.log.2024-06-22.0.gz differ diff --git a/logs/vehicle.log.2024-06-23.0.gz b/logs/vehicle.log.2024-06-23.0.gz new file mode 100644 index 0000000..c3371a8 Binary files /dev/null and b/logs/vehicle.log.2024-06-23.0.gz differ diff --git a/logs/vehicle.log.2024-06-24.0.gz b/logs/vehicle.log.2024-06-24.0.gz new file mode 100644 index 0000000..fe288a7 Binary files /dev/null and b/logs/vehicle.log.2024-06-24.0.gz differ diff --git a/logs/vehicle.log.2024-06-26.0.gz b/logs/vehicle.log.2024-06-26.0.gz new file mode 100644 index 0000000..cfec027 Binary files /dev/null and b/logs/vehicle.log.2024-06-26.0.gz differ diff --git a/logs/vehicle.log.2024-06-27.0.gz b/logs/vehicle.log.2024-06-27.0.gz new file mode 100644 index 0000000..ba831b5 Binary files /dev/null and b/logs/vehicle.log.2024-06-27.0.gz differ diff --git a/logs/vehicle.log.2024-06-28.0.gz b/logs/vehicle.log.2024-06-28.0.gz new file mode 100644 index 0000000..45b9894 Binary files /dev/null and b/logs/vehicle.log.2024-06-28.0.gz differ diff --git a/src/main/java/com/muyu/iotdb/model/VehicleData.java b/src/main/java/com/muyu/iotdb/model/VehicleData.java new file mode 100644 index 0000000..ff74bdc --- /dev/null +++ b/src/main/java/com/muyu/iotdb/model/VehicleData.java @@ -0,0 +1,73 @@ +package com.muyu.iotdb.model; + + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + + +/** + * @author 牧鱼 + * @Classname VehicleData + * @Description 车辆模拟数据对象 + * @Date 2021/8/5 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class VehicleData { + private String vin; // 车辆识别号 + private long timestamp; // 时间戳 + private double longitude; // 经度 + private double latitude; // 纬度 + private int speed; // 速度 + private int mileage; // 里程 + private Double voltage; // 电压,默认为Double而非String以支持数值操作 + private Double current; // 电流 + private Double resistance; // 电阻 + private String gear; // 档位 + private String accelerationPedal; // 加速踏板位置 + private String brakePedal; // 刹车踏板状态 + private String fuelConsumptionRate; // 燃油消耗率 + private String motorControllerTemperature; // 电机控制器温度 + private String motorSpeed; // 电机转速 + private String motorTorque; // 电机扭矩 + private String motorTemperature; // 电机温度 + private String motorVoltage; // 电机电压 + private String motorCurrent; // 电机电流 + private int remainingBattery; // 剩余电池电量百分比 + private int batteryLevel; // 电池等级 + private String maximumFeedbackPower; // 最大回馈功率 + private String maximumDischargePower; // 最大放电功率 + private String selfCheckCounter; // 自检计数器 + private String totalBatteryCurrent; // 总电池电流 + private String totalBatteryVoltage; // 总电池电压 + private String singleBatteryMaxVoltage; // 单体电池最高电压 + private String singleBatteryMinVoltage; // 单体电池最低电压 + private String singleBatteryMaxTemperature; // 单体电池最高温度 + private String singleBatteryMinTemperature; // 单体电池最低温度 + private String availableBatteryCapacity; // 可用电池容量 + private int vehicleStatus; // 车辆状态 + private int chargingStatus; // 充电状态 + private int operatingStatus; // 运行状态 + private int socStatus; // SOC状态 + private int chargingEnergyStorageStatus; // 充电储能状态 + private int driveMotorStatus; // 驱动电机状态 + private int positionStatus; // 位置状态 + private int easStatus; // EAS状态(假设EAS为某种系统状态) + private int ptcStatus; // PTC状态 + private int epsStatus; // EPS状态 + private int absStatus; // ABS状态 + private int mcuStatus; // MCU状态 + private int heatingStatus; // 加热状态 + private int batteryStatus; // 电池状态 + private int batteryInsulationStatus; // 电池绝缘状态 + private int dcdcStatus; // DC/DC转换器状态 + private int chgStatus; // 充电状态(可能与chargingStatus有区别,具体看实际业务) + private String vehicleStatusMsg; // 车辆状态信息 + private String smartHardwareMsg; // 智能硬件信息 + private String batteryMsg; // 电池信息 + +} diff --git a/src/main/java/com/muyu/iotdb/model/param/IotDbParam.java b/src/main/java/com/muyu/iotdb/model/param/IotDbParam.java new file mode 100644 index 0000000..2f846a9 --- /dev/null +++ b/src/main/java/com/muyu/iotdb/model/param/IotDbParam.java @@ -0,0 +1,24 @@ +package com.muyu.iotdb.model.param; + +import lombok.Data; +/** + * description: 入参 + * date: 2022/8/15 21:53 + * author: YangLe + */ +@Data +public class IotDbParam { + + + + private String vin; + /*** + * 查询开始时间 + */ + private String startTime; + /*** + * 查询结束时间 + */ + private String endTime; + +} diff --git a/src/main/java/com/muyu/iotdb/model/result/IotDbResult.java b/src/main/java/com/muyu/iotdb/model/result/IotDbResult.java new file mode 100644 index 0000000..ab2f9d3 --- /dev/null +++ b/src/main/java/com/muyu/iotdb/model/result/IotDbResult.java @@ -0,0 +1,33 @@ +package com.muyu.iotdb.model.result; + +import lombok.Data; + +/** + * description: 返回结果 + * date: 2022/8/15 21:56 + * author: YangLe + */ +@Data +public class IotDbResult { + /*** + * 时间 + */ + private String time; + /*** + * 产品PK + */ + private String pk; + /*** + * 设备号 + */ + private String sn; + /*** + * 实时呼吸 + */ + private String breath; + /*** + * 实时心率 + */ + private String heart; + +} diff --git a/src/main/java/com/muyu/iotdb/respone/ErrorResponseData.java b/src/main/java/com/muyu/iotdb/respone/ErrorResponseData.java new file mode 100644 index 0000000..c679ab4 --- /dev/null +++ b/src/main/java/com/muyu/iotdb/respone/ErrorResponseData.java @@ -0,0 +1,77 @@ +package com.muyu.iotdb.respone; + +/** + * description: 错误返回封装 + * date: 2022/8/15 21:30 + * author: YangLe + */ +public class ErrorResponseData extends ResponseData { + private String exceptionClazz; + + ErrorResponseData(String message) { + super(false, DEFAULT_ERROR_CODE, message, message, (Object)null); + } + + public ErrorResponseData(Integer code, String message) { + super(false, code, message, message, (Object)null); + } + + ErrorResponseData(Integer code, String message, Object object) { + super(false, code, message, object); + } + + ErrorResponseData(Integer code, String message, String localizedMsg, Object object) { + super(false, code, message, localizedMsg, object); + } + + public boolean equals(final Object o) { + if (o == this) { + return true; + } else if (!(o instanceof ErrorResponseData)) { + return false; + } else { + ErrorResponseData other = (ErrorResponseData)o; + if (!other.canEqual(this)) { + return false; + } else if (!super.equals(o)) { + return false; + } else { + Object this$exceptionClazz = this.getExceptionClazz(); + Object other$exceptionClazz = other.getExceptionClazz(); + if (this$exceptionClazz == null) { + if (other$exceptionClazz != null) { + return false; + } + } else if (!this$exceptionClazz.equals(other$exceptionClazz)) { + return false; + } + + return true; + } + } + } + + protected boolean canEqual(final Object other) { + return other instanceof ErrorResponseData; + } + + public int hashCode() { + int result = super.hashCode(); + Object $exceptionClazz = this.getExceptionClazz(); + result = result * 59 + ($exceptionClazz == null ? 43 : $exceptionClazz.hashCode()); + return result; + } + + public String getExceptionClazz() { + return this.exceptionClazz; + } + + public void setExceptionClazz(final String exceptionClazz) { + this.exceptionClazz = exceptionClazz; + } + + public String toString() { + return "ErrorResponseData(exceptionClazz=" + this.getExceptionClazz() + ")"; + } +} + diff --git a/src/main/java/com/muyu/iotdb/respone/ResponseData.java b/src/main/java/com/muyu/iotdb/respone/ResponseData.java new file mode 100644 index 0000000..b070d75 --- /dev/null +++ b/src/main/java/com/muyu/iotdb/respone/ResponseData.java @@ -0,0 +1,214 @@ +package com.muyu.iotdb.respone; + +/** + * description: 返回结果封装 + * date: 2022/8/15 21:32 + * author: YangLe + */ +public class ResponseData { + public static final String DEFAULT_SUCCESS_MESSAGE = "请求成功"; + public static final String DEFAULT_ERROR_MESSAGE = "网络异常"; + public static final Integer DEFAULT_SUCCESS_CODE = 200; + public static final Integer DEFAULT_ERROR_CODE = 500; + private Boolean success; + private Integer code; + private String message; + private String localizedMsg; + private Object data; + + public ResponseData() { + } + + public ResponseData(Boolean success, Integer code, String message, Object data) { + this.success = success; + this.code = code; + this.message = message; + this.data = data; + } + + public ResponseData(Boolean success, Integer code, String message, String localizedMsg, Object data) { + this.success = success; + this.code = code; + this.message = message; + this.localizedMsg = localizedMsg; + this.data = data; + } + + public ResponseData(Boolean success, Integer code, String message) { + this.success = success; + this.code = code; + this.message = message; + } + + public static SuccessResponseData success() { + return new SuccessResponseData(); + } + + public static SuccessResponseData success(Object object) { + return new SuccessResponseData(object); + } + + public static SuccessResponseData success(Integer code, String message, Object object) { + return new SuccessResponseData(code, message, object); + } + + public static SuccessResponseData success(Integer code, String message) { + return new SuccessResponseData(code, message); + } + + public static SuccessResponseData success(Integer code, String message, String localizedMsg, Object object) { + return new SuccessResponseData(code, message, localizedMsg, object); + } + + public static ErrorResponseData error(String message) { + return new ErrorResponseData(message); + } + + public static ErrorResponseData error(Integer code, String message) { + return new ErrorResponseData(code, message); + } + + public static ErrorResponseData error(Integer code, String message, Object object) { + return new ErrorResponseData(code, message, object); + } + + public static ErrorResponseData error(Integer code, String message, String localizedMsg, Object object) { + return new ErrorResponseData(code, message, localizedMsg, object); + } + + public Boolean getSuccess() { + return this.success; + } + + public Integer getCode() { + return this.code; + } + + public String getMessage() { + return this.message; + } + + public String getLocalizedMsg() { + return this.localizedMsg; + } + + public Object getData() { + return this.data; + } + + public void setSuccess(final Boolean success) { + this.success = success; + } + + public void setCode(final Integer code) { + this.code = code; + } + + public void setMessage(final String message) { + this.message = message; + } + + public void setLocalizedMsg(final String localizedMsg) { + this.localizedMsg = localizedMsg; + } + + public void setData(final Object data) { + this.data = data; + } + + public boolean equals(final Object o) { + if (o == this) { + return true; + } else if (!(o instanceof ResponseData)) { + return false; + } else { + ResponseData other = (ResponseData)o; + if (!other.canEqual(this)) { + return false; + } else { + label71: { + Object this$success = this.getSuccess(); + Object other$success = other.getSuccess(); + if (this$success == null) { + if (other$success == null) { + break label71; + } + } else if (this$success.equals(other$success)) { + break label71; + } + + return false; + } + + Object this$code = this.getCode(); + Object other$code = other.getCode(); + if (this$code == null) { + if (other$code != null) { + return false; + } + } else if (!this$code.equals(other$code)) { + return false; + } + + label57: { + Object this$message = this.getMessage(); + Object other$message = other.getMessage(); + if (this$message == null) { + if (other$message == null) { + break label57; + } + } else if (this$message.equals(other$message)) { + break label57; + } + + return false; + } + + Object this$localizedMsg = this.getLocalizedMsg(); + Object other$localizedMsg = other.getLocalizedMsg(); + if (this$localizedMsg == null) { + if (other$localizedMsg != null) { + return false; + } + } else if (!this$localizedMsg.equals(other$localizedMsg)) { + return false; + } + + Object this$data = this.getData(); + Object other$data = other.getData(); + if (this$data == null) { + if (other$data == null) { + return true; + } + } else if (this$data.equals(other$data)) { + return true; + } + + return false; + } + } + } + + protected boolean canEqual(final Object other) { + return other instanceof ResponseData; + } + + public int hashCode() { + int result1 = 1; + Object $success = this.getSuccess(); + int result = result1 * 59 + ($success == null ? 43 : $success.hashCode()); + Object $code = this.getCode(); + result = result * 59 + ($code == null ? 43 : $code.hashCode()); + Object $message = this.getMessage(); + result = result * 59 + ($message == null ? 43 : $message.hashCode()); + Object $localizedMsg = this.getLocalizedMsg(); + result = result * 59 + ($localizedMsg == null ? 43 : $localizedMsg.hashCode()); + Object $data = this.getData(); + result = result * 59 + ($data == null ? 43 : $data.hashCode()); + return result; + } + + public String toString() { + return "ResponseData(success=" + this.getSuccess() + ", code=" + this.getCode() + ", message=" + this.getMessage() + ", localizedMsg=" + this.getLocalizedMsg() + ", data=" + this.getData() + ")"; + } +} diff --git a/src/main/java/com/muyu/iotdb/respone/SuccessResponseData.java b/src/main/java/com/muyu/iotdb/respone/SuccessResponseData.java new file mode 100644 index 0000000..2c97961 --- /dev/null +++ b/src/main/java/com/muyu/iotdb/respone/SuccessResponseData.java @@ -0,0 +1,28 @@ +package com.muyu.iotdb.respone; +/** + * description: 正确返回结果封装 + * date: 2022/8/15 21:40 + * author: YangLe + */ + +public class SuccessResponseData extends ResponseData { + public SuccessResponseData() { + super(true, DEFAULT_SUCCESS_CODE, "请求成功", "请求成功", (Object)null); + } + + public SuccessResponseData(Object object) { + super(true, DEFAULT_SUCCESS_CODE, "请求成功", "请求成功", object); + } + + public SuccessResponseData(Integer code, String message, Object object) { + super(true, code, message, message, object); + } + + public SuccessResponseData(Integer code, String message, String localizedMsg, Object object) { + super(true, code, message, localizedMsg, object); + } + + public SuccessResponseData(Integer code, String message) { + super(true, code, message); + } +} diff --git a/src/main/java/com/muyu/iotdb/server/IotDbServer.java b/src/main/java/com/muyu/iotdb/server/IotDbServer.java new file mode 100644 index 0000000..e0e43fb --- /dev/null +++ b/src/main/java/com/muyu/iotdb/server/IotDbServer.java @@ -0,0 +1,25 @@ +package com.muyu.iotdb.server; + + + +import com.alibaba.fastjson2.JSONObject; +import com.muyu.iotdb.model.VehicleData; +import org.apache.iotdb.rpc.IoTDBConnectionException; +import org.apache.iotdb.rpc.StatementExecutionException; +import org.springframework.stereotype.Service; + +import java.rmi.ServerException; + +/** + * description: iot服务类 + * date: 2022/8/15 21:41 + * author: YangLe + */ +@Service +public interface IotDbServer { + /** + * 添加数据 + */ + void insertData(JSONObject vehicleData) throws StatementExecutionException, ServerException, IoTDBConnectionException; + +} diff --git a/src/main/java/com/muyu/iotdb/server/impl/IotDbServerImpl.java b/src/main/java/com/muyu/iotdb/server/impl/IotDbServerImpl.java new file mode 100644 index 0000000..633d05e --- /dev/null +++ b/src/main/java/com/muyu/iotdb/server/impl/IotDbServerImpl.java @@ -0,0 +1,53 @@ +package com.muyu.iotdb.server.impl; + + +import com.alibaba.fastjson2.JSONObject; + +import com.muyu.iotdb.config.IotDBSessionConfig; +import com.muyu.iotdb.model.VehicleData; +import com.muyu.iotdb.server.IotDbServer; +import lombok.extern.log4j.Log4j2; +import org.apache.iotdb.rpc.IoTDBConnectionException; +import org.apache.iotdb.rpc.StatementExecutionException; +import org.apache.iotdb.session.SessionDataSet; +import org.apache.iotdb.tsfile.read.common.Field; +import org.apache.iotdb.tsfile.read.common.RowRecord; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.rmi.ServerException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * description: iot服务实现类 + * date: 2022/8/15 9:43 + * author: yangle + */ + +@Log4j2 +@Service +public class IotDbServerImpl implements IotDbServer { + + @Resource + private IotDBSessionConfig iotDBSessionConfig; + + @Override + public void insertData(JSONObject vehicleData) throws StatementExecutionException, ServerException, IoTDBConnectionException { + // iotDbParam: 模拟设备上报消息 + // bizkey: 业务唯一key PK :产品唯一编码 SN:设备唯一编码 + String deviceId = "root.test." + vehicleData.getString("vin"); + // 将设备上报的数据存入数据库(时序数据库) + List measurementsList = new ArrayList<>(); + measurementsList.add("vehicleStatusMsg"); + List valuesList = new ArrayList<>(); + valuesList.add(vehicleData.toString()); + log.info("vehicleData:{}", vehicleData); + log.info("valuesList:{}",valuesList); + iotDBSessionConfig.insertRecord(deviceId, vehicleData.getLong("timestamp"), measurementsList, valuesList); + } + + +} diff --git a/src/main/java/com/muyu/mqtt/dao/MessageDa.java b/src/main/java/com/muyu/mqtt/dao/MessageDa.java new file mode 100644 index 0000000..bffdc94 --- /dev/null +++ b/src/main/java/com/muyu/mqtt/dao/MessageDa.java @@ -0,0 +1,30 @@ +package com.muyu.mqtt.dao; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.SuperBuilder; +import org.apache.iotdb.tsfile.read.filter.operator.In; +import org.checkerframework.checker.units.qual.N; + +/** + * 解析完成对象 + * + * @author Yangle + * Date 2024/6/26 22:30 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@SuperBuilder +public class MessageDa { + private Integer id; + private String analyzeKey; + private Integer analyzeStart; + private Integer ent; + private String lable; + private String value; + + + +} diff --git a/src/main/java/com/muyu/redis/RedisApplication.java b/src/main/java/com/muyu/redis/RedisApplication.java new file mode 100644 index 0000000..33ac9e8 --- /dev/null +++ b/src/main/java/com/muyu/redis/RedisApplication.java @@ -0,0 +1,17 @@ +package com.muyu.redis; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Redis启动类 RedisApplication + * + * @author Yangle + * Date 2024/6/26 16:45 + */ +@SpringBootApplication +public class RedisApplication { + + public static void main(String[] args) { + + } +} diff --git a/src/main/java/com/muyu/redis/controller/RedisController.java b/src/main/java/com/muyu/redis/controller/RedisController.java new file mode 100644 index 0000000..579b77b --- /dev/null +++ b/src/main/java/com/muyu/redis/controller/RedisController.java @@ -0,0 +1,36 @@ +package com.muyu.redis.controller; + +import com.muyu.redis.demo.RedisData; +import com.muyu.redis.service.RedisService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +/** + * RedisController + * + * @author Yangle + * Date 2024/6/26 16:48 + */ +@RestController +public class RedisController { + + @Autowired + private RedisService redisService; + + + @PostMapping("/redis/set") + public String set(@RequestBody RedisData redisData) { + redisService.set(redisData); + return "success"; + } + + @PostMapping("/redis/get") + public String get(@RequestBody RedisData redisData) { + return redisService.get(redisData); + } + + + +} diff --git a/src/main/java/com/muyu/redis/service/RedisService.java b/src/main/java/com/muyu/redis/service/RedisService.java new file mode 100644 index 0000000..8eed3b8 --- /dev/null +++ b/src/main/java/com/muyu/redis/service/RedisService.java @@ -0,0 +1,15 @@ +package com.muyu.redis.service; + +import com.muyu.redis.demo.RedisData; + +/** + * RedisService + * + * @author Yangle + * Date 2024/6/26 16:49 + */ +public interface RedisService { + void set(RedisData redisData); + + String get(RedisData redisData); +} diff --git a/src/main/java/com/muyu/redis/service/impl/RedisServiceImpl.java b/src/main/java/com/muyu/redis/service/impl/RedisServiceImpl.java new file mode 100644 index 0000000..29bda26 --- /dev/null +++ b/src/main/java/com/muyu/redis/service/impl/RedisServiceImpl.java @@ -0,0 +1,117 @@ +package com.muyu.redis.service.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.muyu.mqtt.dao.MessageDa; +import com.muyu.redis.demo.RedisData; +import com.muyu.redis.service.RedisService; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +import java.util.*; +import java.util.concurrent.TimeUnit; + +/** + * RedisServiceImpl + * + * @author Yangle + * Date 2024/6/26 16:49 + */ +@Service +@Log4j2 +public class RedisServiceImpl implements RedisService { + @Autowired + private RedisTemplate redisTemplate; + + @Override + public void set(RedisData redisData) { + + + List messageDas = new ArrayList<>(); + messageDas.add( + new MessageDa( + 1, + "vin", + 0, + 17, + "vIN", + "基础信息" + ) + ); + + messageDas.add( + new MessageDa( + 2, + "timestamp", + 17, + 30, + "时间戳", + "基础信息" + ) + ); + messageDas.add( + new MessageDa( + 3, + "longitude", + 38, + 41, + "经度", + "基础信息" + ) + ); + redisTemplate.opsForList().set(redisData.getVin(), 0, JSON.toJSONString(messageDas)); + } + + @Override + public String get(RedisData redisData) { + + String string = redisTemplate.opsForValue().get(redisData.getVin()); + log.error("string:{}", string); + return null; +// String index = redisTemplate.opsForList().index(redisData.getVin(), 0); +// log.error("index:{}",index); +// // 解析JSON字符串 +// JSONArray objects = JSON.parseArray(index); +// log.error("objects:{}",objects); +// for (Object object : objects) { +// JSONObject jsonObject = JSONObject.parseObject(object.toString()); +// String typeIdentification = jsonObject.getString("typeIdentification"); +// String initiationPosition = jsonObject.getString("initiationPosition"); +// String terminatePosition = jsonObject.getString("terminatePosition"); +// String messageLabel = jsonObject.getString("messageLabel"); +// +// log.error("typeIdentification:{}",typeIdentification); +// log.error("initiationPosition:{}",initiationPosition); +// log.error("terminatePosition:{}",terminatePosition); +// log.error("messageLabel:{}",messageLabel); +// } +// +// // 遍历JSON数组 +//// for (int i = 0; i < jsonArray.length(); i++) { +//// // 获取当前项 +//// JSONObject jsonObject = jsonArray.getJSONObject(i); +//// +//// // 提取initiationPosition的值 +//// String initiationPosition = jsonObject.getString("initiationPosition"); +//// +//// // 打印结果 +//// System.out.println("initiationPosition at index " + i + " is: " + initiationPosition); +//// } +// return null; + } + + /** + * 本地缓存 + */ + Cache localCache = Caffeine.newBuilder() + .initialCapacity(5) + .maximumSize(10) + //过期时间3秒钟 + .expireAfterWrite(5, TimeUnit.SECONDS) + .build(); +} diff --git a/src/main/java/com/muyu/test/Application.java b/src/main/java/com/muyu/test/Application.java new file mode 100644 index 0000000..774847f --- /dev/null +++ b/src/main/java/com/muyu/test/Application.java @@ -0,0 +1,27 @@ +package com.muyu.test; + +import lombok.RequiredArgsConstructor; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +@RequiredArgsConstructor +public class Application implements CommandLineRunner { + + private final UserRegistrationService userRegistrationService; + + public static void main(String[] args) { + SpringApplication app = new SpringApplication(Application.class); + app.setWebApplicationType(WebApplicationType.NONE); + app.run(args); + } + + @Override + public void run(String... args) { + // 在启动时注册新用户 + userRegistrationService.registerUser("小猪猪"); + } +} + diff --git a/src/main/java/com/muyu/test/UserNotificationService.java b/src/main/java/com/muyu/test/UserNotificationService.java new file mode 100644 index 0000000..2195a5e --- /dev/null +++ b/src/main/java/com/muyu/test/UserNotificationService.java @@ -0,0 +1,16 @@ +package com.muyu.test; + +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Service; + +@Service +public class UserNotificationService { + + @EventListener + public void handleUserRegisteredEvent(UserRegisteredEvent event) { + // 处理用户注册事件,例如发送通知 + String username = event.getUsername(); + System.out.println("Notification sent for user: " + username); + } +} + diff --git a/src/main/java/com/muyu/test/UserRegisteredEvent.java b/src/main/java/com/muyu/test/UserRegisteredEvent.java new file mode 100644 index 0000000..49526b2 --- /dev/null +++ b/src/main/java/com/muyu/test/UserRegisteredEvent.java @@ -0,0 +1,19 @@ +package com.muyu.test; + +import org.springframework.context.ApplicationEvent; + +public class UserRegisteredEvent extends ApplicationEvent { + private final String username; + + public UserRegisteredEvent(Object source, String username) { + super(source); + this.username = username; + } + + public String getUsername() { + return username; + } +} + + + diff --git a/src/main/java/com/muyu/test/UserRegistrationService.java b/src/main/java/com/muyu/test/UserRegistrationService.java new file mode 100644 index 0000000..ad90c93 --- /dev/null +++ b/src/main/java/com/muyu/test/UserRegistrationService.java @@ -0,0 +1,21 @@ +package com.muyu.test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.stereotype.Service; + +@Service +public class UserRegistrationService { + + @Autowired + private ApplicationEventPublisher eventPublisher; + + public void registerUser(String username) { + // 注册新用户逻辑 + // ... + + // 发布用户注册事件,这里使用 eventPublisher 将 预先定义好的事件交由 Spring 的Event Channel 管理 + eventPublisher.publishEvent(new UserRegisteredEvent(this, username)); + } +} +