如何实现定时推送的具体方案

详细内容

详细内容大概分为4个部分,1.应用场景 2.遇到问题 3.设计 4.实现 5.运行效果

1.应用场景

需要定时推送数据,且轻量化的实现。

2.遇到问题

  • 如果启动一个定时器去定时轮询
  • (1)轮询效率比较低
  • (2)每次扫库,已经被执行过记录,仍然会被扫描(只是不会出现在结果集中),会做重复工作
  • (3)时效性不够好,如果每小时轮询一次,最差的情况下会有时间误差
  • 如何利用“延时消息”,对于每个任务只触发一次,保证效率的同时保证实时性,是今天要讨论的问题。

3.设计

高效延时消息,包含两个重要的数据结构:

  1. 环形队列,例如可以创建一个包含3600个slot的环形队列(本质是个数组)
  2. 任务集合,环上每一个slot是一个Set

同时,启动一个timer,这个timer每隔1s,在上述环形队列中移动一格,有一个Current Index指针来标识正在检测的slot。

Task结构中有两个很重要的属性:

  1. Cycle-Num:当Current Index第几圈扫描到这个Slot时,执行任务
  2. Task-Function:需要执行的任务指针

假设当前Current Index指向第一格,当有延时消息到达之后,例如希望3610秒之后,触发一个延时消息任务,只需:

  1. 计算这个Task应该放在哪一个slot,现在指向1,3610秒之后,应该是第11格,所以这个Task应该放在第11个slot的Set中
  2. 计算这个Task的Cycle-Num,由于环形队列是3600格(每秒移动一格,正好1小时),这个任务是3610秒后执行,所以应该绕3610/3600=1圈之后再执行,于是Cycle-Num=1

Current Index不停的移动,每秒移动到一个新slot,这个slot中对应的Set,每个Task看Cycle-Num是不是0:

  1. 如果不是0,说明还需要多移动几圈,将Cycle-Num减1
  2. 如果是0,说明马上要执行这个Task了,取出Task-Funciton执行(可以用单独的线程来执行Task),并把这个Task从Set中删除

使用了“延时消息”方案之后,“订单48小时后关闭评价”的需求,只需将在订单关闭时,触发一个48小时之后的延时消息即可:

  1. 无需再轮询全部订单,效率高
  2. 一个订单,任务只执行一次
  3. 时效性好,精确到秒(控制timer移动频率可以控制精度)

4.实现

首先写一个方案要理清楚自己的项目结构,我做了如下分层。

Interfaces , 这层里主要约束延迟消息队列的队列和消息任务行。

public interface IRingQueue<T>
{
    /// <summary>
    /// Add tasks [add tasks will automatically generate: task Id, task slot location, number of task cycles]
    /// </summary>
    /// <param name="delayTime">The specified task is executed after N seconds.</param>
    /// <param name="action">Definitions of callback</param>
    void Add(long delayTime,Action<T> action);
    /// <summary>
    /// Add tasks [add tasks will automatically generate: task Id, task slot location, number of task cycles]
    /// </summary>
    /// <param name="delayTime">The specified task is executed after N seconds.</param>
    /// <param name="action">Definitions of callback.</param>
    /// <param name="data">Parameters used in the callback function.</param>
    void Add(long delayTime, Action<T> action, T data);
    /// <summary>
    /// Add tasks [add tasks will automatically generate: task Id, task slot location, number of task cycles]
    /// </summary>
    /// <param name="delayTime"></param>
    /// <param name="action">Definitions of callback</param>
    /// <param name="data">Parameters used in the callback function.</param>
    /// <param name="id">Task ID, used when deleting tasks.</param>
    void Add(long delayTime, Action<T> action, T data, long id);
    /// <summary>
    /// Remove tasks [need to know: where the task is, which specific task].
    /// </summary>
    /// <param name="index">Task slot location</param>
    /// <param name="id">Task ID, used when deleting tasks.</param>
    void Remove(long id);
    /// <summary>
    /// Launch queue.
    /// </summary>
    void Start();
}
 public interface ITask
 {
 }

Achieves,这层里实现之前定义的接口,这里写成抽象类是为了后面方便扩展。

using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DelayMessageApp.Interfaces;
namespace DelayMessageApp.Achieves.Base
{
public abstract class BaseQueue<T> : IRingQueue<T>
{
    private long _pointer = 0L;
    private ConcurrentBag<BaseTask<T>>[] _arraySlot;
    private int ArrayMax;
    /// <summary>
    /// Ring queue.
    /// </summary>
    public ConcurrentBag<BaseTask<T>>[] ArraySlot
    {
        get { return _arraySlot ?? (_arraySlot = new ConcurrentBag<BaseTask<T>>[ArrayMax]); }
    }
    public BaseQueue(int arrayMax)
    {
        if (arrayMax < 60 && arrayMax % 60 == 0)
            throw new Exception("Ring queue length cannot be less than 60 and is a multiple of 60 .");
        ArrayMax = arrayMax;
    }
    public void Add(long delayTime, Action<T> action)
    {
        Add(delayTime, action, default(T));
    }
    public void Add(long delayTime,Action<T> action,T data)
    {
        Add(delayTime, action, data,0);
    }
    public void Add(long delayTime, Action<T> action, T data,long id)
    {
        NextSlot(delayTime, out long cycle, out long pointer);
        ArraySlot[pointer] =  ArraySlot[pointer] ?? (ArraySlot[pointer] = new ConcurrentBag<BaseTask<T>>());
        var baseTask = new BaseTask<T>(cycle, action, data,id);
        ArraySlot[pointer].Add(baseTask);
    }
    /// <summary>
    /// Remove tasks based on ID.
    /// </summary>
    /// <param name="id"></param>
    public void Remove(long id)
    {
        try
        {
            Parallel.ForEach(ArraySlot, (ConcurrentBag<BaseTask<T>> collection, ParallelLoopState state) =>
            {
                var resulTask = collection.FirstOrDefault(p => p.Id == id);
                if (resulTask != null)
                {
                    collection.TryTake(out resulTask);
                    state.Break();
                }
            });
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
    public void Start()
    {
        while (true)
        {
            RightMovePointer();
            Thread.Sleep(1000);
            Console.WriteLine(DateTime.Now.ToString());
        }
    }
    /// <summary>
    /// Calculate the information of the next slot.
    /// </summary>
    /// <param name="delayTime">Delayed execution time.</param>
    /// <param name="cycle">Number of turns.</param>
    /// <param name="index">Task location.</param>
    private void NextSlot(long delayTime, out long cycle,out long index)
    {
        try
        {
            var circle = delayTime / ArrayMax;
            var second = delayTime % ArrayMax;
            var current_pointer = GetPointer();
            var queue_index = 0L;
            if (delayTime - ArrayMax > ArrayMax)
            {
                circle = 1;
            }
            else if (second > ArrayMax)
            {
                circle += 1;
            }
            if (delayTime - circle * ArrayMax < ArrayMax)
            {
                second = delayTime - circle * ArrayMax;
            }
            if (current_pointer + delayTime >= ArrayMax)
            {
                cycle = (int)((current_pointer + delayTime) / ArrayMax);
                if (current_pointer + second - ArrayMax < 0)
                {
                    queue_index = current_pointer + second;
                }
                else if (current_pointer + second - ArrayMax > 0)
                {
                    queue_index = current_pointer + second - ArrayMax;
                }
            }
            else
            {
                cycle = 0;
                queue_index = current_pointer + second;
            }
            index = queue_index;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }
    /// <summary>
    /// Get the current location of the pointer.
    /// </summary>
    /// <returns></returns>
    private long GetPointer()
    {
        return Interlocked.Read(ref _pointer);
    }
    /// <summary>
    /// Reset pointer position.
    /// </summary>
    private void ReSetPointer()
    {
        Interlocked.Exchange(ref _pointer, 0);
    }
    /// <summary>
    /// Pointer moves clockwise.
    /// </summary>
    private void RightMovePointer()
    {
        try
        {
            if (GetPointer() >= ArrayMax - 1)
            {
                ReSetPointer();
            }
            else
            {
                Interlocked.Increment(ref _pointer);
            }
            var pointer = GetPointer();
            var taskCollection = ArraySlot[pointer];
            if (taskCollection == null || taskCollection.Count == 0) return;
            Parallel.ForEach(taskCollection, (BaseTask<T> task) =>
            {
                if (task.Cycle > 0)
                {
                    task.SubCycleNumber();
                }
                if (task.Cycle <= 0)
                {
                    taskCollection.TryTake(out task);
                    task.TaskAction(task.Data);
                }
            });
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }
}
}
using System;
using System.Threading;
using DelayMessageApp.Interfaces;
namespace DelayMessageApp.Achieves.Base
{
public class BaseTask<T> : ITask
{
    private long _cycle;
    private long _id;
    private T _data;
    public Action<T> TaskAction { get; set; }
    public long Cycle
    {
        get { return Interlocked.Read(ref _cycle); }
        set { Interlocked.Exchange(ref _cycle, value); }
    }
    public long Id
    {
        get { return _id; }
        set { _id = value; }
    }
    public T Data
    {
        get { return _data; }
        set { _data = value; }
    }
    public BaseTask(long cycle, Action<T> action, T data,long id)
    {
        Cycle = cycle;
        TaskAction = action;
        Data = data;
        Id = id;
    }
    public BaseTask(long cycle, Action<T> action,T data)
    {
        Cycle = cycle;
        TaskAction = action;
        Data = data;
    }
    public BaseTask(long cycle, Action<T> action)
    {
        Cycle = cycle;
        TaskAction = action;
    }
    public void SubCycleNumber()
    {
        Interlocked.Decrement(ref _cycle);
    }
}
}

Logic,这层主要实现调用逻辑,调用者最终只需要关心把任务放进队列并指定什么时候执行就行了,根本不需要关心其它的任何信息。

public static void Start()
{
    //1.Initialize queues of different granularity.
    IRingQueue<NewsModel> minuteRingQueue = new MinuteQueue<NewsModel>();
    //2.Open thread.
    var lstTasks = new List<Task>
    {
        Task.Factory.StartNew(minuteRingQueue.Start)
    };
    //3.Add tasks performed in different periods.
    minuteRingQueue.Add(5, new Action<NewsModel>((NewsModel newsObj) =>
    {
        Console.WriteLine(newsObj.News);
    }), new NewsModel() { News = "Trump's visit to China!" });
    minuteRingQueue.Add(10, new Action<NewsModel>((NewsModel newsObj) =>
    {
        Console.WriteLine(newsObj.News);
    }), new NewsModel() { News = "Putin Pu's visit to China!" });
    minuteRingQueue.Add(60, new Action<NewsModel>((NewsModel newsObj) =>
    {
        Console.WriteLine(newsObj.News);
    }), new NewsModel() { News = "Eisenhower's visit to China!" });
    minuteRingQueue.Add(120, new Action<NewsModel>((NewsModel newsObj) =>
    {
        Console.WriteLine(newsObj.News);
    }), new NewsModel() { News = "Xi Jinping's visit to the US!" });
    //3.Waiting for all tasks to complete is usually not completed. Because there is an infinite loop.
    //F5 Run the program and see the effect.
    Task.WaitAll(lstTasks.ToArray());
    Console.Read();
}

Models,这层就是用来在延迟任务中带入的数据模型类而已了。自己用的时候换成任意自定义类型都可以。

5.运行效果

到此这篇关于如何实现定时推送的具体方案的文章就介绍到这了,希望对大家有所帮助,更多相关C#内容请搜索我们以前的文章或继续浏览下面的相关文章,希望大家以后多多支持我们!

(0)

相关推荐

  • python给微信好友定时推送消息的示例

    如下所示: from __future__ import unicode_literals from threading import Timer from wxpy import * import requests #bot = Bot() #bot = Bot(console_qr=2,cache_path="botoo.pkl")#这里的二维码是用像素的形式打印出来!,如果你在win环境上运行,替换为 bot=Bot() bot = Bot(cache_path=True) de

  • Android顶栏定时推送消息

    在用安卓设备时,经常会应用到弹出推送消息.下面在此把我之前写的推送代码分享给大家,供大家参考,有不同见解的朋友欢迎提出,共同学习进步! 最近搜索看这个的朋友比较多.这个也只是单独的内置推送.时时推送与服务器关联 我们可以用SDK云推送来实现我们所需的需求.相关介绍内容.往下移! 首先XML <!-- 安卓推送服务 --> <service android:name=".MessageService" android:enabled="true" a

  • C#实现一阶卡尔曼滤波算法的示例代码

    //FilterKalman.cs namespace FusionFiltering { public class FilterKalman { private double A = 1; private double B = 0; private double H = 1; private double R; private double Q; private double cov = double.NaN; private double x = double.NaN; public Fil

  • 利用Python将每日一句定时推送至微信的实现方法

    前言 前几天在网上看到一篇文章<教你用微信每天给女票说晚安>,感觉很神奇的样子,随后研究了一下,构思的确是巧妙.好,那就开始动工吧!服务器有了,Python环境有了,IDE打开了...然而...然而...我意识到了一个非常严重的问题...没有女朋友 (T_T)... 微信开发已经活跃了很长时间了,在微信开发中有一个神奇的接口它叫模板消息接口,它可以根据用户的openid从服务端给用户推送自定义的模板消息,正因如此,我们可以利用这个特征在服务器端随时向用户推送消息(前提是该用户关注了该公众号).

  • C# 调用exe传参,并获取打印值的实例

    调用方法: string baseName = System.IO.Directory.GetCurrentDirectory(); // baseName+"/" // string fileName = @"C:\Users\59930\Desktop\20170605\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1\bin\x86\Debug\WindowsFormsApp1.exe"; string fi

  • 如何实现定时推送的具体方案

    详细内容 详细内容大概分为4个部分,1.应用场景 2.遇到问题 3.设计 4.实现 5.运行效果 1.应用场景 需要定时推送数据,且轻量化的实现. 2.遇到问题 如果启动一个定时器去定时轮询 (1)轮询效率比较低 (2)每次扫库,已经被执行过记录,仍然会被扫描(只是不会出现在结果集中),会做重复工作 (3)时效性不够好,如果每小时轮询一次,最差的情况下会有时间误差 如何利用"延时消息",对于每个任务只触发一次,保证效率的同时保证实时性,是今天要讨论的问题. 3.设计 高效延时消息,包含

  • iOS13即将到来,iOS推送DeviceToken适配方案详解

    随着苹果iOS13系统即将发布,个推提前推出DeviceToken适配方案,以确保新版本的兼容与APP推送服务的正常使用.iOS13的一个重要变化是"[deviceToken description]" 会受不同运行环境及系统的影响而发生变化,如果未及时做好适配工作,会导致SDK绑定到错误的DeviceToken,从而影响APN推送.请各位开发者根据当前运行环境做出相应的更新与优化,以保障用户在iOS13系统上有更好的使用体验. 在Xcode11.iOS13运行时"[devi

  • flutter消息推送客户端集成方案详解

    目录 一.背景 二.第三方消息推送——个推 1.简介 2.注册开通 3.自定义消息推送——透传 三.项目集成 1.个推客户端flutter插件 2.Android和IOS配置 1)Android 2) iOS 3.通知栏插件flutter_local_notifications 4.个推消息与通知栏整合 最后 一.背景 公司一个CRM APP项目是用Flutter写的,根据业务要求,需要集成消息推送功能.所谓的消息推送就是系统会根据某些行为自动推送信息,手机的通知栏会接收到信息,点击可以打开ap

  • Java实现给微信群中定时推送消息

    目录 前言 准备工作 注册智能微秘书 代码实现 前言 上一篇,我们介绍了如何通过调用接口的方式,将每日新闻发送到自己的博客中.我们会发现,将新闻以文章的形式发布,并且相关内容按照markdown的形式进行格式调整,有需要的可以点击这里查看:如何将每日新闻添加到自己博客中 今天,我们看看如何将每日新闻推送到自己的社群中. 准备工作 你可以没有自己的博客,但是你需要跑一个springboot的项目(java环境下,别的语言另说,不过需要在服务器上跑) 你已经申请了天行数据的[每日新闻简报]接口 你项

  • iPhone/iPad开发通过LocalNotification实现iOS定时本地推送功能

    通过iOS的UILocalNotification Class可以实现本地app的定时推送功能,即使当前app是后台关闭状态. 可以实现诸如,设置app badgenum,弹出一个alert,播放声音等等,实现很简单 UILocalNotification *notification=[[UILocalNotification alloc] init]; if (notification!=nil) { NSDate *now=[NSDate new]; notification.fireDat

  • python微信聊天机器人改进版(定时或触发抓取天气预报、励志语录等,向好友推送)

    最近想着做一个微信机器人,主要想要实现能够每天定时推送天气预报或励志语录,励志语录要每天有自动更新,定时或当有好友回复时,能够随机推送不同的内容.于是开始了分析思路.博主是采用了多线程群发,因为微信对频繁发送消息过快还会出现发送失败的问题,因此还要加入time.sleep(1),当然时间根据自身情况自己定咯.本想把接入写诗机器人,想想自己的渣电脑于是便放弃了,感兴趣的可以尝试一下.做完会有不少收获希望对你有帮助. (1)我们要找个每天定时更新天气预报的网站,和一个更新励志语录的网站.当然如果你想

  • Android实现系统消息推送

    现在好多应用都接入了推送功能,市面上也有很多关于推送的第三方,例如极光等等,那么我们需求不大,接入极光会造成很大的资源浪费,下面我们来看下利用android服务进行本地推送消息, 1.注册一个Service import android.annotation.TargetApi; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; im

随机推荐