简体   繁体   English

特定时间后停止计划程序

[英]Stop Scheduler after Specific Time

I am trying to create a Scheduler in AKKA. 我正在尝试在AKKA中创建一个Scheduler。 Requirement is :- 要求是: -

  1. It will start every day 5:00AM 它将每天凌晨5:00开始
  2. It will stop at 10:00 PM every day 它将在每天晚上10点停止
  3. It will execute job in every 1 hr (Frequency). 它将每1小时(频率)执行一次工作。

I find the solution for 3rd point (Frequency) but not able to find the solution for 1st and 2nd point. 我找到第3点(频率)的解决方案,但无法找到第1点和第2点的解决方案。

 system.scheduler().schedule(Duration.create(10, TimeUnit.SECONDS), Duration.create(1, TimeUnit.HOURS), actorRef, "Hello", system.dispatcher(), null);
// For 5.00 am time period. 

int InHrs = 17;
int InMinutes = 00;

scheduler = Akka.system().scheduler().schedule(Duration.create(nextExecutionInSeconds(InHrs, InMinutes), TimeUnit.SECONDS),
                Duration.create(24, TimeUnit.HOURS),
                new Runnable() {
                    @Override
                    public void run() {
                        // Call your method
                        System.out.println("EVERY 24:00 Later ---    " + System.currentTimeMillis());   

                    }
                },
                Akka.system().dispatcher());


public static int nextExecutionInSeconds(int hour, int minute){
            return Seconds.secondsBetween(
                    new DateTime(),
                    nextExecution(hour, minute)
            ).getSeconds();
        }

public static DateTime nextExecution(int hour, int minute){
        DateTime next = new DateTime()
                .withHourOfDay(hour)
                .withMinuteOfHour(minute)
                .withSecondOfMinute(0)
                .withMillisOfSecond(0);

        return (next.isBeforeNow())
                ? next.plusHours(24)
                : next;
    }

Repeat this for 10 AM time period which will be 22 inHrs. 重复此操作10 AM时间段,这将是22小时。 Hopefully it solves the problem. 希望它能解决问题。

// I am using this for scheduling and repeating, it is working fine
//**I am also searching for to Stop Scheduler after Specific Time**
 public static void setReminder(Context context, Class<?> cls, int hour, int min, int uniqueAlertID)
    {
        Calendar calendar = Calendar.getInstance();
        Calendar setcalendar = Calendar.getInstance();
        setcalendar.set(Calendar.HOUR_OF_DAY, hour);
        setcalendar.set(Calendar.MINUTE, min);
        setcalendar.set(Calendar.SECOND, 0);
        // cancel already scheduled reminders
        cancelReminder(context,cls, uniqueAlertID);

        if(setcalendar.before(calendar))
            setcalendar.add(Calendar.DATE,1);

        // Enable a receiver
        ComponentName receiver = new ComponentName(context, cls);
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

        Intent intent1 = new Intent(context, cls);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, uniqueAlertID, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        am.setInexactRepeating(AlarmManager.RTC_WAKEUP, setcalendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);
    }

    public static void cancelReminder(Context context,Class<?> cls, int uniqueAlertID)
    {
        // Disable a receiver
        ComponentName receiver = new ComponentName(context, cls);
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);

        Intent intent1 = new Intent(context, cls);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                uniqueAlertID, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        am.cancel(pendingIntent);
        pendingIntent.cancel();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM