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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.walker.push.mail;
 
import com.walker.infrastructure.utils.WaitConsoleInput;
import com.walker.push.Notification;
import com.walker.push.NotificationChannel;
import com.walker.push.util.PushUtils;
import org.junit.Test;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;
 
public class TestMail {
 
//    @Test
    public void testMultipleChannelParallel(){
        DefaultSystemPush systemPush = new DefaultSystemPush();
        systemPush.startup();
 
        WebsocketPush websocketPush = new WebsocketPush();
        websocketPush.startup();
 
        DefaultPushManager pushManager = new DefaultPushManager();
        pushManager.setThreadPoolTaskExecutor(this.acquireThreadPoolTask());
        pushManager.register(systemPush);
        pushManager.register(websocketPush);
 
        List<NotificationChannel> channels = new ArrayList<>(4);
        channels.add(NotificationChannel.System);
        channels.add(NotificationChannel.WebSocket);
 
        List<String> userList = new ArrayList<>(2);
        userList.add("shikeying");
 
        Notification notification = PushUtils.acquireNotification("标题", "sfsfd", "平台", userList, "creator", true, channels, false);
        pushManager.push(notification, null);
        WaitConsoleInput.waitInput();
    }
 
//    @Test
    public void testMailPush(){
        MailPush mailPush = new DefaultMailPush();
        mailPush.setMailServerHost("smtp.126.com");
        mailPush.setMailServerPort("25");
        mailPush.setFromAddress("hnzzzhsl@126.com");
        mailPush.setFromPassword("UWBUXNLFJEANRCXX");
        mailPush.startup();
 
        ThreadPoolTaskExecutor executor = this.acquireThreadPoolTask();
 
        DefaultPushManager pushManager = new DefaultPushManager();
        pushManager.setThreadPoolTaskExecutor(executor);
        pushManager.register(mailPush);
//        pushManager.addStrategy();
 
        Notification notification = PushUtils.acquireEmailNotificationOne("测试邮件标题"
                , "您的验证码是:0098", "123333@126.com", "shikeying", "0");
 
        pushManager.push(notification, null);
 
        WaitConsoleInput.waitInput();
//        try {
//            PushResult pushResult = mailPush.push(notification);
//            System.out.println(pushResult);
//        } catch (PushException e) {
//            throw new RuntimeException(e);
//        }
    }
 
    private ThreadPoolTaskExecutor acquireThreadPoolTask(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(4);
        executor.setCorePoolSize(2);
        executor.setQueueCapacity(16);
        executor.setKeepAliveSeconds(300);
        // 线程池对拒绝任务(无线程可用)的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}