Search in sources :

Example 1 with DefaultLeaderEventPublisher

use of org.springframework.integration.leader.event.DefaultLeaderEventPublisher in project spring-integration by spring-projects.

the class LockRegistryLeaderInitiatorTests method testExceptionFromEvent.

@Test
public void testExceptionFromEvent() throws Exception {
    CountDownLatch onGranted = new CountDownLatch(1);
    this.initiator.setLeaderEventPublisher(new DefaultLeaderEventPublisher() {

        @Override
        public void publishOnGranted(Object source, Context context, String role) {
            try {
                throw new RuntimeException("intentional");
            } finally {
                onGranted.countDown();
            }
        }
    });
    this.initiator.start();
    assertTrue(onGranted.await(10, TimeUnit.SECONDS));
    assertTrue(initiator.getContext().isLeader());
    this.initiator.stop();
}
Also used : Context(org.springframework.integration.leader.Context) DefaultLeaderEventPublisher(org.springframework.integration.leader.event.DefaultLeaderEventPublisher) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with DefaultLeaderEventPublisher

use of org.springframework.integration.leader.event.DefaultLeaderEventPublisher in project spring-integration by spring-projects.

the class LeaderInitiatorFactoryBean method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    if (this.leaderInitiator == null) {
        this.leaderInitiator = new LeaderInitiator(this.client, this.candidate, this.path);
        this.leaderInitiator.setPhase(this.phase);
        this.leaderInitiator.setAutoStartup(this.autoStartup);
        if (this.leaderEventPublisher != null) {
            this.leaderInitiator.setLeaderEventPublisher(this.leaderEventPublisher);
        } else if (this.applicationEventPublisher != null) {
            this.leaderInitiator.setLeaderEventPublisher(new DefaultLeaderEventPublisher(this.applicationEventPublisher));
        }
    }
}
Also used : LeaderInitiator(org.springframework.integration.zookeeper.leader.LeaderInitiator) DefaultLeaderEventPublisher(org.springframework.integration.leader.event.DefaultLeaderEventPublisher)

Example 3 with DefaultLeaderEventPublisher

use of org.springframework.integration.leader.event.DefaultLeaderEventPublisher in project spring-integration by spring-projects.

the class LockRegistryLeaderInitiator method start.

/**
 * Start the registration of the {@link #candidate} for leader election.
 */
@Override
public void start() {
    if (this.leaderEventPublisher == null && this.applicationEventPublisher != null) {
        this.leaderEventPublisher = new DefaultLeaderEventPublisher(this.applicationEventPublisher);
    }
    synchronized (this.lifecycleMonitor) {
        if (!this.running) {
            this.leaderSelector = new LeaderSelector(buildLeaderPath());
            this.running = true;
            this.future = this.executorService.submit(this.leaderSelector);
            logger.debug("Started LeaderInitiator");
        }
    }
}
Also used : DefaultLeaderEventPublisher(org.springframework.integration.leader.event.DefaultLeaderEventPublisher)

Example 4 with DefaultLeaderEventPublisher

use of org.springframework.integration.leader.event.DefaultLeaderEventPublisher in project spring-integration by spring-projects.

the class LeaderInitiatorFactoryBeanTests method testExceptionFromEvent.

@Test
public void testExceptionFromEvent() throws Exception {
    CountDownLatch onGranted = new CountDownLatch(1);
    LeaderInitiator initiator = new LeaderInitiator(client, new DefaultCandidate());
    initiator.setLeaderEventPublisher(new DefaultLeaderEventPublisher() {

        @Override
        public void publishOnGranted(Object source, Context context, String role) {
            try {
                throw new RuntimeException("intentional");
            } finally {
                onGranted.countDown();
            }
        }
    });
    initiator.start();
    assertTrue(onGranted.await(10, TimeUnit.SECONDS));
    assertTrue(initiator.getContext().isLeader());
    initiator.stop();
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) Context(org.springframework.integration.leader.Context) LeaderInitiator(org.springframework.integration.zookeeper.leader.LeaderInitiator) DefaultLeaderEventPublisher(org.springframework.integration.leader.event.DefaultLeaderEventPublisher) DefaultCandidate(org.springframework.integration.leader.DefaultCandidate) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

DefaultLeaderEventPublisher (org.springframework.integration.leader.event.DefaultLeaderEventPublisher)4 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 Context (org.springframework.integration.leader.Context)2 LeaderInitiator (org.springframework.integration.zookeeper.leader.LeaderInitiator)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 DefaultCandidate (org.springframework.integration.leader.DefaultCandidate)1 DirtiesContext (org.springframework.test.annotation.DirtiesContext)1