8 changed files with 132 additions and 19 deletions
@ -0,0 +1,55 @@ |
|||||
|
package org.dromara.im.task; |
||||
|
|
||||
|
import cn.hutool.core.util.ObjectUtil; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.dromara.common.websocket.dto.WebSocketMessageDto; |
||||
|
import org.dromara.common.websocket.utils.WebSocketUtils; |
||||
|
import org.dromara.im.service.IImAgentService; |
||||
|
import org.springframework.scheduling.annotation.Scheduled; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.util.Date; |
||||
|
import java.util.List; |
||||
|
import java.util.Set; |
||||
|
import java.util.concurrent.ScheduledExecutorService; |
||||
|
import java.util.concurrent.TimeUnit; |
||||
|
|
||||
|
@Component |
||||
|
@RequiredArgsConstructor |
||||
|
public class ExpiredMsgSendTask { |
||||
|
|
||||
|
|
||||
|
private final IImAgentService imAgentService; |
||||
|
|
||||
|
private final ScheduledExecutorService scheduledExecutorService; |
||||
|
|
||||
|
/** |
||||
|
* 每30分钟执行一次 |
||||
|
* cron表达式: 0 0/30 * * * ? |
||||
|
*/ |
||||
|
@Scheduled(cron = "0 0/30 * * * ?") |
||||
|
// @Scheduled(cron = "* * * * * ?")
|
||||
|
// @Scheduled(cron = "*/15 * * * * ?")
|
||||
|
public void executeEvery30Minutes() { |
||||
|
// 在这里编写您需要每30分钟执行的操作
|
||||
|
// System.out.println("定时任务执行时间: " + new Date());
|
||||
|
|
||||
|
Set<Long> list = WebSocketUtils.getOnlineUserIds(); |
||||
|
|
||||
|
if(!list.isEmpty()) { |
||||
|
for (Long userId : list) { |
||||
|
//判断是否到期,到期7天内发送到期时间提醒
|
||||
|
String expiredMsg = imAgentService.checkIsExpiredTask(userId); |
||||
|
if (expiredMsg != null && ObjectUtil.isNotEmpty(expiredMsg)) { |
||||
|
scheduledExecutorService.schedule(() -> { |
||||
|
WebSocketMessageDto dto = new WebSocketMessageDto(); |
||||
|
dto.setMessage(expiredMsg); |
||||
|
dto.setSessionKeys(List.of(userId)); |
||||
|
WebSocketUtils.publishMessage(dto); |
||||
|
}, 5, TimeUnit.SECONDS); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue