user Message with rabbitMQ

main
wangqing 1 year ago
parent 8521863255
commit 58d9c66939

@ -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;
}

@ -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();
}
/**IDTOKEN
* ID
* @return
*/
@GetMapping("/list/{userId}")
public Result list(@PathVariable String userId){
List list = userMessageService.findByUserId(userId);
return Result.success(list);
}
}

@ -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<UserMessage,String> {
List<UserMessage> findAllByUserId(String userId);
}

@ -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);
}
}

@ -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;
}

@ -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);
}
}
Loading…
Cancel
Save