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

以线程组非线程池的方式进行多线程推送。