org.springframework.integration.leader.event.OnRevokedEvent Java Examples
The following examples show how to use
org.springframework.integration.leader.event.OnRevokedEvent.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocalLeaderTest.java From genie with Apache License 2.0 | 6 votes |
/** * Ensure behavior in case of start, stop in case the module is not configured to be leader. */ @Test void startAndStopIfNotLeader() { this.localLeader = new LocalLeader(this.genieEventBus, false); Assertions.assertThat(this.localLeader.isRunning()).isFalse(); Assertions.assertThat(this.localLeader.isLeader()).isFalse(); Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class)); Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class)); // Start this.localLeader.start(); Assertions.assertThat(this.localLeader.isRunning()).isTrue(); Assertions.assertThat(this.localLeader.isLeader()).isFalse(); Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class)); Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class)); // Stop this.localLeader.stopLeadership(this.contextClosedEvent); Assertions.assertThat(this.localLeader.isRunning()).isFalse(); Assertions.assertThat(this.localLeader.isLeader()).isFalse(); Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class)); Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class)); }
Example #2
Source File: LeaderAspect.java From hedera-mirror-node with Apache License 2.0 | 5 votes |
@EventListener public void revoked(OnRevokedEvent event) { if (leader) { leader = false; log.info("Transitioned to follower"); } }
Example #3
Source File: LocalLeader.java From genie with Apache License 2.0 | 5 votes |
/** * If configured to be leader and previously started, deactivate and send a leadership lost notification. * NOOP if not running or if this node is not configured to be leader. */ public void stop() { if (this.isRunning.compareAndSet(true, false) && this.isLeader) { log.info("Stopping Leadership"); this.genieEventBus.publishSynchronousEvent(new OnRevokedEvent(this, null, "leader")); } }
Example #4
Source File: LocalLeaderTest.java From genie with Apache License 2.0 | 5 votes |
/** * Ensure behavior in case of start, stop, start when already running and stop when not running in case the module * is configured to be leader. */ @Test void startAndStopIfLeader() { this.localLeader = new LocalLeader(this.genieEventBus, true); Assertions.assertThat(this.localLeader.isRunning()).isFalse(); Assertions.assertThat(this.localLeader.isLeader()).isFalse(); Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class)); Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class)); // Start with application context event this.localLeader.startLeadership(this.contextRefreshedEvent); Assertions.assertThat(this.localLeader.isRunning()).isTrue(); Assertions.assertThat(this.localLeader.isLeader()).isTrue(); Mockito.verify(this.genieEventBus, Mockito.times(1)).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class)); Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class)); // Start again this.localLeader.start(); Assertions.assertThat(this.localLeader.isRunning()).isTrue(); Assertions.assertThat(this.localLeader.isLeader()).isTrue(); Mockito.verify(this.genieEventBus, Mockito.times(1)).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class)); Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class)); // Stop with application context event this.localLeader.stopLeadership(this.contextClosedEvent); Assertions.assertThat(this.localLeader.isRunning()).isFalse(); Assertions.assertThat(this.localLeader.isLeader()).isFalse(); Mockito.verify(this.genieEventBus, Mockito.times(1)).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class)); Mockito.verify(this.genieEventBus, Mockito.times(1)).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class)); // Stop again this.localLeader.stopLeadership(this.contextClosedEvent); Assertions.assertThat(this.localLeader.isRunning()).isFalse(); Assertions.assertThat(this.localLeader.isLeader()).isFalse(); Mockito.verify(this.genieEventBus, Mockito.times(1)).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class)); Mockito.verify(this.genieEventBus, Mockito.times(1)).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class)); }
Example #5
Source File: LeaderController.java From spring-cloud-kubernetes with Apache License 2.0 | 4 votes |
/** * Handle a notification that this instance's leadership has been revoked. * @param event on revoked event */ @EventListener public void handleEvent(OnRevokedEvent event) { System.out.println(String.format("'%s' leadership revoked", event.getRole())); this.context = null; }
Example #6
Source File: LeaderTasksCoordinator.java From genie with Apache License 2.0 | 4 votes |
/** * Leadership event listener. Starts and stop processes when this Genie node is elected the leader of the cluster. * <p> * Synchronized to ensure no race conditions between threads trying to start and stop leadership tasks. * * @param leaderEvent The leader grant or revoke event * @see org.springframework.integration.leader.event.OnGrantedEvent * @see org.springframework.integration.leader.event.OnRevokedEvent */ @EventListener public synchronized void onLeaderEvent(final AbstractLeaderEvent leaderEvent) { if (leaderEvent instanceof OnGrantedEvent) { if (this.isRunning) { return; } log.info("Leadership granted."); this.isRunning = true; this.tasks.forEach( task -> { switch (task.getScheduleType()) { case TRIGGER: final Trigger trigger = task.getTrigger(); log.info( "Scheduling leadership task {} to run with trigger {}", task.getClass().getCanonicalName(), trigger ); this.futures.add(this.taskScheduler.schedule(task, trigger)); break; case FIXED_RATE: final long rate = task.getFixedRate(); log.info( "Scheduling leadership task {} to run every {} second(s)", task.getClass().getCanonicalName(), rate / 1000.0 ); this.futures.add(this.taskScheduler.scheduleAtFixedRate(task, rate)); break; case FIXED_DELAY: final long delay = task.getFixedDelay(); log.info( "Scheduling leadership task {} to run at a fixed delay of every {} second(s)", task.getClass().getCanonicalName(), delay / 1000.0 ); this.futures.add(this.taskScheduler.scheduleWithFixedDelay(task, delay)); break; default: log.error("Unknown Genie task type {}", task.getScheduleType()); } } ); } else if (leaderEvent instanceof OnRevokedEvent) { if (!this.isRunning) { return; } log.info("Leadership revoked."); this.isRunning = false; this.cancelTasks(); } else { log.warn("Unknown leadership event {}. Ignoring.", leaderEvent); } }
Example #7
Source File: LeaderTasksCoordinatorTest.java From genie with Apache License 2.0 | 4 votes |
/** * Make sure all leadership activities are stopped when leadership is revoked. */ @Test @SuppressWarnings("unchecked") 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); }