List of usage examples for org.springframework.integration.leader DefaultCandidate DefaultCandidate
public DefaultCandidate(String id, String role)
From source file:org.springframework.cloud.task.configuration.SingleInstanceTaskListener.java
@BeforeTask public void lockTask(TaskExecution taskExecution) { if (this.lockRegistry == null) { this.lockRegistry = getDefaultLockRegistry(taskExecution.getExecutionId()); }/* w w w. j a v a 2s. c o m*/ this.lockRegistryLeaderInitiator = new LockRegistryLeaderInitiator(this.lockRegistry, new DefaultCandidate( String.valueOf(taskExecution.getExecutionId()), taskNameResolver.getTaskName())); this.lockRegistryLeaderInitiator.setApplicationEventPublisher(this.applicationEventPublisher); this.lockRegistryLeaderInitiator.setPublishFailedEvents(true); this.lockRegistryLeaderInitiator.start(); while (!this.lockReady) { try { Thread.sleep(this.taskProperties.getSingleInstanceLockCheckInterval()); } catch (InterruptedException ex) { logger.warn("Thread Sleep Failed", ex); } if (this.lockFailed) { String errorMessage = String.format("Task with name \"%s\" is already running.", this.taskNameResolver.getTaskName()); try { this.lockRegistryLeaderInitiator.destroy(); } catch (Exception exception) { throw new TaskExecutionException("Failed to destroy lock.", exception); } throw new TaskExecutionException(errorMessage); } } }