QueuedApnsService.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2009, Mahmood Ali.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Mahmood Ali. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. package com.notnoop.apns.internal;
  32. import java.util.Date;
  33. import java.util.Map;
  34. import java.util.concurrent.atomic.AtomicBoolean;
  35. import java.util.concurrent.BlockingQueue;
  36. import java.util.concurrent.Executors;
  37. import java.util.concurrent.LinkedBlockingQueue;
  38. import java.util.concurrent.ThreadFactory;
  39. import org.slf4j.Logger;
  40. import org.slf4j.LoggerFactory;
  41. import com.notnoop.apns.ApnsNotification;
  42. import com.notnoop.apns.ApnsService;
  43. import com.notnoop.exceptions.NetworkIOException;
  44. public class QueuedApnsService extends AbstractApnsService {
  45. private static final Logger logger = LoggerFactory.getLogger(QueuedApnsService.class);
  46. private ApnsService service;
  47. private BlockingQueue<ApnsNotification> queue;
  48. private AtomicBoolean started = new AtomicBoolean(false);
  49. public QueuedApnsService(ApnsService service) {
  50. this(service, null);
  51. }
  52. public QueuedApnsService(ApnsService service, final ThreadFactory tf) {
  53. super(null);
  54. this.service = service;
  55. this.queue = new LinkedBlockingQueue<ApnsNotification>();
  56. this.threadFactory = tf == null ? Executors.defaultThreadFactory() : tf;
  57. this.thread = null;
  58. }
  59. @Override
  60. public void push(ApnsNotification msg) {
  61. if (!started.get()) {
  62. throw new IllegalStateException("service hasn't be started or was closed");
  63. }
  64. queue.add(msg);
  65. }
  66. private final ThreadFactory threadFactory;
  67. private Thread thread;
  68. private volatile boolean shouldContinue;
  69. public void start() {
  70. if (started.getAndSet(true)) {
  71. // I prefer if we throw a runtime IllegalStateException here,
  72. // but I want to maintain semantic backward compatibility.
  73. // So it is returning immediately here
  74. return;
  75. }
  76. service.start();
  77. shouldContinue = true;
  78. thread = threadFactory.newThread(new Runnable() {
  79. public void run() {
  80. while (shouldContinue) {
  81. try {
  82. ApnsNotification msg = queue.take();
  83. service.push(msg);
  84. } catch (InterruptedException e) {
  85. // ignore
  86. } catch (NetworkIOException e) {
  87. // ignore: failed connect...
  88. } catch (Exception e) {
  89. // weird if we reached here - something wrong is happening, but we shouldn't stop the service anyway!
  90. logger.warn("Unexpected message caught... Shouldn't be here", e);
  91. }
  92. }
  93. }
  94. });
  95. thread.start();
  96. }
  97. public void stop() {
  98. started.set(false);
  99. shouldContinue = false;
  100. thread.interrupt();
  101. service.stop();
  102. }
  103. @Override
  104. public Map<String, Date> getInactiveDevices() throws NetworkIOException {
  105. return service.getInactiveDevices();
  106. }
  107. public void testConnection() throws NetworkIOException {
  108. service.testConnection();
  109. }
  110. }