package me.ifelseif.apns2.impl;

import me.ifelseif.apns2.ApnsHttp2Client;
import me.ifelseif.apns2.ApnsHttp2Service;
import me.ifelseif.apns2.ResponseListener;
import me.ifelseif.apns2.model.Apns2Config;
import me.ifelseif.apns2.model.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by linmingchang on 16/12/29.
 */
public class ApnsHttp2ServiceImpl implements ApnsHttp2Service {
    private static final Logger log = LoggerFactory.getLogger(ApnsHttp2ServiceImpl.class);
    private final ExecutorService service;
    private final ApnsHttp2ClientPool clientPool;

    public ApnsHttp2ServiceImpl(Apns2Config config) {
        service = Executors.newFixedThreadPool(config.getPoolSize());
        clientPool = ApnsHttp2ClientPool.newClientPool(config);
    }

    @Override
    public void push(String token, Notification notification, ResponseListener listener) {
        service.execute(new Runnable() {
            @Override
            public void run() {
                ApnsHttp2Client client = null;
                try {
                    client = clientPool.borrowClient();
                    client.push(token, notification, listener);
                } finally {
                    if (client != null) {
                        clientPool.returnClient(client);
                    }
                }
            }
        });
    }

    @Override
    public void shutdown() {
        service.shutdown();
        try {
            service.awaitTermination(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            log.error("Shutdown ApnsHttp2Service interrupted", e);
        }
        clientPool.close();
    }

}

@@ -1049,83 +1049,14 @@
 ute(
-new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A    
+()-%3E%7B%0A
     
@@ -1102,38 +1102,30 @@
             
- 
-   
 try %7B%0A
-    
             
@@ -1160,24 +1160,60 @@
 owClient();%0A
+                if(client != null)%7B%0A
             
@@ -1264,24 +1264,112 @@
 tener);%0A    
+            %7Delse%7B%0A                    log.error(%22can't get client%22);%0A                %7D%0A
             
@@ -1396,20 +1396,16 @@
         
- 
-   
 if (clie
@@ -1414,28 +1414,24 @@
  != null) %7B%0A
-    
             
@@ -1475,30 +1475,8 @@
 t);%0A
-                    %7D%0A
     

change Runable to lambda