Search in sources :

Example 1 with TwoPhaseCommitDeactivateOnCloseAction

use of won.bot.framework.eventbot.action.impl.facet.TwoPhaseCommitDeactivateOnCloseAction in project webofneeds by researchstudio-sat.

the class StandardTwoPhaseCommitBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = getEventBus();
    ParticipantCoordinatorBotContextWrapper botContextWrapper = (ParticipantCoordinatorBotContextWrapper) getBotContextWrapper();
    // create needs every trigger execution until noOfNeeds are created
    this.participantNeedCreator = new ActionOnEventListener(ctx, "participantCreator", new CreateNeedWithFacetsAction(ctx, botContextWrapper.getParticipantListName(), FacetType.ParticipantFacet.getURI()), noOfNeeds - 1);
    bus.subscribe(ActEvent.class, this.participantNeedCreator);
    // when done, create one coordinator need
    this.coordinatorNeedCreator = new ActionOnEventListener(ctx, "coordinatorCreator", new FinishedEventFilter(participantNeedCreator), new CreateNeedWithFacetsAction(ctx, botContextWrapper.getCoordinatorListName(), FacetType.CoordinatorFacet.getURI()), 1);
    bus.subscribe(FinishedEvent.class, this.coordinatorNeedCreator);
    // wait for N NeedCreatedEvents
    creationWaiter = new WaitForNEventsListener(ctx, noOfNeeds);
    bus.subscribe(NeedCreatedEvent.class, creationWaiter);
    // when done, connect the participants to the coordinator
    this.needConnector = new ActionOnEventListener(ctx, "needConnector", new FinishedEventFilter(creationWaiter), new ConnectFromListToListAction(ctx, botContextWrapper.getCoordinatorListName(), botContextWrapper.getParticipantListName(), FacetType.CoordinatorFacet.getURI(), FacetType.ParticipantFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi!"), 1);
    bus.subscribe(FinishedEvent.class, this.needConnector);
    // add a listener that is informed of the connect/open events and that auto-opens
    // subscribe it to:
    // * connect events - so it responds with open
    // * open events - so it responds with open (if the open received was the first open, and we still need to accept the connection)
    this.autoOpener = new ActionOnEventListener(ctx, new NeedUriInNamedListFilter(ctx, botContextWrapper.getParticipantListName()), new OpenConnectionAction(ctx, "Hi!"));
    bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
    // after the last connect event, all connections are closed!
    this.participantDeactivator = new ActionOnEventListener(ctx, "participantDeactivator", new NeedUriInNamedListFilter(ctx, botContextWrapper.getParticipantListName()), new TwoPhaseCommitDeactivateOnCloseAction(ctx), noOfNeeds - 1);
    bus.subscribe(CloseFromOtherNeedEvent.class, this.participantDeactivator);
    coordinatorDeactivator = new ActionOnEventListener(ctx, "coordinatorDeactivator", new FinishedEventFilter(participantDeactivator), new DeactivateAllNeedsOfListAction(ctx, botContextWrapper.getCoordinatorListName()), 1);
    bus.subscribe(FinishedEvent.class, coordinatorDeactivator);
    // add a listener that counts two NeedDeactivatedEvents and then tells the
    // framework that the bot's work is done
    this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, noOfNeeds, new SignalWorkDoneAction(ctx));
    bus.subscribe(NeedDeactivatedEvent.class, this.workDoneSignaller);
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ActionOnceAfterNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener) TwoPhaseCommitDeactivateOnCloseAction(won.bot.framework.eventbot.action.impl.facet.TwoPhaseCommitDeactivateOnCloseAction) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) NeedUriInNamedListFilter(won.bot.framework.eventbot.filter.impl.NeedUriInNamedListFilter) EventBus(won.bot.framework.eventbot.bus.EventBus) OpenConnectionAction(won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) DeactivateAllNeedsOfListAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) WaitForNEventsListener(won.bot.framework.eventbot.listener.impl.WaitForNEventsListener) FinishedEventFilter(won.bot.framework.eventbot.filter.impl.FinishedEventFilter) ParticipantCoordinatorBotContextWrapper(won.bot.framework.bot.context.ParticipantCoordinatorBotContextWrapper)

Aggregations

ParticipantCoordinatorBotContextWrapper (won.bot.framework.bot.context.ParticipantCoordinatorBotContextWrapper)1 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)1 TwoPhaseCommitDeactivateOnCloseAction (won.bot.framework.eventbot.action.impl.facet.TwoPhaseCommitDeactivateOnCloseAction)1 SignalWorkDoneAction (won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction)1 CreateNeedWithFacetsAction (won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction)1 DeactivateAllNeedsOfListAction (won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction)1 ConnectFromListToListAction (won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction)1 OpenConnectionAction (won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction)1 EventBus (won.bot.framework.eventbot.bus.EventBus)1 FinishedEventFilter (won.bot.framework.eventbot.filter.impl.FinishedEventFilter)1 NeedUriInNamedListFilter (won.bot.framework.eventbot.filter.impl.NeedUriInNamedListFilter)1 ActionOnEventListener (won.bot.framework.eventbot.listener.impl.ActionOnEventListener)1 ActionOnceAfterNEventsListener (won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener)1 WaitForNEventsListener (won.bot.framework.eventbot.listener.impl.WaitForNEventsListener)1