生命周期组件框架:生命周期描述语言——简单状态机示例代码

    //状态机元数据描述
    @StateMachine
    protected static interface CustomerLifecycleMeta{
        @StateSet
        static interface States {
            @Initial
            @Transition(event = CustomerLifecycleMeta.Events.Activate.class, value = {Active.class})
            static interface Draft{}
            @Transitions({@Transition(event = CustomerLifecycleMeta.Events.Suspend.class, value = Suspended.class), @Transition(event = CustomerLifecycleMeta.Events.Cancel.class, value = Cancelled.class)})
            static interface Active {}
            
            @Transition(event = CustomerLifecycleMeta.Events.Resume.class, value = Active.class)
            static interface Suspended {}
            
            @Final
            static interface Cancelled {}
        }
        @EventSet
        static interface Events {
            static interface Activate {}
            static interface Suspend {}
            static interface Resume {}
            static interface Cancel {}
        }
    }

    

    public abstract static class ReactiveObject {

        @StateIndicator
        private String state = null;

        protected void initialState(String stateName) {
            if ( null == state ) {
                this.state = stateName;
            } else {
                throw new IllegalStateException("Cannot call initialState method after state had been intialized.");
            }
        }

        public String getState() {
            return state;
        }
    }

 

   // 标记生命周期元数据引用的业务对象(反应型对象)

    @LifecycleMeta(CustomerLifecycleMeta.class)
    public static class Customer extends ReactiveObject {

        protected Customer() {
            initialState(Draft.class.getSimpleName());
        }

        @Event
        public void activate() {}

        @Event
        public void suspend() {}

        @Event
        public void resume() {}

        @Event
        public void cancel() {}
    }

 

   // 测试用例

    @Test
    public void test_standalone_object_without_relation_lifecycle() throws VerificationException {
        Customer customer = new Customer();
        customer.activate();
        assertEquals(CustomerLifecycleMeta.States.Active.class.getSimpleName(), customer.getState());
        customer.suspend();
        assertEquals(CustomerLifecycleMeta.States.Suspended.class.getSimpleName(), customer.getState());
        customer.resume();
        assertEquals(CustomerLifecycleMeta.States.Active.class.getSimpleName(), customer.getState());
        customer.cancel();
        assertEquals(CustomerLifecycleMeta.States.Canceled.class.getSimpleName(), customer.getState());
    }

 

正确配置执行环境参数后可以获得下面的引擎执行Log

 //开始执行测试方法

Processing test: test_standalone_object_with_definite_relation(net.madz.lifecycle.engine.EngineCoreFunctionPositiveTests)

//状态机引擎拦截到标记有@Transition的方法执行,开始执行生命周期相关工作
[FINE]: Found Intercept Point: class net.madz.lifecycle.engine.CoreFuntionTestMetadata$Customer.activate( )
[FINE]: Intercepting....instatiating InterceptContext ...

[FINE]: Intercepting....InterceptorController is doing exec ...

[FINE]: Intercepting....instantiating LifecycleInterceptor

[FINE]: intercepting with :net.madz.bcel.intercept.LifecycleInterceptor @preExec

[FINE]: intercepting [net.madz.lifecycle.engine.CoreFuntionTestMetadata$Customer@67bba966]
from state: [Draft]
[FINE]: Step 1. start validating State [Draft]
[FINE]: Step 2. start validating transition: [Activate] on state: [Draft]
[FINE]: Step 3. start validating inbound relation constraint is next state is predictable before method invocation.
[FINE]: Step 4. start callback before state change from : Draft => to : Active

[FINE]: intercepting with: net.madz.bcel.intercept.CallableInterceptor @intercept

[FINE]: intercepting with :net.madz.bcel.intercept.LifecycleInterceptor @postExec

[FINE]: Step 5. start validating inbound relation constraint is next state after method invocation.
[FINE]: Step 6. Set next state to reactiveObject.
[FINE]: Step 6. ReactiveObject is tranisited to state: [Active]
[FINE]: Step 7. Start Callback after state change from : Draft => to : Active
[FINE]: Step 8. Start fire state change event.

[FINE]: intercepting with :net.madz.bcel.intercept.LifecycleInterceptor @cleanup

[FINE]: Intercepting....LifecycleInterceptor is doing cleanup ...

[FINE]: Found Intercept Point: class net.madz.lifecycle.engine.CoreFuntionTestMetadata$InternetServiceOrder.start( )
[FINE]: Intercepting....instatiating InterceptContext ...

[FINE]: Intercepting....InterceptorController is doing exec ...

[FINE]: Intercepting....instantiating LifecycleInterceptor

[FINE]: intercepting with :net.madz.bcel.intercept.LifecycleInterceptor @preExec

[FINE]: intercepting [net.madz.lifecycle.engine.CoreFuntionTestMetadata$InternetServiceOrder@3033e3e0]
from state: [New]
[FINE]: Step 1. start validating State [New]
[FINE]: Step 2. start validating transition: [Start] on state: [New]
[FINE]: Step 3. start validating inbound relation constraint is next state is predictable before method invocation.
[FINE]: Step 4. start callback before state change from : New => to : InService

[FINE]: intercepting with: net.madz.bcel.intercept.CallableInterceptor @intercept

[FINE]: intercepting with :net.madz.bcel.intercept.LifecycleInterceptor @postExec

[FINE]: Step 5. start validating inbound relation constraint is next state after method invocation.
[FINE]: Step 6. Set next state to reactiveObject.
[FINE]: Step 6. ReactiveObject is tranisited to state: [InService]
[FINE]: Step 7. Start Callback after state change from : New => to : InService
[FINE]: Step 8. Start fire state change event.

[FINE]: intercepting with :net.madz.bcel.intercept.LifecycleInterceptor @cleanup

[FINE]: Intercepting....LifecycleInterceptor is doing cleanup ...
Finish test: test_standalone_object_with_definite_relation(net.madz.lifecycle.engine.EngineCoreFunctionPositiveTests)
##################################################################################

 

 前文:生命周期组件框架——关系型状态机服务

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值