如何批量排队记录并在不同的线程中执行它们并等待它结束?

Posted

技术标签:

【中文标题】如何批量排队记录并在不同的线程中执行它们并等待它结束?【英文标题】:How to batch queue records and execute them in a different thread and wait till it;s over? 【发布时间】:2016-04-05 11:05:50 【问题描述】:

我正在使用 PushSharp 版本 PushSharp 4.0.4。

我在 Windows 应用程序中使用它。

我主要有三种方法

1-BroadCastToAll

2- BrodcatsToios

3- BrodcatsToAndriod

我有一个叫做发送的按钮。在按钮的单击事件上。我打电话给

BroadCastToAll 函数。

 private void btnSend_Click(object sender, EventArgs e)
 
    var url = "www.mohammad-jouhari.com"
    var promotion = new Promotion ();
    BroadCastToAll(promotion, url);
 

这是 BrodcastToAll 函数

 public void BroadCastToAll(Promotion promotion, string url)
 
    var deviceCatalogs = GetDeviceCatalog();
    BroadCastToIOS(promotion, url, deviceCatalogs.Where(d => d.OS == "IOS").ToList());
    BroadCastToAndriod(promotion, url, deviceCatalogs.Where(d => d.OS == "android").ToList());
 

这是 BrodcastToIOS 函数

 public void BroadCastToIOS(Promotion promotion, string url, List<DeviceCatalog> deviceCatalogs)
 
    if (deviceCatalogs.Count == 0)
       return;
       lock (_lock)// Added this lock because there is a potential chance that PushSharp callback execute during registering devices
       
          QueueAllAppleDevicesForNotification(promotion, url, deviceCatalogs, logsMessage);
       
 

这是 BrodcastToAndriod 函数

 public void BroadCastToAndriod(Promotion promotion, string url, List<DeviceCatalog> deviceCatalogs)
 
    if (deviceCatalogs.Count == 0)
        return;
        lock (_lock)// Added this lock because there is a potential chance that PushSharp callback execute during registering devices
        
           QueueAllGcmDevicesForNotification(promotion, url, deviceCatalogs, logsMessage);
        
 

这里是 QueueAllAppleDevicesForNotification 函数

  private void QueueAllAppleDevicesForNotification(Promotion promotion, string url, List<DeviceCatalog> deviceCatalogs)
        
            var apnsServerEnviroment = UseProductionCertificate ? ApnsConfiguration.ApnsServerEnvironment.Production : ApnsConfiguration.ApnsServerEnvironment.Sandbox;
            var fileService = new FileService();
            var filePath = Application.StartupPath+ "/Certifcates/" +  (UseProductionCertificate ? "prod.p12" : "dev.p12");
            var buffer = fileService.GetFileBytes(filePath);
            var config = new ApnsConfiguration(apnsServerEnviroment, buffer, APPLE_CERTIFICATE_PWD);
            apnsServiceBroker = new ApnsServiceBroker(config);
            apnsServiceBroker.OnNotificationFailed += (notification, aggregateEx) => 
                aggregateEx.Handle (ex => 
                    // Log the Resposne
                );

            ;
            apnsServiceBroker.OnNotificationSucceeded += (notification) => 
                   // Log The Response 
            ;
            apnsServiceBroker.Start();
            foreach (var deviceToken in deviceCatalogs) 
                var title = GetTitle(promotion, deviceToken);
                //title += DateTime.UtcNow.TimeOfDay.ToString();
                var NotificationPayLoadObject = new NotificationPayLoadObjectApple();
                NotificationPayLoadObject.aps.alert = title;
                NotificationPayLoadObject.aps.badge = 0;
                NotificationPayLoadObject.aps.sound = "default";
                NotificationPayLoadObject.url = url;
                var payLoad = JObject.Parse(JsonConvert.SerializeObject(NotificationPayLoadObject));
                apnsServiceBroker.QueueNotification(new ApnsNotification
                
                    Tag = this,
                    DeviceToken = deviceToken.UniqueID,
                    Payload = payLoad
                );
            
            var fbs = new FeedbackService(config);
            fbs.FeedbackReceived += (string deviceToken, DateTime timestamp) =>
            
                // This Token is no longer avaialble in APNS
                new DeviceCatalogService().DeleteExpiredIosDevice(deviceToken);
            ;
            fbs.Check();

            apnsServiceBroker.Stop();
        

这里是 QueueAllGcmDevicesForNotification

private void QueueAllGcmDevicesForNotification(Promotion promotion, string url, List<DeviceCatalog> deviceCatalogs, )
    
        var config = new GcmConfiguration(ANDROID_SENDER_ID, ANDROID_SENDER_AUTH_TOKEN, ANDROID_APPLICATION_ID_PACKAGE_NAME);
        gcmServiceBroker = new GcmServiceBroker(config);
        gcmServiceBroker.OnNotificationFailed += (notification, aggregateEx) => 
            aggregateEx.Handle (ex => 
                // Log Response
                return true;
            );
        ;

        gcmServiceBroker.OnNotificationSucceeded += (notification) => 
           // Log Response
        ;
        var title = GetTitle(shopexPromotion);
        gcmServiceBroker.Start ();
        foreach (var regId in deviceCatalogs) 
            var NotificationPayLoadObject = new NotificationPayLoadObjectAndriod(url, title, "7", promotion.ImageUrl);
            var payLoad = JObject.Parse(JsonConvert.SerializeObject(NotificationPayLoadObject));
            gcmServiceBroker.QueueNotification(new GcmNotification
            
                RegistrationIds = new List<string>  
                    regId.UniqueID
                ,
                Data = payLoad
            );
        
        gcmServiceBroker.Stop();

    

现在当我点击发送按钮时。事件将开始执行。

将调用 BrodcastToAll 函数。我先调用 BrodcastToIOS 设备,然后再调用 BrodcatsToAndriod。

有什么方法可以调用 BrodcastToIOS 并等到所有设备都已排队并且库已推送通知并且回调事件完全触发然后开始执行 BrodcastToAndriod 函数?

我需要添加哪些代码行?

还有有没有办法批量处理要排队的设备数量?

例如。

假设我有 1000 个设备

500个IOS

500 安卓

我可以为 IOS 排队 100、100,100,100,100 以及何时完成

我为 Andriod 排队 100,100,100,100,100。

任何帮助表示赞赏。

谢谢。

【问题讨论】:

【参考方案1】:

默认情况下,对broker.Stop () 的调用将阻塞,直到队列中的所有通知都已处理完毕。

【讨论】:

我用了它并没有阻塞 github.com/Redth/PushSharp/blob/master/PushSharp.Core/… 正在阻塞... 好的,他正在使用 Task.WaitAll (all);我需要在我的代码中使用它吗? 我在发送 ios 通知时恢复了很多 ConnectionError。我检查了这个链接github.com/Redth/PushSharp/issues/672,我有一个锁定声明,但我仍然收到错误。

以上是关于如何批量排队记录并在不同的线程中执行它们并等待它结束?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 MySQL 中进行批量插入

PHP 请求“排队”

线程的通信(生产者消费者问题)

线程的通信(生产者消费者问题)

线程池的简单实现

js事件循环运行机制