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.eventsourcing;
 
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;

Implementation of the AggregateSnapshotter that eases the configuration when used within a Spring Application Context. It will automatically detect a Transaction Manager and Aggregate Factories.

The only mandatory properties to set is AbstractSnapshotter.setEventStore(org.axonframework.eventstore.SnapshotEventStore). In most cases, you should also provide an executor, as the default will execute snapshotter tasks in the calling thread.

Author(s):
Allard Buijze
Since:
0.6
 
         implements InitializingBeanApplicationContextAware {
 
     private boolean autoDetectAggregateFactories = true;
 
     @Override
     protected Runnable createSnapshotterTask(String typeIdentifierAggregateIdentifier aggregateIdentifier) {
         Runnable command = super.createSnapshotterTask(typeIdentifieraggregateIdentifier);
         if ( != null) {
             return new TransactionalRunnableWrapper(command);
         }
         return command;
     }

    
Sets the transaction manager to manager underlying transaction with. If none is provided, an attempt is made to auto detect is from the application context. If a single transaction manager is found, it is used to manage transactions. Of none or more than one is found, they are ignored.

Parameters:
transactionManager the transaction manager managing underlying transactions
 
     public void setTransactionManager(PlatformTransactionManager transactionManager) {
         this. = transactionManager;
     }

    
Optionally sets the transaction definition to use. By default, uses the application context's default transaction semantics (see org.springframework.transaction.support.DefaultTransactionDefinition).

Parameters:
transactionDefinition the transaction definition to use
 
     public void setTransactionDefinition(TransactionDefinition transactionDefinition) {
         this. = transactionDefinition;
     }
 
     @SuppressWarnings({"unchecked"})
     @Override
     public void afterPropertiesSet() throws Exception {
         if () {
             Set<AggregateFactoryfactoriesFound = new HashSet<AggregateFactory>();
             factoriesFound.addAll(.getBeansOfType(AggregateFactory.class).values());
             Collection<EventSourcingRepositoryeventSourcingRepositories =
                     .getBeansOfType(EventSourcingRepository.class).values();
             for (EventSourcingRepository repo : eventSourcingRepositories) {
                 factoriesFound.add(repo.getAggregateFactory());
             }
             setAggregateFactories(new ArrayList(factoriesFound));
         }
 
        if ( == null) {
            Map<StringPlatformTransactionManagercandidates = .getBeansOfType(
                    PlatformTransactionManager.class);
            if (candidates.size() == 1) {
                this. = candidates.values().iterator().next();
            }
        }
    }

    
Optionally sets the aggregate factories to use. By default, this implementation will auto detect available factories from the application context. Configuring them using this method will prevent auto detection.

Parameters:
aggregateFactories The list of aggregate factories creating the aggregates to store.
    @Override
    public void setAggregateFactories(List<AggregateFactory<?>> aggregateFactories) {
        this. = false;
        super.setAggregateFactories(aggregateFactories);
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContextthrows BeansException {
        this. = applicationContext;
    }
    private class TransactionalRunnableWrapper implements Runnable {
        private final Runnable command;
        public TransactionalRunnableWrapper(Runnable command) {
            this. = command;
        }
        @Override
        public void run() {
            try {
                .run();
                if (transaction.isNewTransaction()) {
                    .commit(transaction);
                }
            } catch (RuntimeException e) {
                if (transaction.isNewTransaction()) {
                    .rollback(transaction);
                }
                throw e;
            }
        }
    }
New to GrepCode? Check out our FAQ X