Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. Scheduler.Start();
  2.  
  3. public class Scheduler
  4. {
  5. private readonly static Lazy<Scheduler> _instance = new Lazy<Scheduler>(() => new Scheduler());
  6. private static IScheduler scheduler;
  7.  
  8. public static Scheduler Instance
  9. {
  10. get
  11. {
  12. return _instance.Value;
  13. }
  14. }
  15.  
  16. public static async void Start()
  17. {
  18. scheduler = await StdSchedulerFactory.GetDefaultScheduler();
  19. await scheduler.Start();
  20.  
  21. InicializarMonitoreo();
  22.  
  23. }
  24.  
  25.  
  26. private static void InicializarMonitoreo()
  27. {
  28. EnviarCorreos();
  29. }
  30.  
  31. public static void EnviarCorreos()
  32. {
  33. IJobDetail job = JobBuilder.Create<JobCorreoAdministradores>().WithIdentity("myJob", "myGroup").Build();
  34.  
  35. ITrigger trigger = TriggerBuilder.Create()
  36. .WithSchedule(CronScheduleBuilder.CronSchedule("0 0/1 * 1/1 * ? *"))
  37. .Build();
  38.  
  39. Scheduler.AddJob(job, trigger);
  40. }
  41.  
  42.  
  43. public static void AddJob(IJobDetail job, ITrigger trigger)
  44. {
  45. scheduler.ScheduleJob(job, trigger);
  46. }
  47. }
  48.  
  49. public class JobCorreoAdministradores: IJob
  50. {
  51. public Task Execute(IJobExecutionContext context)
  52. {
  53. List<string> emails = new List<string>();
  54.  
  55. emails.Add("correo1@dominio.com");
  56. emails.Add("correo2@dominio.com");
  57.  
  58.  
  59. string asunto = "correo de prueba";
  60. string mensaje = "Hola este correo es de prueba para envio programado";
  61.  
  62. EmailSender _emailSender = new EmailSender();
  63. _emailSender.SendMultipleRecipientsEmailAsync(emails, asunto, mensaje);
  64.  
  65. return Task.CompletedTask;
  66. }
  67. }
  68.  
  69. public class CorreoObjetosGeograficosAdministradores : IJob
  70. {
  71. public Task Execute(IJobExecutionContext context)
  72. {
  73. List<string> emails = new List<string>();
  74.  
  75. var usuarios = dbcontext.Tabla.where(usuario => usuario.usuariomodificacion != null).select(usuarios => usuarios.email).ToList();
  76.  
  77. foreach (var item in usuarios)
  78. {
  79. emails.Add(item);
  80. }
  81.  
  82. string asunto = "correo de prueba";
  83. string mensaje = "Hola este correo es de prueba para envio programado";
  84.  
  85. EmailSender _emailSender = new EmailSender(UrlAplicaciones.CORREO_HOST, UrlAplicaciones.CORREO_PORT, UrlAplicaciones.CORREO_ENABLE_SSL, UrlAplicaciones.CORREO_USER_NAME, UrlAplicaciones.CORREO_PASSWORD);
  86. _emailSender.SendMultipleRecipientsEmailAsync(emails, asunto, mensaje);
  87.  
  88. return Task.CompletedTask;
  89. }
  90. }
  91.  
  92. public class MyJobFactory : IJobFactory
  93. {
  94. private readonly IServiceProvider _serviceProvider;
  95.  
  96. public MyJobFactory(IServiceProvider serviceProvider)
  97. {
  98. _serviceProvider = serviceProvider;
  99. }
  100.  
  101. public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
  102. {
  103. return (IJob)this._serviceProvider.GetService(bundle.JobDetail.JobType);
  104. }
  105.  
  106. public void ReturnJob(IJob job)
  107. {
  108. var disposable = job as IDisposable;
  109. disposable?.Dispose();
  110. }
  111. }
  112.  
  113. public class Scheduler
  114. {
  115. private readonly static Lazy<Scheduler> _instance = new Lazy<Scheduler>(() => new Scheduler());
  116. private static IScheduler scheduler;
  117.  
  118. public static Scheduler Instance
  119. {
  120. get
  121. {
  122. return _instance.Value;
  123. }
  124. }
  125.  
  126. public static async void Start(IServiceProvider serviceProvider)
  127. {
  128. scheduler = await StdSchedulerFactory.GetDefaultScheduler();
  129. scheduler.JobFactory = new JobFactory(serviceProvider);
  130. await scheduler.Start();
  131.  
  132. InicializarMonitoreo();
  133.  
  134. }
  135.  
  136.  
  137. private static void InicializarMonitoreo()
  138. {
  139. EnviarCorreos();
  140. }
  141.  
  142. public static void EnviarCorreos()
  143. {
  144. IJobDetail job = JobBuilder.Create<JobCorreoAdministradores>().WithIdentity("myJob", "myGroup").Build();
  145.  
  146. ITrigger trigger = TriggerBuilder.Create()
  147. .WithSchedule(CronScheduleBuilder.CronSchedule("0 0/1 * 1/1 * ? *"))
  148. .Build();
  149.  
  150. Scheduler.AddJob(job, trigger);
  151. }
  152.  
  153.  
  154. public static void AddJob(IJobDetail job, ITrigger trigger)
  155. {
  156. scheduler.ScheduleJob(job, trigger);
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement