123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- using Common.Model;
- using Common.Wechat;
- using CoreEntity.BAL;
- using CoreEntity.DAL;
- using CoreEntity.Entity;
- using CoreEntity.TimeJob;
- using JCSoft.WX.Framework.Api;
- using Pomelo.AspNetCore.TimedJob;
- using Quartz;
- using Quartz.Impl;
- using Quartz.Impl.Matchers;
- using Quartz.Impl.Triggers;
- using Quartz.Spi;
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Threading.Tasks;
- using ZcPeng.PublicLibrary;
- using ZcPeng.weixin.PublicAccount;
- namespace CoreEntity.TimedTask
- {
- public class PaidBackMsgPushJob : IJob
- {
- //private IApiClient _client;
- public PaidBackMsgPushJob()
- {
- // _client = new DefaultApiClient();
- }
- public Task Execute(IJobExecutionContext context)
- {
- JobDataMap dataMap = context.JobDetail.JobDataMap;
- string LoginAccountId = dataMap.GetString("LoginAccountId");
- string RoleId = dataMap.GetString("RoleId");
- string Filter = dataMap.GetString("Filter");
- bool success = PushBAL.PushPaidBackMsg(LoginAccountId,RoleId, Filter,out var result);
-
- return Console.Out.WriteLineAsync("Greetings from PaidBackMsgPushJob!:" + result);
- }
- }
- public class PaidBackMsgProgram
- {
- public static async Task RunProgram(string second , string minute, string hour,
- string RoleId,string LoginAccountId,string Filter, IApiClient _client)
- {
- try
- {
- // Grab the Scheduler instance from the Factory
- NameValueCollection props = new NameValueCollection
- {
- { "quartz.serializer.type", "binary" }
- };
- StdSchedulerFactory factory = new StdSchedulerFactory(props);
- IScheduler scheduler = await factory.GetScheduler();
- // 启动任务调度器
- await scheduler.Start();
- // 定义一个 Job
- string key = "job_" + "PaidBackMsg";
- IJobDetail job = JobBuilder.Create<PaidBackMsgPushJob>()
- .WithIdentity(key, "adminPushGroup")
- .UsingJobData("LoginAccountId", LoginAccountId)
- .UsingJobData("RoleId", RoleId)
- .UsingJobData("FilterLimit", Filter)
- .Build();
- //ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
- // .WithIdentity("trigger1") // 给任务一个名字
- // .StartAt(DateTime.Now) // 设置任务开始时间
- // .ForJob("job1", "group1") //给任务指定一个分组
- // .WithSimpleSchedule(x => x
- // .WithIntervalInSeconds(30) //循环的时间 1秒1次
- // .RepeatForever())
- // .Build();
- //每天9:30:00执行
- CronTriggerImpl trigger = (CronTriggerImpl)TriggerBuilder.Create()
- .WithIdentity("trigger"+ key) // 给任务一个名字
- .StartAt(DateTime.Now) // 设置任务开始时间
- .ForJob(key, "adminPushGroup") //给任务指定一个分组 :管理员,每一次推送一个key
- .WithCronSchedule(second + " "+ minute + " " +hour+ " * * ?")
- .Build();
- GroupMatcher<JobKey> groupMatcher = GroupMatcher<JobKey>.GroupEquals("adminPushGroup");
- IReadOnlyCollection<JobKey> set = await scheduler.GetJobKeys(groupMatcher);
- if (set.Count > 0)
- {
- foreach (JobKey jobKey in set)
- {
- if(jobKey.Name == key) {
- bool delsuccess = await scheduler.DeleteJob(jobKey);
- }
- }
- }
-
- // 等待执行任务
- await scheduler.ScheduleJob(job, trigger);
- // some sleep to show what's happening
- //await Task.Delay(TimeSpan.FromMilliseconds(2000));
- }
- catch (SchedulerException se)
- {
- await Console.Error.WriteLineAsync(se.ToString());
- }
- }
- }
-
- }
|