Add config builder for distributed machine

- New configurer which can be used to define an ensemble which
  if is set automatically wraps a machine with a distributed
  state machine and sets it to use an given ensemble.
- Relates to #35
This commit is contained in:
Janne Valkealahti
2015-06-18 13:20:04 +01:00
parent 742010f1a5
commit e2fe4907d3
7 changed files with 225 additions and 45 deletions

View File

@@ -18,65 +18,32 @@ package demo.zookeeper;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.shell.Bootstrap;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.ensemble.DistributedStateMachine;
import org.springframework.statemachine.zookeeper.ZookeeperStateMachineEnsemble;
@Configuration
public class Application {
@Configuration
static class ZkConfig {
@Qualifier("internalStateMachine")
@Autowired
StateMachine<String, String> internalMachine;
@Bean
public StateMachine<String, String> stateMachine() throws Exception {
DistributedStateMachine<String, String> machine =
new DistributedStateMachine<String, String>(ensemble(), internalMachine);
return machine;
}
@Bean
public ZookeeperStateMachineEnsemble<String, String> ensemble() throws Exception {
ZookeeperStateMachineEnsemble<String, String> ensemble =
new ZookeeperStateMachineEnsemble<String, String>(curatorClient(), "/foo");
return ensemble;
}
// for now lets not close it here, we need to let
// some other framework, ie cloud, to create curator
@Bean//(destroyMethod = "close")
public CuratorFramework curatorClient() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.builder().defaultData(new byte[0])
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.connectString("localhost:2181").build();
// for testing we start it here, thought initiator
// is trying to start it if not already done
client.start();
return client;
}
}
//tag::snippetA[]
@Configuration
@EnableStateMachine(name="internalStateMachine")
@EnableStateMachine
static class StateMachineConfig
extends StateMachineConfigurerAdapter<String, String> {
@Override
public void configure(StateMachineConfigurationConfigurer<String, String> config) throws Exception {
config
.withDistributed()
.ensemble(new ZookeeperStateMachineEnsemble<String, String>(curatorClient(), "/foo"));
}
@Override
public void configure(StateMachineStateConfigurer<String, String> states)
throws Exception {
@@ -101,6 +68,18 @@ public class Application {
.event("PUSH");
}
@Bean
public CuratorFramework curatorClient() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.builder().defaultData(new byte[0])
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.connectString("localhost:2181").build();
// for testing we start it here, thought initiator
// is trying to start it if not already done
client.start();
return client;
}
}
//end::snippetA[]