/src/main/java/com/notnoop/apns/internal/ApnsPooledConnection.java

http://github.com/notnoop/java-apns · Java · 121 lines · 75 code · 14 blank · 32 comment · 3 complexity · 16a9f92d28c44a01364a10a7324cd186 MD5 · raw file

  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.concurrent.*;
  33. import com.notnoop.apns.ApnsNotification;
  34. import com.notnoop.exceptions.NetworkIOException;
  35. import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
  36. import org.slf4j.Logger;
  37. import org.slf4j.LoggerFactory;
  38. public class ApnsPooledConnection implements ApnsConnection {
  39. private static final Logger logger = LoggerFactory.getLogger(ApnsPooledConnection.class);
  40. private final ApnsConnection prototype;
  41. private final int max;
  42. private final ExecutorService executors;
  43. private final ConcurrentLinkedQueue<ApnsConnection> prototypes;
  44. public ApnsPooledConnection(ApnsConnection prototype, int max) {
  45. this(prototype, max, Executors.newFixedThreadPool(max));
  46. }
  47. public ApnsPooledConnection(ApnsConnection prototype, int max, ExecutorService executors) {
  48. this.prototype = prototype;
  49. this.max = max;
  50. this.executors = executors;
  51. this.prototypes = new ConcurrentLinkedQueue<ApnsConnection>();
  52. }
  53. private final ThreadLocal<ApnsConnection> uniquePrototype =
  54. new ThreadLocal<ApnsConnection>() {
  55. protected ApnsConnection initialValue() {
  56. ApnsConnection newCopy = prototype.copy();
  57. prototypes.add(newCopy);
  58. return newCopy;
  59. }
  60. };
  61. public void sendMessage(final ApnsNotification m) throws NetworkIOException {
  62. Future<Void> future = executors.submit(new Callable<Void>() {
  63. public Void call() throws Exception {
  64. uniquePrototype.get().sendMessage(m);
  65. return null;
  66. }
  67. });
  68. try {
  69. future.get();
  70. } catch (InterruptedException ie) {
  71. Thread.currentThread().interrupt();
  72. } catch (ExecutionException ee) {
  73. if (ee.getCause() instanceof NetworkIOException) {
  74. throw (NetworkIOException) ee.getCause();
  75. }
  76. }
  77. }
  78. public ApnsConnection copy() {
  79. // TODO: Should copy executor properly.... What should copy do
  80. // really?!
  81. return new ApnsPooledConnection(prototype, max);
  82. }
  83. public void close() {
  84. executors.shutdown();
  85. try {
  86. executors.awaitTermination(10, TimeUnit.SECONDS);
  87. } catch (InterruptedException e) {
  88. logger.warn("pool termination interrupted", e);
  89. }
  90. for (ApnsConnection conn : prototypes) {
  91. Utilities.close(conn);
  92. }
  93. Utilities.close(prototype);
  94. }
  95. public void testConnection() {
  96. prototype.testConnection();
  97. }
  98. public synchronized void setCacheLength(int cacheLength) {
  99. for (ApnsConnection conn : prototypes) {
  100. conn.setCacheLength(cacheLength);
  101. }
  102. }
  103. @SuppressFBWarnings(value = "UG_SYNC_SET_UNSYNC_GET", justification = "prototypes is a MT-safe container")
  104. public int getCacheLength() {
  105. return prototypes.peek().getCacheLength();
  106. }
  107. }