shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.walker.push.support;
 
import com.walker.push.AbstractPushManager;
import com.walker.push.Notification;
import com.walker.push.PushException;
import com.walker.push.PushResult;
import com.walker.push.Pushable;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
import java.util.List;
 
/**
 * 异步任务执行的推送管理器实现。
 * <p>这些主要是针对推送者为'同步模式'的情况。</p>
 * @author 时克英
 * @date 2023-04-24
 */
public abstract class AsyncPushManager extends AbstractPushManager {
 
    @Override
    protected PushResult pushOnce(List<Pushable> pushableList, Notification notification) throws PushException {
        if(this.threadPoolTaskExecutor == null){
            throw new IllegalStateException("threadPoolTaskExecutor未设置");
        }
        this.threadPoolTaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    long startTime = System.nanoTime();
                    invokePush(pushableList.get(0), notification);
                    logger.debug("异步时间:" + (System.nanoTime() - startTime));
                } catch (PushException e) {
                    logger.error("推送失败,id=" + e.getMessageId(), e);
                    throw new RuntimeException("推送(异步)失败:" + e.getMessage(), e);
                }
            }
        });
        return null;
    }
 
//    private class InterTask implements Runnable{
//        private Pushable pushable;
//        private Notification notification;
//
//        public InterTask(Pushable pushable, Notification notification){
//            this.pushable = pushable;
//            this.notification = notification;
//        }
//
//        @Override
//        public void run() {
//            try {
//                invokePush(pushable, notification);
//            } catch (PushException e) {
//                logger.error("推送失败,id=" + e.getMessageId(), e);
//                throw new RuntimeException("推送(异步)失败:" + e.getMessage(), e);
//            }
//        }
//    }
 
    public void setThreadPoolTaskExecutor(ThreadPoolTaskExecutor threadPoolTaskExecutor) {
        this.threadPoolTaskExecutor = threadPoolTaskExecutor;
    }
 
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
}