MassTransit Saga example using Automatonynous library

1,139 views
Skip to first unread message

Mark Gillen

unread,
May 22, 2014, 11:13:22 AM5/22/14
to masstrans...@googlegroups.com
Hi all,

Look a quick question:  I need a concrete example showing how to setup a saga map for NHibernate and bus confiuration when one uses the Automatonymous library as the state machine for the saga.

I've just about got it but there's a couple of bits that I can't seem to find any documentation on.  For instance do I map the saga or the saga instance when mapping to NHibernate..I'm thinking the instance but I want to make sure.

Regards,

Mark Gillen

Chris Patterson

unread,
May 22, 2014, 12:02:02 PM5/22/14
to masstrans...@googlegroups.com


--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/60463401-1539-4fdc-8eac-791e5502270f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Gillen

unread,
May 28, 2014, 8:43:09 AM5/28/14
to masstrans...@googlegroups.com
Chris,

One follow up question.  I'm looking to use the Windsor DI component to configure my MassTransit service.  However, looking at the code (the link to which you provided below) to integrate the Automatonymous library into the mix I'm wondering if the WindsorSagaFactoryConfigurator method in the WindsorContainerExtensions.LoadFrom extension method is going to work with the Automatonymous sagas.

Can you tell me if you're using a DI component with your Automatonymous state machine library with and if so which one?  Is there any code that you can share that illustrates how your DI component is configured when Automatonymous is part of the mix? 

The reason I bring this up is that the configuration for Automatonymous state machines is significantly different when configuring the bus (and the "containerized" components) so I wanted to get some guidance on the use of DI and this particular library.  If it can't be done that's fine too, but I'd prefer a consistent architecture across all my services and clients so a well structured DI approach is my preferred method of doing things.

Regards,

Mark Gillen


--
You received this message because you are subscribed to a topic in the Google Groups "masstransit-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/masstransit-discuss/9rXDa8T1Big/unsubscribe.
To unsubscribe from this group and all its topics, send an email to masstransit-dis...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.

Mark Gillen

unread,
May 28, 2014, 11:12:08 AM5/28/14
to masstrans...@googlegroups.com
One other question, Chris:  Is there any code samples, other than the unit tests, that demonstrate setting up the Automatonymous based sagas to the database.  The test case is fine but there are a number of "test" extensions that kind of obscure the actual bus configuration pieces.  Eh, it's a pain to wade through to get at all the bits I need.

Thanks, Chris.

Chris Patterson

unread,
May 28, 2014, 5:11:54 PM5/28/14
to masstrans...@googlegroups.com
The Riktig sample is your best guide for how to setup and persist Automatonymous state using NHibernate.



Mark Gillen

unread,
May 29, 2014, 12:48:02 PM5/29/14
to masstrans...@googlegroups.com
Chris,

Okay, I'm looking at the Riktig example, and I'm curious.  I don't see anywhere where the AutomatonymousStateMachineSagaRepository class is being instantiated by AutoFac.  Do I need to use the AutomatonymousStateMachineSagaRepository to persist state or can I just use a straight NHibernate repo?

Regards,

Mark


Mark Gillen

unread,
May 29, 2014, 1:15:34 PM5/29/14
to masstrans...@googlegroups.com
Also Chris,

I did get everything running for the Starbucks Barista service using the Automatonymous library.  All I'm really looking for is a bit more of an elegant way to configure the AutomatonymousStateMachineSagaRepository class in the DI container, assuming I need it.

So my Windsor container setup is like this:

var container = new WindsorContainer();
            container.Register(
                Component.For(typeof(ISessionFactory))
                    .UsingFactoryMethod(() =>  CreateASMSessionFactory())
                    .LifeStyle.Singleton,
                Component.For(typeof(ISagaRepository<>))
                    .ImplementedBy(typeof(NHibernateSagaRepository<>))                   
                    .LifeStyle.Singleton,               
                Component.For<AutomatonymousStateMachineSagaRepository<DrinkPreparationSagaInstance>>().UsingFactoryMethod(() =>
                {
                    var _repository = container.Resolve<ISagaRepository<DrinkPreparationSagaInstance>>();
                    return new AutomatonymousStateMachineSagaRepository<DrinkPreparationSagaInstance>(_repository,
                        x => false, Enumerable.Empty<StateMachineEventCorrelation<DrinkPreparationSagaInstance>>());
                }).LifeStyle.Singleton,
                Component.For<DrinkPreparationSagaASM>(),
                Component.For<BaristaService>()
                    .LifeStyle.Singleton,
                Component.For<IServiceBus>().UsingFactoryMethod(() =>
                    {
                        return ServiceBusFactory.New(sbc =>
                            {                               
                                sbc.UseRabbitMq();
                                sbc.ReceiveFrom("rabbitmq://localhost/starbucks_barista");
                                sbc.UseControlBus();
                               

                                sbc.Subscribe(subs => { subs.LoadFrom(container); });

                                sbc.Subscribe(subs =>
                                    {                                      
                                        var _machine = container.Resolve<DrinkPreparationSagaASM>();

                                        AutomatonymousStateUserType<DrinkPreparationSagaASM>.SaveAsString(_machine);

                                        var _repository = container.Resolve<AutomatonymousStateMachineSagaRepository<DrinkPreparationSagaInstance>>();

                                        subs.StateMachineSaga(_machine, _repository, x =>
                                        {
                                            x.RemoveWhenFinalized();
                                        });
                                    });
                               
                            });
                    }).LifeStyle.Singleton);

Chris Patterson

unread,
May 30, 2014, 3:28:48 PM5/30/14
to masstrans...@googlegroups.com
.Subscribe(x => x.StateMachineSaga<T>(machine, repository)) is what I think you're looking for -- it uses the regular NHibernate saga repository and puts the appropriate Automatonymous repository in front of it to handle the states and events for you.


Mark Gillen

unread,
May 30, 2014, 3:34:02 PM5/30/14
to masstrans...@googlegroups.com
Thanks, Chris.

Yea I do use this to do the subscription but I'm
AutomatonymousStateMachineSagaRepository.  When I replace it with just an NHibernateSagaRepository I got some funky errors about not being able to find any persistors etc.  I'm not using RapidTransit (Riktig example is very good) so I'll look at the how I wired up this thing again.

Anyway thanks..I did get it all working so that's another step in the right direction.

Regards,
Mark


Reply all
Reply to author
Forward
0 new messages