1

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?

ufk
  • 30,912
  • 70
  • 235
  • 386
  • 2
    This is the first time I heard about iSchedulingService. I doubt if this is well known among fellow javaians. You may have more luck on answers here if you use Quartz or the builtin `java.util.concurrent.Executors` or just `java.util.Timer`. – BalusC May 05 '10 at 14:33

2 Answers2

1

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
  • 14,610
  • 8
  • 42
  • 62
  • how can i fetch the bettingExecutor within the callable class ? – ufk May 11 '10 at 09:41
  • in your example you created a new schedule and right away canceled it. so.. it will never be executed no? – ufk May 11 '10 at 09:52
  • @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 May 11 '10 at 12:07
  • 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 May 11 '10 at 12:35
  • 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 May 11 '10 at 12:37
  • 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 May 11 '10 at 13:15
  • 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 May 11 '10 at 13:30
1

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
  • 30,912
  • 70
  • 235
  • 386