public int schedule(int input) throws TimeoutException { Config config = ConfigFactory.load().getConfig("coordinator"); ZKConfig zkConfig = ZKConfigBuilder.getZKConfig(config); ExclusiveExecutor executor = new ExclusiveExecutor(zkConfig); final AtomicInteger r = new AtomicInteger(); executor.execute("/alert/test", () -> { try { Thread.sleep(input); } catch (Exception e){ } r.set(input); }); try { executor.close(); } catch (IOException e) { e.printStackTrace(); } throw new RuntimeException("Acquire greedy scheduler lock failed, please retry later"); } }
public synchronized ScheduleState schedule(ScheduleOption option) throws TimeoutException { ExclusiveExecutor executor = new ExclusiveExecutor(zkConfig); AtomicReference<ScheduleState> reference = new AtomicReference<>(); try {
@Override public void run() { if (Coordinator.isPeriodicallyForceBuildEnable()) { LOG.info("CoordinatorTrigger started ... "); Stopwatch watch = Stopwatch.createStarted(); ZKConfig zkConfig = ZKConfigBuilder.getZKConfig(config); try (ExclusiveExecutor executor = new ExclusiveExecutor(zkConfig)) { executor.execute(Coordinator.GREEDY_SCHEDULER_ZK_PATH, () -> { // schedule IScheduleContext context = new ScheduleContextBuilder(config, client).buildContext(); TopologyMgmtService mgmtService = new TopologyMgmtService(); IPolicyScheduler scheduler = PolicySchedulerFactory.createScheduler(); scheduler.init(context, mgmtService); ScheduleState state = scheduler.schedule(new ScheduleOption()); // use try catch to use AutoCloseable interface to close producer automatically try (ConfigBusProducer producer = new ConfigBusProducer(ZKConfigBuilder.getZKConfig(config))) { Coordinator.postSchedule(client, state, producer); } watch.stop(); LOG.info("CoordinatorTrigger ended, used time {} sm.", watch.elapsed(TimeUnit.MILLISECONDS)); }); } catch (Exception e) { LOG.error("trigger schedule failed!", e); } } else { LOG.info("CoordinatorTrigger found isPeriodicallyForceBuildEnable = false, skipped build"); } }
Runnable runnableOne = () -> { System.out.println("this is thread one"); lock1.set(true);}; new Thread(() -> { ExclusiveExecutor executor = new ExclusiveExecutor(zkConfig); try { executor.execute(path, runnableOne); Runnable runnableTwo = () -> { System.out.println("this is thread two"); lock2.set(true);}; new Thread(() -> { ExclusiveExecutor executor = new ExclusiveExecutor(zkConfig); try { executor.execute(path, runnableTwo);