Search in sources :

Example 1 with LeaderInitiator

use of org.springframework.integration.zookeeper.leader.LeaderInitiator 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 2 with LeaderInitiator

use of org.springframework.integration.zookeeper.leader.LeaderInitiator in project spring-integration by spring-projects.

the class ZookeeperLeaderTests method testLeader.

@Test
public void testLeader() throws Exception {
    assertFalse(this.adapter.isRunning());
    LeaderEventPublisher publisher = publisher();
    DefaultCandidate candidate1 = new DefaultCandidate("foo", "sitest");
    LeaderInitiator initiator1 = new LeaderInitiator(this.client, candidate1, "/sitest");
    initiator1.setLeaderEventPublisher(publisher);
    initiator1.start();
    DefaultCandidate candidate2 = new DefaultCandidate("bar", "sitest");
    LeaderInitiator initiator2 = new LeaderInitiator(this.client, candidate2, "/sitest");
    initiator2.setLeaderEventPublisher(publisher);
    initiator2.start();
    AbstractLeaderEvent event = this.events.poll(30, TimeUnit.SECONDS);
    assertNotNull(event);
    assertThat(event, instanceOf(OnGrantedEvent.class));
    assertTrue(this.adapter.isRunning());
    event.getContext().yield();
    event = this.events.poll(30, TimeUnit.SECONDS);
    assertNotNull(event);
    assertThat(event, instanceOf(OnRevokedEvent.class));
    assertFalse(this.adapter.isRunning());
    this.yieldBarrier.countDown();
    event = this.events.poll(30, TimeUnit.SECONDS);
    assertNotNull(event);
    assertThat(event, instanceOf(OnGrantedEvent.class));
    assertTrue(this.adapter.isRunning());
    initiator1.stop();
    initiator2.stop();
    event = this.events.poll(30, TimeUnit.SECONDS);
    assertNotNull(event);
    assertThat(event, instanceOf(OnRevokedEvent.class));
    assertFalse(this.adapter.isRunning());
}
Also used : AbstractLeaderEvent(org.springframework.integration.leader.event.AbstractLeaderEvent) LeaderInitiator(org.springframework.integration.zookeeper.leader.LeaderInitiator) OnGrantedEvent(org.springframework.integration.leader.event.OnGrantedEvent) LeaderEventPublisher(org.springframework.integration.leader.event.LeaderEventPublisher) DefaultLeaderEventPublisher(org.springframework.integration.leader.event.DefaultLeaderEventPublisher) DefaultCandidate(org.springframework.integration.leader.DefaultCandidate) OnRevokedEvent(org.springframework.integration.leader.event.OnRevokedEvent) Test(org.junit.Test)

Example 3 with LeaderInitiator

use of org.springframework.integration.zookeeper.leader.LeaderInitiator 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)3 LeaderInitiator (org.springframework.integration.zookeeper.leader.LeaderInitiator)3 Test (org.junit.Test)2 DefaultCandidate (org.springframework.integration.leader.DefaultCandidate)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 Context (org.springframework.integration.leader.Context)1 AbstractLeaderEvent (org.springframework.integration.leader.event.AbstractLeaderEvent)1 LeaderEventPublisher (org.springframework.integration.leader.event.LeaderEventPublisher)1 OnGrantedEvent (org.springframework.integration.leader.event.OnGrantedEvent)1 OnRevokedEvent (org.springframework.integration.leader.event.OnRevokedEvent)1 DirtiesContext (org.springframework.test.annotation.DirtiesContext)1