Search code examples
javaspringdistributed-computinghazelcasthazelcast-imap

Hazelcast ScheduledExecutorService


Using ScheduledExecutorService from hazelcast 3.8 i've got an out of memory exception. The thing is i'm making a service which should remind to user about some event during a day. Execution of task should be fault-tolerant. A user sets new reminder which is a task for ScheduledExecutorService, thus amount of tasks can be several thousands per day. Each task adds to scheduler as one-shot action using schedule(Runnable command, long delay, TimeUnit unit). A delay can't be longer then 24 hours. When i've started to make tests and add tasks in loop, i've got an oom exception. I thought a task after execution will be removed from memory, but probably i was wrong.

Can you answer for several question:

  1. Should a task be removed from memory after execution?
  2. Is there any setting to do that?
  3. Can i use ScheduledExecutorService for a lot of one-shot action tasks?
  4. Is there another way to solve my task with hazelcast bit don't using a ScheduledExecutorService

Solution

    1. Yes
    2. future.dispose();
    3. Yes
    4. Yes you could have the scheduled executor service run at a certain frequency and it could check that users preferences if the service execution is the same for all the iterations.

            HazelcastInstance instance = Hazelcast.newHazelcastInstance();
    
            IScheduledExecutorService scheduler = instance.getScheduledExecutorService("scheduler");
            IScheduledFuture future = scheduler.schedule(named("MyTask",
                    new EchoTask("foobar")), 1, TimeUnit.SECONDS);
    
            Object result = future.get();
            System.out.println(future.getHandler().getTaskName() + " result: " + result);
    
            future.dispose();
    
            System.out.println("Press any key to exit");
            System.in.read();
            Hazelcast.shutdownAll();