ABP Quartz 作业调度第三篇

1、第一步安装Abp.Quartz ,把他安装到核心层

核心模块添加对quarz的依赖,

领域层创建firstjob类

    public class FirstJob : JobBase, ITransientDependency
    {
        public override Task Execute(IJobExecutionContext context)
        {
            Task t = Task.Run(delegate { Console.WriteLine(DateTime.Now); });
            return t;
     
        }
    }

 创建领域层作业服务类

 public class JobService
    {
        private readonly IQuartzScheduleJobManager _jobManager;

        public JobService(IQuartzScheduleJobManager i)
        {
            _jobManager = i;
        }
        public async Task ScheduleJob()
        {


            await _jobManager.ScheduleAsync<FirstJob>(
          job =>
          {
              job.WithIdentity("MyLogJobIdentity", "MyGroup")
                  .WithDescription("A job to simply write logs.");
          },
          trigger =>
          {
              trigger.StartNow()
                  .WithSimpleSchedule(schedule =>
                  {
                      schedule.RepeatForever()
                          .WithIntervalInSeconds(5)
                          .Build();
                  });
          });
    
           // return ;
                 
        }
    }

  

应用层创建接口

   public  class JobManager : IApplicationService
    {
        IQuartzScheduleJobManager m;

        public JobManager(IQuartzScheduleJobManager d)
        {
            m = d;
        }
        public string GetString() {
            return "成功";
        }


        public async Task GetStartJob()
        {
            Jobs.JobService j = new Jobs.JobService(m);
            await j.ScheduleJob();
        }
    }

  

 ABP默认接口访问地址,调用接口:

http://localhost:12021/api/services/app/JobManager/GetStartJob  使用Get方式访问

 

最后测试的结果是:

每隔5秒就读取当前时间并且显示出来

 

posted on 2018-09-10 23:14  topguntopgun  阅读(671)  评论(0编辑  收藏  举报

导航