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:
@@ -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[]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user