3030 */
3131package com .notnoop .apns .internal ;
3232
33- import java .io .DataInputStream ;
3433import java .io .EOFException ;
3534import java .io .IOException ;
35+ import java .io .InputStream ;
3636import java .net .InetSocketAddress ;
3737import java .net .Proxy ;
3838import java .net .Socket ;
3939import java .util .LinkedList ;
4040import java .util .Queue ;
4141import java .util .concurrent .ConcurrentLinkedQueue ;
42+ import java .util .concurrent .Executors ;
43+ import java .util .concurrent .ThreadFactory ;
44+
4245import javax .net .SocketFactory ;
4346import javax .net .ssl .SSLSocketFactory ;
4447import com .notnoop .apns .ApnsDelegate ;
@@ -67,6 +70,7 @@ public class ApnsConnectionImpl implements ApnsConnection {
6770 private final ApnsDelegate delegate ;
6871 private int cacheLength ;
6972 private final boolean errorDetection ;
73+ private final ThreadFactory threadFactory ;
7074 private final boolean autoAdjustCacheLength ;
7175 private final ConcurrentLinkedQueue <ApnsNotification > cachedNotifications , notificationsBuffer ;
7276
@@ -80,12 +84,12 @@ private ApnsConnectionImpl(SocketFactory factory, String host, int port, Reconne
8084
8185 private ApnsConnectionImpl (SocketFactory factory , String host , int port , Proxy proxy , String proxyUsername , String proxyPassword ,
8286 ReconnectPolicy reconnectPolicy , ApnsDelegate delegate ) {
83- this (factory , host , port , proxy , proxyUsername , proxyPassword , reconnectPolicy , delegate , false ,
87+ this (factory , host , port , proxy , proxyUsername , proxyPassword , reconnectPolicy , delegate , false , null ,
8488 ApnsConnection .DEFAULT_CACHE_LENGTH , true , 0 , 0 );
8589 }
8690
8791 public ApnsConnectionImpl (SocketFactory factory , String host , int port , Proxy proxy , String proxyUsername , String proxyPassword ,
88- ReconnectPolicy reconnectPolicy , ApnsDelegate delegate , boolean errorDetection , int cacheLength ,
92+ ReconnectPolicy reconnectPolicy , ApnsDelegate delegate , boolean errorDetection , ThreadFactory tf , int cacheLength ,
8993 boolean autoAdjustCacheLength , int readTimeout , int connectTimeout ) {
9094 this .factory = factory ;
9195 this .host = host ;
@@ -94,6 +98,7 @@ public ApnsConnectionImpl(SocketFactory factory, String host, int port, Proxy pr
9498 this .delegate = delegate == null ? ApnsDelegate .EMPTY : delegate ;
9599 this .proxy = proxy ;
96100 this .errorDetection = errorDetection ;
101+ this .threadFactory = tf == null ? defaultThreadFactory () : tf ;
97102 this .cacheLength = cacheLength ;
98103 this .autoAdjustCacheLength = autoAdjustCacheLength ;
99104 this .readTimeout = readTimeout ;
@@ -104,25 +109,38 @@ public ApnsConnectionImpl(SocketFactory factory, String host, int port, Proxy pr
104109 notificationsBuffer = new ConcurrentLinkedQueue <ApnsNotification >();
105110 }
106111
112+ private ThreadFactory defaultThreadFactory () {
113+ return new ThreadFactory () {
114+ ThreadFactory wrapped = Executors .defaultThreadFactory ();
115+ @ Override
116+ public Thread newThread ( Runnable r )
117+ {
118+ Thread result = wrapped .newThread (r );
119+ result .setName ("MonitoringThread" );
120+ result .setDaemon (true );
121+ return result ;
122+ }
123+ };
124+ }
125+
107126 public synchronized void close () {
108127 Utilities .close (socket );
109128 }
110129
111130 private void monitorSocket (final Socket socket ) {
112131 logger .debug ("Launching Monitoring Thread for socket {}" , socket );
113132
114- class MonitoringThread extends Thread {
133+ Thread t = threadFactory . newThread ( new Runnable () {
115134 final static int EXPECTED_SIZE = 6 ;
116135
117136 @ SuppressWarnings ("InfiniteLoopStatement" )
118137 @ Override
119138 public void run () {
120139 logger .debug ("Started monitoring thread" );
121140 try {
122-
123- DataInputStream in ;
141+ InputStream in ;
124142 try {
125- in = new DataInputStream ( socket .getInputStream () );
143+ in = socket .getInputStream ();
126144 } catch (IOException ioe ) {
127145 in = null ;
128146 }
@@ -213,7 +231,7 @@ public void run() {
213231 * @return true if a packet as been read, false if the stream was at EOF right at the beginning.
214232 * @throws IOException When a problem occurs, especially EOFException when there's an EOF in the middle of the packet.
215233 */
216- private boolean readPacket (final DataInputStream in , final byte [] bytes ) throws IOException {
234+ private boolean readPacket (final InputStream in , final byte [] bytes ) throws IOException {
217235 final int len = bytes .length ;
218236 int n = 0 ;
219237 while (n < len ) {
@@ -231,10 +249,7 @@ private boolean readPacket(final DataInputStream in, final byte[] bytes) throws
231249 }
232250 return true ;
233251 }
234- }
235- Thread t = new MonitoringThread ();
236- t .setName ("MonitoringThread" );
237- t .setDaemon (true );
252+ });
238253 t .start ();
239254 }
240255
@@ -353,7 +368,7 @@ private void cacheNotification(ApnsNotification notification) {
353368
354369 public ApnsConnectionImpl copy () {
355370 return new ApnsConnectionImpl (factory , host , port , proxy , proxyUsername , proxyPassword , reconnectPolicy .copy (), delegate ,
356- errorDetection , cacheLength , autoAdjustCacheLength , readTimeout , connectTimeout );
371+ errorDetection , threadFactory , cacheLength , autoAdjustCacheLength , readTimeout , connectTimeout );
357372 }
358373
359374 public void testConnection () throws NetworkIOException {
0 commit comments