Triggering a Java program based on database updates and time interval

后端 未结 2 1212

I want a mechanism that will start a java program ( quite a big one ) depending on 2 conditions:

  1. N new inserts in a MySQL table
  2. Every 5 minutes interv
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-21 21:13

    It is actually not that big using a ScheduledExecutorService:

    private static final Runnable PROGRAM_RUNNABLE =  new Runnable() {
        @Override
        public void run() {
            // run the program
        }
    }
    
    private ScheduledExecutorService ses = Executors.newScheduledThreadPool(2);
    
    public static void main(String[] args) {
        // database based
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                boolean inserted = checkDatabase(); // check the insert in the db
                if(inserted) {
                    PROGRAM_RUNNABLE.run();
                }
            }
        }, 0, 1, TimeUnit.MINUTES);
    
        // time based
        ses.scheduleAtFixedRate(PROGRAM_RUNNABLE, 5, 5, TimeUnit.MINUTES);
    }
    

提交回复
热议问题