Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2010-2023. 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 org.axonframework.common.AxonConfigurationException;
import org.axonframework.common.transaction.TransactionManager;
import org.axonframework.eventhandling.DomainEventMessage;
import org.axonframework.eventhandling.GenericDomainEventMessage;
import org.axonframework.eventsourcing.eventstore.DomainEventStream;
import org.axonframework.eventsourcing.eventstore.EventStore;
import org.axonframework.messaging.Message;
import org.axonframework.messaging.MetaData;
import org.axonframework.messaging.annotation.ClasspathHandlerDefinition;
import org.axonframework.messaging.annotation.ClasspathParameterResolverFactory;
import org.axonframework.messaging.annotation.HandlerDefinition;
import org.axonframework.messaging.annotation.ParameterResolverFactory;
import org.axonframework.modelling.command.ApplyMore;
import org.axonframework.modelling.command.RepositoryProvider;
import org.axonframework.modelling.command.inspection.AggregateModel;
import org.axonframework.modelling.command.inspection.AnnotatedAggregateMetaModelFactory;
import org.axonframework.tracing.SpanFactory;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import static org.axonframework.common.BuilderUtils.assertNonNull;
/**
* Implementation of a snapshotter that uses the actual aggregate and its state to create a snapshot event. The
* motivation is that an aggregate always contains all relevant state. Therefore, storing the aggregate itself inside an
* event should capture all necessary information.
*
* @author Allard Buijze
* @since 0.6
*/
public class AggregateSnapshotter extends AbstractSnapshotter {
private final Map, AggregateFactory>> aggregateFactories;
private final RepositoryProvider repositoryProvider;
private final ParameterResolverFactory parameterResolverFactory;
private final HandlerDefinition handlerDefinition;
private final Map, AggregateModel>> aggregateModels = new ConcurrentHashMap<>();
/**
* Instantiate a {@link AggregateSnapshotter} based on the fields contained in the {@link Builder}.
*
* Will assert that the {@link EventStore}, {@link ParameterResolverFactory} and {@link HandlerDefinition} are not
* {@code null}, and will throw an {@link AxonConfigurationException} if any of them is {@code null}. The
* {@link SpanFactory} is defaulted to a {@link org.axonframework.tracing.NoOpSpanFactory}.
*
* @param builder the {@link Builder} used to instantiate a {@link AggregateSnapshotter} instance
*/
protected AggregateSnapshotter(Builder builder) {
super(builder);
this.aggregateFactories = new ConcurrentHashMap<>(builder.aggregateFactories);
this.repositoryProvider = builder.repositoryProvider;
this.parameterResolverFactory = builder.buildParameterResolverFactory();
this.handlerDefinition = builder.buildHandlerDefinition();
}
/**
* Instantiate a Builder to be able to create a {@link AggregateSnapshotter}.
*
* The {@link Executor} is defaulted to an {@link org.axonframework.common.DirectExecutor#INSTANCE} and the
* {@link TransactionManager} defaults to a {@link org.axonframework.common.transaction.NoTransactionManager}.
* Additionally, this Builder has convenience functions to default the {@link ParameterResolverFactory} and
* {@link HandlerDefinition} based on instances of these available on the classpath in case these are not provided
* (respectively {@link Builder#buildParameterResolverFactory()} and {@link Builder#buildHandlerDefinition()}). Upon
* instantiation of a {@link AggregateSnapshotter}, it is recommended to use these function to set those fields.
*
* The {@link EventStore} is a hard requirement and as such should be provided.
*
* @return a Builder to be able to create a {@link AggregateSnapshotter}
* @see ClasspathParameterResolverFactory
* @see ClasspathHandlerDefinition
*/
public static Builder builder() {
return new Builder();
}
@Override
protected DomainEventMessage createSnapshot(Class> aggregateType,
String aggregateIdentifier,
DomainEventStream eventStream) {
DomainEventMessage> firstEvent = eventStream.peek();
AggregateFactory> aggregateFactory = getAggregateFactory(aggregateType);
if (aggregateFactory == null) {
throw new IllegalArgumentException(
"Aggregate Type is unknown in this snapshotter: " + aggregateType.getName()
);
}
aggregateModels.computeIfAbsent(aggregateType,
k -> AnnotatedAggregateMetaModelFactory
.inspectAggregate(k, parameterResolverFactory, handlerDefinition));
Object aggregateRoot = aggregateFactory.createAggregateRoot(aggregateIdentifier, firstEvent);
//noinspection rawtypes,unchecked
SnapshotAggregate