views:

89

answers:

2

I'm developing a poker game. On the betting stage I create a scheduled job using Red5 iSchedulingService that will run every 8 seconds to forward to the next player to place a bet. Now if the user placed a bet before the 8 seconds are over, I want to manually force the next scheduled job to start.

Is there a way to force the scheduled job to start immediately when required?

+1  A: 

You can do this with Executors. There are cleaner implementations, but this is a stab and something basic that does what you want using Future and Callable.

// wherever you set up the betting stage
ScheduledExecutorService bettingExecutor = 
    Executors.newSingleThreadScheduledExecutor();

ScheduledFuture<?> future = bettingExecutor.schedule(new BettingStage(), 8,
    TimeUnit.SECONDS);

//...

// in the same class (or elsewhere as a default/protected/public class)
private class BettingStage implements Callable<ScheduledFuture<?>> () {
    public ScheduledFuture<?> call() thows ExecutionException {
        ScheduledFuture<?> future = bettingExecutor.schedule(new BettingStage(), 8, 
            TimeUnit.SECONDS);
        // betting code here
        boolean canceled = future.cancel(false); // cancels the task if not running yet
        if(canceled) {
             // run immediately
             future = bettingExecutor.schedule(new BettingStage(), 
                0, TimeUnit.SECONDS)
        }
        return future;
    }
}
justkt
thanks a lot for the information! going to learn about it.
ufk
how can i fetch the bettingExecutor within the callable class ?
ufk
in your example you created a new schedule and right away canceled it. so.. it will never be executed no?
ufk
@ufk - the `//betting code here` part is where you put the code for betting. The job will get canceled after that, as you requested. Also, bettingExecutor in my example should be a member variable of the containing class or passed into the private class. Either works.
justkt
when a user places a bet, it calls a function that's not in that class at all, all that the job does is to start the timer at the user side, it does not wait for input.
ufk
the client side is connect to a shared object with red5. when the user press a betting command it runs a function in my main application class. only then i need to take the next betting schedule that should start in X seconds and to start it immediately.
ufk
My answer was intended to give you an idea of the use of Executors. There are also some fine tutorials out there. Hopefully if you decide Executors will work you can adapt the code as needed. Did you have another specific question you needed to ask?
justkt
ahh in general i have no idea how to get the future variable outside of the scope of the callable class to the main application, i guess i need to read some more executor tutorials to figure that out.
ufk
+1  A: 

an answer to my specific question that I started with in this thread:

i cannot force to start a scheduled job, but what I can do is remove the scheduled job and start a new job with a delay of 0 seconds.

addScheduledJobAfterDelay() return a string that represents the job id. i can use it to remove the scheduled job. The problem is that there is no way to know if I'm interrupting the scheduled job or not. Executors do provide that information. that is why Executors are better choice in this specific case then using the red5 scheduling service.

how to remove a scheduled job (red5):

ISchedulingService scheduler = (ISchedulingService) getScope().getContext().getBean(ISchedulingService.BEAN_NAME);
     scheduler.removeScheduledJob("ScheduleJobString");

the string ScheduleJobString should be replaced with the string that you have received from creating the job:

String scheduleJobString = scheduler.addScheduledOnceJob(DelayInSeconds*1000,new MyJob());
ufk