私はポーカーゲームを開発しています。賭けの段階で、Red5 iSchedulingServiceを使用してスケジュールされたジョブを作成します。このジョブは、8秒ごとに実行され、次のプレーヤーに転送して賭けを行います。ここで、ユーザーが8秒が経過する前に賭けをした場合、次にスケジュールされたジョブを手動で強制的に開始したいと思います。
スケジュールされたジョブを必要なときにすぐに開始するように強制する方法はありますか?
私はポーカーゲームを開発しています。賭けの段階で、Red5 iSchedulingServiceを使用してスケジュールされたジョブを作成します。このジョブは、8秒ごとに実行され、次のプレーヤーに転送して賭けを行います。ここで、ユーザーが8秒が経過する前に賭けをした場合、次にスケジュールされたジョブを手動で強制的に開始したいと思います。
スケジュールされたジョブを必要なときにすぐに開始するように強制する方法はありますか?
このスレッドで始めた私の特定の質問への回答:
スケジュールされたジョブを強制的に開始することはできませんが、スケジュールされたジョブを削除して、0秒の遅延で新しいジョブを開始することができます。
addScheduledJobAfterDelay()は、ジョブIDを表す文字列を返します。スケジュールされたジョブを削除するために使用できます。問題は、スケジュールされたジョブを中断しているかどうかを知る方法がないことです。エグゼキュータはその情報を提供します。そのため、この特定のケースでは、red5スケジューリングサービスを使用するよりもエグゼキュータを選択する方が適切です。
スケジュールされたジョブを削除する方法(red5):
ISchedulingService scheduler = (ISchedulingService) getScope().getContext().getBean(ISchedulingService.BEAN_NAME);
scheduler.removeScheduledJob("ScheduleJobString");
文字列ScheduleJobString
は、ジョブの作成から受け取った文字列に置き換える必要があります。
String scheduleJobString = scheduler.addScheduledOnceJob(DelayInSeconds*1000,new MyJob());
これは、 Executorsを使用して行うことができます。よりクリーンな実装がありますが、これは、 Futureと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;
}
}