ScheduledPushService.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package com.gyee.alarm.stomp;
  2. import com.gyee.alarm.model.vo.AlarmTag;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.scheduling.annotation.Scheduled;
  5. import org.springframework.stereotype.Service;
  6. import java.time.LocalDateTime;
  7. import java.time.format.DateTimeFormatter;
  8. /**
  9. * 定时推送服务
  10. */
  11. @Service
  12. public class ScheduledPushService {
  13. @Autowired
  14. private MessagePushService pushService;
  15. /**
  16. * 每分钟发送一次系统时间广播
  17. */
  18. @Scheduled(fixedRate = 1000)
  19. public void pushSystemTime() {
  20. String time = LocalDateTime.now().format(
  21. DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
  22. );
  23. ChatMessage message= new ChatMessage(ChatMessage.MessageType.LEAVE,
  24. "test1" + " 测试", "test2",null, LocalDateTime.now());
  25. pushService.pushToUser("test1",message);
  26. }
  27. /**
  28. * 每小时发送一次活跃用户统计
  29. */
  30. // @Scheduled(cron = "0 0 * * * *")
  31. @Scheduled(fixedRate = 1000)
  32. public void pushUserStats() {
  33. ChatMessage message= new ChatMessage(ChatMessage.MessageType.LEAVE,
  34. "test2" + " 离开了聊天室", "test2",null, LocalDateTime.now());
  35. pushService.broadcastToAll(message);
  36. }
  37. }