Pu Zhibing
昨天 cd972838ea38c1bd5e742dc5298f32a6b8d6ca71
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.sinata.push.util;
 
import com.sinata.push.util.echo.Method;
import com.sinata.push.util.echo.NettyChannelMap;
import com.sinata.push.util.echo.NettyMsg;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
 
/**
 * @author zhibing.pu
 * @Date 2025/6/25 20:33
 */
@Slf4j
@Component
public class TaskUtil {
    
    
    @Scheduled(fixedRate = 1000)
    public void taskMinute(){
        NettyChannelMap.map.keySet().forEach(key -> {
            ChannelHandlerContext context = NettyChannelMap.map.get(key);
            Channel channel = context.channel();
            if(context != null && channel.isActive()){
                String s = NettyMsg.setMsg(Method.ok, new HashMap<String, Object>());
                context.writeAndFlush(Unpooled.copiedBuffer((s).getBytes()));
                log.info("send channel:{}", key);
            }else{
                NettyChannelMap.map.remove(key);
                log.info("remove channel:{}", key);
            }
        });
    }
}