Example usage for org.springframework.integration.leader.event OnRevokedEvent OnRevokedEvent

List of usage examples for org.springframework.integration.leader.event OnRevokedEvent OnRevokedEvent

Introduction

In this page you can find the example usage for org.springframework.integration.leader.event OnRevokedEvent OnRevokedEvent.

Prototype

public OnRevokedEvent(Object source, Context context, String role) 

Source Link

Document

Instantiates a new revoked event.

Usage

From source file:com.netflix.genie.web.tasks.leader.LocalLeader.java

/**
 * Before the application shuts down need to turn off leadership activities.
 *
 * @param event The application context closing event
 *//*from  w w w  .  ja v a  2  s .  com*/
@EventListener
public void stopLeadership(final ContextClosedEvent event) {
    if (this.isLeader) {
        log.debug("Stopping Leadership due to {}", event);
        this.genieEventBus.publishSynchronousEvent(new OnRevokedEvent(this, null, "leader"));
    }
}

From source file:com.netflix.genie.web.tasks.leader.LeadershipTasksCoordinatorUnitTests.java

/**
 * Make sure all leadership activities are stopped when leadership is revoked.
 *///  w w w  . ja va  2s  .  c om
@Test
@SuppressWarnings("unchecked")
public void canStopLeadershipTasks() {
    final long task1Period = 13238;
    Mockito.when(this.task1.getScheduleType()).thenReturn(GenieTaskScheduleType.FIXED_RATE);
    Mockito.when(this.task1.getFixedRate()).thenReturn(task1Period);
    final long task2Period = 3891082;
    Mockito.when(this.task2.getScheduleType()).thenReturn(GenieTaskScheduleType.FIXED_DELAY);
    Mockito.when(this.task2.getFixedDelay()).thenReturn(task2Period);
    final Trigger task3Trigger = Mockito.mock(Trigger.class);
    Mockito.when(this.task3.getScheduleType()).thenReturn(GenieTaskScheduleType.TRIGGER);
    Mockito.when(this.task3.getTrigger()).thenReturn(task3Trigger);

    final ScheduledFuture future1 = Mockito.mock(ScheduledFuture.class);
    Mockito.when(future1.cancel(true)).thenReturn(true);
    Mockito.when(this.scheduler.scheduleAtFixedRate(this.task1, task1Period)).thenReturn(future1);

    final ScheduledFuture future2 = Mockito.mock(ScheduledFuture.class);
    Mockito.when(future2.cancel(true)).thenReturn(true);
    Mockito.when(this.scheduler.scheduleWithFixedDelay(this.task2, task2Period)).thenReturn(future2);

    final ScheduledFuture future3 = Mockito.mock(ScheduledFuture.class);
    Mockito.when(future3.cancel(true)).thenReturn(false);
    Mockito.when(this.scheduler.schedule(this.task3, task3Trigger)).thenReturn(future3);

    final OnGrantedEvent grantedEvent = new OnGrantedEvent(this, null, "blah");

    this.coordinator.onLeaderEvent(grantedEvent);

    Mockito.verify(this.scheduler, Mockito.times(1)).scheduleAtFixedRate(this.task1, task1Period);
    Mockito.verify(this.task1, Mockito.never()).getFixedDelay();
    Mockito.verify(this.task1, Mockito.never()).getTrigger();
    Mockito.verify(this.scheduler, Mockito.times(1)).scheduleWithFixedDelay(this.task2, task2Period);
    Mockito.verify(this.task2, Mockito.never()).getFixedRate();
    Mockito.verify(this.task2, Mockito.never()).getTrigger();
    Mockito.verify(this.scheduler, Mockito.times(1)).schedule(this.task3, task3Trigger);
    Mockito.verify(this.task3, Mockito.never()).getFixedRate();
    Mockito.verify(this.task3, Mockito.never()).getFixedDelay();

    // Should now be running

    final OnRevokedEvent revokedEvent = new OnRevokedEvent(this, null, "blah");

    this.coordinator.onLeaderEvent(revokedEvent);

    Mockito.verify(future1, Mockito.times(1)).cancel(true);
    Mockito.verify(future2, Mockito.times(1)).cancel(true);
    Mockito.verify(future3, Mockito.times(1)).cancel(true);

    // Call again to make sure nothing is invoked even though they were cancelled
    this.coordinator.onLeaderEvent(revokedEvent);

    Mockito.verify(future1, Mockito.times(1)).cancel(true);
    Mockito.verify(future2, Mockito.times(1)).cancel(true);
    Mockito.verify(future3, Mockito.times(1)).cancel(true);
}