快速返回IM服务的调用

This commit is contained in:
imhao183
2021-03-12 12:21:42 +08:00
parent 27b6efcaee
commit 4ede98636c
3 changed files with 59 additions and 34 deletions

View File

@@ -13,6 +13,11 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Service
public class AndroidPushServiceImpl implements AndroidPushService {
private static final Logger LOG = LoggerFactory.getLogger(AndroidPushServiceImpl.class);
@@ -34,32 +39,45 @@ public class AndroidPushServiceImpl implements AndroidPushService {
@Autowired
private FCMPush fcmPush;
private ExecutorService executorService = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors() * 100,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
@Override
public Object push(PushMessage pushMessage) {
LOG.info("Android push {}", new Gson().toJson(pushMessage));
switch (pushMessage.getPushType()) {
case AndroidPushType.ANDROID_PUSH_TYPE_XIAOMI:
xiaomiPush.push(pushMessage);
break;
case AndroidPushType.ANDROID_PUSH_TYPE_HUAWEI:
hmsPush.push(pushMessage);
break;
case AndroidPushType.ANDROID_PUSH_TYPE_MEIZU:
meiZuPush.push(pushMessage);
break;
case AndroidPushType.ANDROID_PUSH_TYPE_VIVO:
vivoPush.push(pushMessage);
break;
case AndroidPushType.ANDROID_PUSH_TYPE_OPPO:
oppoPush.push(pushMessage);
break;
case AndroidPushType.ANDROID_PUSH_TYPE_FCM:
fcmPush.push(pushMessage);
break;
default:
LOG.info("unknown push type");
break;
}
final long start = System.currentTimeMillis();
executorService.execute(()->{
long now = System.currentTimeMillis();
if (now - start > 15000) {
LOG.error("等待太久,消息抛弃");
return;
}
switch (pushMessage.getPushType()) {
case AndroidPushType.ANDROID_PUSH_TYPE_XIAOMI:
xiaomiPush.push(pushMessage);
break;
case AndroidPushType.ANDROID_PUSH_TYPE_HUAWEI:
hmsPush.push(pushMessage);
break;
case AndroidPushType.ANDROID_PUSH_TYPE_MEIZU:
meiZuPush.push(pushMessage);
break;
case AndroidPushType.ANDROID_PUSH_TYPE_VIVO:
vivoPush.push(pushMessage);
break;
case AndroidPushType.ANDROID_PUSH_TYPE_OPPO:
oppoPush.push(pushMessage);
break;
case AndroidPushType.ANDROID_PUSH_TYPE_FCM:
fcmPush.push(pushMessage);
break;
default:
LOG.info("unknown push type");
break;
}
});
return "ok";
}
}

View File

@@ -28,7 +28,6 @@ import static java.lang.System.exit;
@Component
public class ApnsServer {
private static final Logger LOG = LoggerFactory.getLogger(ApnsServer.class);
private static ExecutorService mExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 5);
final SimpleMeterRegistry meterRegistry = new SimpleMeterRegistry();
final MicrometerApnsClientMetricsListener productMetricsListener =
@@ -90,13 +89,6 @@ public class ApnsServer {
public void pushMessage(PushMessage pushMessage) {
final long start = System.currentTimeMillis();
mExecutor.submit(()-> {
long now = System.currentTimeMillis();
if (now - start > 5000) {
LOG.error("等待太久,消息抛弃");
return;
}
ApnsClient service;
if (pushMessage.getPushType() == IOSPushType.IOS_PUSH_TYPE_DISTRIBUTION) {
if (!mConfig.voipFeature || pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_NORMAL || StringUtils.isEmpty(pushMessage.getVoipDeviceToken())) {
@@ -254,7 +246,5 @@ public class ApnsServer {
}
}
});
});
}
}

View File

@@ -7,16 +7,33 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Service
public class IOSPushServiceImpl implements IOSPushService {
private static final Logger LOG = LoggerFactory.getLogger(IOSPushServiceImpl.class);
@Autowired
public ApnsServer apnsServer;
private ExecutorService executorService = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors() * 100,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
@Override
public Object push(PushMessage pushMessage) {
LOG.info("iOS push {}", new Gson().toJson(pushMessage));
apnsServer.pushMessage(pushMessage);
final long start = System.currentTimeMillis();
executorService.execute(()->{
long now = System.currentTimeMillis();
if (now - start > 15000) {
LOG.error("等待太久,消息抛弃");
return;
}
apnsServer.pushMessage(pushMessage);
});
return "OK";
}
}