Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2010-2011. Axon Framework
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.axonframework.saga;
 
 
 import java.util.Set;
 
 import static java.lang.String.format;

Abstract implementation of the SagaManager interface that provides basic functionality required by most SagaManager implementations. Provides support for Saga lifecycle management and asynchronous handling of events.

Author(s):
Allard Buijze
Since:
0.7
 
 public abstract class AbstractSagaManager implements SagaManagerSubscribable {
 
     private static final Logger logger = LoggerFactory.getLogger(AbstractSagaManager.class);
 
     private final EventBus eventBus;
     private final SagaRepository sagaRepository;
     private SagaFactory sagaFactory;
     private volatile boolean suppressExceptions = true;
     private volatile boolean synchronizeSagaAccess = true;
     private final SagaHandlerExecutor executionWrapper;

    
Initializes the SagaManager with the given eventBus and sagaRepository.

Parameters:
eventBus The event bus providing the events to route to sagas.
sagaRepository The repository providing the saga instances.
sagaFactory The factory providing new saga instances
 
     public AbstractSagaManager(EventBus eventBusSagaRepository sagaRepositorySagaFactory sagaFactory) {
         this. = eventBus;
         this. = sagaRepository;
         this. = sagaFactory;
         this. = new SynchronousSagaExecutionWrapper();
     }

    
Initializes the SagaManager with the given eventBus and sagaRepository which handles the saga lookup and invocation asynchronously using the given executor and transactionManager.

Parameters:
eventBus The event bus providing the events to route to sagas.
sagaRepository The repository providing the saga instances.
sagaFactory The factory providing new saga instances
executor The executor providing the threads to process events in
transactionManager The transaction manager that manages transactions around event processing
 
     public AbstractSagaManager(EventBus eventBusSagaRepository sagaRepositorySagaFactory sagaFactory,
                                Executor executorTransactionManager transactionManager) {
         this. = eventBus;
         this. = sagaRepository;
         this. = sagaFactory;
         this. = new AsynchronousSagaExecutor(executortransactionManager);
     }
 
     @Override
     public void handle(final Event event) {
     }

    
Create a new instance of a Saga of the given sagaType. Resources must have been injected into the Saga before returning it.

Parameters:
sagaType The type of Saga to create an instance for
<T> The type of Saga to create an instance for
Returns:
A newly created Saga.
 
     protected <T extends Saga> T createSaga(Class<T> sagaType) {
         return .createSaga(sagaType);
     }
    private void invokeSagaHandler(Event eventSaga saga) {
        if (!saga.isActive()) {
            return;
        }
        try {
            saga.handle(event);
        } catch (RuntimeException e) {
            if () {
                .error(format("An exception occurred while a Saga [%s] was handling an Event [%s]:",
                                    saga.getClass().getSimpleName(),
                                    event.getClass().getSimpleName()),
                             e);
            } else {
                throw e;
            }
        } finally {
            commit(saga);
        }
    }

    
Finds the saga instances that the given event needs to be routed to. The event is sent to each of the returned instances.

Parameters:
event The event to find relevant Sagas for
Returns:
The Set of relevant Sagas
    protected abstract Set<SagafindSagas(Event event);

    
Commits the given saga to the registered repository.

Parameters:
saga the Saga to commit.
    protected void commit(Saga saga) {
        .commit(saga);
    }

    
Unsubscribe the EventListener with the configured EventBus.
    @Override
    @PreDestroy
    public void unsubscribe() {
        .unsubscribe(this);
    }

    
Subscribe the EventListener with the configured EventBus.
    @Override
    public void subscribe() {
        .subscribe(this);
    }

    
Returns the EventBus that delivers the events to route to Sagas.

Returns:
the EventBus that delivers the events to route to Sagas
    protected EventBus getEventBus() {
        return ;
    }

    
Returns the repository that provides access to Saga instances.

Returns:
the repository that provides access to Saga instances
    protected SagaRepository getSagaRepository() {
        return ;
    }

    
Sets whether or not to suppress any exceptions that are cause by invoking Sagas. When suppressed, exceptions are logged. Defaults to true.

Parameters:
suppressExceptions whether or not to suppress exceptions from Sagas.
    public void setSuppressExceptions(boolean suppressExceptions) {
        this. = suppressExceptions;
    }

    
Sets whether of not access to Saga's Event Handler should by synchronized. Defaults to true. Sets to false only if the Saga managed by this manager are completely thread safe by themselves.

Parameters:
synchronizeSagaAccess whether or not to synchronize access to Saga's event handlers.
    public void setSynchronizeSagaAccess(boolean synchronizeSagaAccess) {
        this. = synchronizeSagaAccess;
    }
    private class SagaInvocationTask implements Runnable {
        private final Saga saga;
        private final Event event;
        public SagaInvocationTask(Saga sagaEvent event) {
            this. = saga;
            this. = event;
        }
        @Override
        public void run() {
            if () {
                //Saga instance is unique (no two instances will exist inside a JVM with the same identifier)
                //noinspection SynchronizationOnLocalVariableOrMethodParameter
                synchronized () {
                    invokeSagaHandler();
                }
            } else {
                invokeSagaHandler();
            }
        }
    }
    private class SagaLookupAndInvocationTask implements Runnable {
        private final Event event;
        public SagaLookupAndInvocationTask(Event event) {
            this. = event;
        }
        @Override
        public void run() {
            Set<Sagasagas = findSagas();
            for (final Saga saga : sagas) {
                .scheduleEventProcessingTask(saganew SagaInvocationTask(saga));
            }
        }
    }
New to GrepCode? Check out our FAQ X