iOS推送javaPNS源码解析八,多线程推送类
来源:互联网 发布:python 按任意键继续 编辑:程序博客网 时间:2024/05/16 23:50
多线程部分有两个模式:LIST模式和QUEUE模式。
list模式用于当开始后有大量的预定义的设备要推送
queue模式用于零星的推送,可能要等待推送消息过来再去推送。
重要接口:PushQueue,用于存储,清除,启动多线程推送操作。
具体实现类
public class NotificationThread implements Runnable, PushQueue
该类实现了多线程接口,构造函数和set/get方法自己看
/**
* Start the transmission thread.
*
* This method returns immediately, as the thread starts working on its own.
*/
public synchronized NotificationThread start() {
if (started) return this;
started = true;
try {
this.thread.start();
} catch (IllegalStateException e) {
}
return this;
}
/**
* Run method for the thread; do not call this method directly.
*/
public void run() {
switch (mode) {
case LIST: //list模式走runList方法
runList();
break;
case QUEUE:queue模式走runQueue方法
runQueue();
break;
default:
break;
}
}
private void runList() {
if (listener != null) listener.eventThreadStarted(this);
busy = true;
try {
int total = size();
notificationManager.initializeConnection(server);
for (int i = 0; i < total; i++) {
Device device;
Payload payload;
if (devices != null) {
device = devices.get(i);
payload = this.payload;
} else {
PayloadPerDevice message = messages.get(i);
device = message.getDevice();
payload = message.getPayload();
}
int message = newMessageIdentifier();
PushedNotification notification = notificationManager.sendNotification(device, payload, false, message);
notifications.add(notification);
try {
if (sleepBetweenNotifications > 0) Thread.sleep(sleepBetweenNotifications);
} catch (InterruptedException e) {
}
if (i != 0 && i % maxNotificationsPerConnection == 0) {
if (listener != null) listener.eventConnectionRestarted(this);
notificationManager.restartConnection(server);
}
}
notificationManager.stopConnection();
} catch (KeystoreException e) {
this.exception = e;
if (listener != null) listener.eventCriticalException(this, e);
} catch (CommunicationException e) {
this.exception = e;
if (listener != null) listener.eventCriticalException(this, e);
}
busy = false;
if (listener != null) listener.eventThreadFinished(this);
/* Also notify the parent NotificationThreads, so that it can determine when all threads have finished working */
if (this.thread.getThreadGroup() instanceof NotificationThreads) ((NotificationThreads) this.thread.getThreadGroup()).threadFinished(this);
}
private void runQueue() {
if (listener != null) listener.eventThreadStarted(this);
try {
notificationManager.initializeConnection(server);
int notificationsPushed = 0;
while (mode == MODE.QUEUE) {
while (!messages.isEmpty()) {//循环推送,在此过程中异步加入messages
busy = true;
PayloadPerDevice message = messages.get(0);
messages.remove(message);
notificationsPushed++;
int messageId = newMessageIdentifier();
PushedNotification notification = notificationManager.sendNotification(message.getDevice(), message.getPayload(), false, messageId);
notifications.add(notification);
try {
if (sleepBetweenNotifications > 0) Thread.sleep(sleepBetweenNotifications);
} catch (InterruptedException e) {
}
if (notificationsPushed != 0 && notificationsPushed % maxNotificationsPerConnection == 0) {
if (listener != null) listener.eventConnectionRestarted(this);
notificationManager.restartConnection(server);
}
busy = false;
}
try {
Thread.sleep(10 * 1000);
} catch (Exception e) {
}
}
notificationManager.stopConnection();
} catch (KeystoreException e) {
this.exception = e;
if (listener != null) listener.eventCriticalException(this, e);
} catch (CommunicationException e) {
this.exception = e;
if (listener != null) listener.eventCriticalException(this, e);
}
if (listener != null) listener.eventThreadFinished(this);
/* Also notify the parent NotificationThreads, so that it can determine when all threads have finished working */
if (this.thread.getThreadGroup() instanceof NotificationThreads) ((NotificationThreads) this.thread.getThreadGroup()).threadFinished(this);
}
public PushQueue add(Payload payload, String token) throws InvalidDeviceTokenFormatException {
return add(new PayloadPerDevice(payload, token));
}
public PushQueue add(Payload payload, Device device) {
return add(new PayloadPerDevice(payload, device));
}
public PushQueue add(PayloadPerDevice message) {
if (mode != MODE.QUEUE) return this;
try {//异步添加是在这里操作的
messages.add(message);
this.thread.interrupt();
} catch (Exception e) {
}
return this;
}
public class NotificationThreads extends ThreadGroup implements PushQueue
以线程组非线程池的方式进行多线程推送。
- iOS推送javaPNS源码解析八,多线程推送类
- iOS推送javaPNS源码解析七,推送信息类
- iOS推送javaPNS源码解析九,总结
- iOS推送javaPNS源码解析一,设备类
- iOS推送javaPNS源码解析二,消息体类
- iOS推送javaPNS源码解析三,服务器类
- iOS推送javaPNS源码解析四,证书管理类
- iOS推送javaPNS源码解析五,代理管理类
- iOS推送javaPNS源码解析六,连接管理类
- javapns 推送ios消息
- 多线程推送 push JavaPNS 2.1
- javapns实现apple ios推送
- Javapns-sdk16-2.2.1多线程推送
- 利用javapns对IOS进行推送
- javaPNS进阶-高级推送技巧
- JavaPns向APNS发送推送信息
- 使用JavaPns向APNs推送通知
- IOS push推送(javapns包与notnoop包的区别)
- 三秒更新一次头条信息
- 171105__我的Java编程思想printf,System.out.format()
- Leetcode题解-33. Search in Rotated Sorted Array
- WANGLUOqingqiuGj
- 进度条3
- iOS推送javaPNS源码解析八,多线程推送类
- Handler的运行机制
- 数据库E-R图的概念
- NOIP2017巴蜀模拟总结
- 最近的学习计划
- VR开发--(HTC Vive)设备一览
- Get与Post区别
- iOS推送javaPNS源码解析九,总结
- op的交流通路