From 58d9c669391365becce86ed225c85c8af7134f85 Mon Sep 17 00:00:00 2001 From: wangqing <408564126@qq.com> Date: Tue, 23 Jan 2024 12:56:37 +0800 Subject: [PATCH] user Message with rabbitMQ --- .../src/main/java/dto/UserMessageDto.java | 51 ++++++++++++++++++ .../controller/UserMessageController.java | 45 ++++++++++++++++ .../com/luoo/user/dao/UserMessageDao.java | 11 ++++ .../user/listener/UserMessageListener.java | 29 ++++++++++ .../java/com/luoo/user/pojo/UserMessage.java | 53 +++++++++++++++++++ .../luoo/user/service/UserMessageService.java | 42 +++++++++++++++ 6 files changed, 231 insertions(+) create mode 100644 luoo_common/src/main/java/dto/UserMessageDto.java create mode 100644 luoo_user/src/main/java/com/luoo/user/controller/UserMessageController.java create mode 100644 luoo_user/src/main/java/com/luoo/user/dao/UserMessageDao.java create mode 100644 luoo_user/src/main/java/com/luoo/user/listener/UserMessageListener.java create mode 100644 luoo_user/src/main/java/com/luoo/user/pojo/UserMessage.java create mode 100644 luoo_user/src/main/java/com/luoo/user/service/UserMessageService.java diff --git a/luoo_common/src/main/java/dto/UserMessageDto.java b/luoo_common/src/main/java/dto/UserMessageDto.java new file mode 100644 index 0000000..43d383d --- /dev/null +++ b/luoo_common/src/main/java/dto/UserMessageDto.java @@ -0,0 +1,51 @@ +package dto; + + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import java.io.Serializable; +import java.util.Date; + +@Data +public class UserMessageDto implements Serializable { + + private static final long serialVersionUID = 1L; + + + /** + * 消息ID + */ + private String messageId; + + /** + * 消息ID + */ + private String userId; + + + /** + * 消息标题 + */ + private String title; + + /** + * 消息内容 + */ + private String content; + + + /** + * 是否已读 0为未读 1为已读 + */ + private Integer havaRead; + + + /** + * 发送时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date sendTime; +} diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserMessageController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserMessageController.java new file mode 100644 index 0000000..73d067f --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/controller/UserMessageController.java @@ -0,0 +1,45 @@ +package com.luoo.user.controller; + + +import api.Result; +import com.luoo.user.pojo.UserMessage; +import com.luoo.user.service.UserMessageService; +import dto.UserMessageDto; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/userMessage") +public class UserMessageController { + + @Autowired + private UserMessageService userMessageService; + + + /** + * 测试时用的,忽略这个方法 + * * @param userMessageDto + * @return + */ + + @PostMapping("/sendUserMessage") + public Result sendUserMessage(@RequestBody UserMessageDto userMessageDto) { + + userMessageService.sendUserMessage(userMessageDto); + return Result.success(); + } + + + /**测试时用的,获取当前登录用户ID请走TOKEN + * 根据登录的用户ID获取消息 + * @return + */ + @GetMapping("/list/{userId}") + public Result list(@PathVariable String userId){ + List list = userMessageService.findByUserId(userId); + return Result.success(list); + } + +} diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserMessageDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserMessageDao.java new file mode 100644 index 0000000..144ef15 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dao/UserMessageDao.java @@ -0,0 +1,11 @@ +package com.luoo.user.dao; + +import com.luoo.user.pojo.UserMessage; +import org.springframework.data.mongodb.repository.MongoRepository; + +import java.util.List; + +public interface UserMessageDao extends MongoRepository { + + List findAllByUserId(String userId); +} diff --git a/luoo_user/src/main/java/com/luoo/user/listener/UserMessageListener.java b/luoo_user/src/main/java/com/luoo/user/listener/UserMessageListener.java new file mode 100644 index 0000000..79a706a --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/listener/UserMessageListener.java @@ -0,0 +1,29 @@ +package com.luoo.user.listener; + + +import com.luoo.user.dao.UserMessageDao; +import com.luoo.user.pojo.UserMessage; +import dto.UserMessageDto; +import org.springframework.amqp.rabbit.annotation.RabbitHandler; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +@RabbitListener(queues = "userMessage") +public class UserMessageListener { + + @Autowired + private UserMessageDao userMessageDao; + + @RabbitHandler + public void executeSendUserMessage(UserMessageDto userMessageDto) { + + UserMessage userMessage = new UserMessage(); + BeanUtils.copyProperties(userMessageDto,userMessage); + + userMessageDao.save(userMessage); + } + +} diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/UserMessage.java b/luoo_user/src/main/java/com/luoo/user/pojo/UserMessage.java new file mode 100644 index 0000000..b209cbb --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/pojo/UserMessage.java @@ -0,0 +1,53 @@ +package com.luoo.user.pojo; + + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.springframework.data.annotation.Id; +import org.springframework.format.annotation.DateTimeFormat; + +import java.io.Serializable; +import java.util.Date; + +@Data +public class UserMessage implements Serializable { + + private static final long serialVersionUID = 1L; + + + /** + * 消息ID + */ + @Id + private String messageId; + + /** + * 消息ID + */ + private String userId; + + + /** + * 消息标题 + */ + private String title; + + /** + * 消息内容 + */ + private String content; + + + /** + * 是否已读 0为未读 1为已读 + */ + private Integer havaRead; + + + /** + * 发送时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date sendTime; +} diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserMessageService.java b/luoo_user/src/main/java/com/luoo/user/service/UserMessageService.java new file mode 100644 index 0000000..eb12109 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/service/UserMessageService.java @@ -0,0 +1,42 @@ +package com.luoo.user.service; + +import com.luoo.user.dao.UserMessageDao; +import com.luoo.user.pojo.UserMessage; +import dto.UserMessageDto; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import util.IdWorker; + +import java.util.Date; +import java.util.List; + +@Service +public class UserMessageService { + + @Autowired + private RabbitTemplate rabbitTemplate; + + @Autowired + private UserMessageDao userMessageDao; + @Autowired + private IdWorker idWorker; + public void sendUserMessage(UserMessageDto userMessageDto) { + + userMessageDto.setMessageId(idWorker.nextId()+""); + userMessageDto.setHavaRead(0); + userMessageDto.setSendTime(new Date()); + rabbitTemplate.convertAndSend("userMessage",userMessageDto); + } + + + public List list() { + + return userMessageDao.findAll(); + } + + public List findByUserId(String userId) { + + return userMessageDao.findAllByUserId(userId); + } +}