Example usage for org.springframework.integration.support.leader LockRegistryLeaderInitiator LockRegistryLeaderInitiator

List of usage examples for org.springframework.integration.support.leader LockRegistryLeaderInitiator LockRegistryLeaderInitiator

Introduction

In this page you can find the example usage for org.springframework.integration.support.leader LockRegistryLeaderInitiator LockRegistryLeaderInitiator.

Prototype

public LockRegistryLeaderInitiator(LockRegistry locks, Candidate candidate) 

Source Link

Document

Create a new leader initiator.

Usage

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());
    }/*from  ww  w.ja va2s .  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);
        }
    }
}