Skip to content

Commit

Permalink
Simplification of configuration of MongoTemplate
Browse files Browse the repository at this point in the history
Instead of having an instance for each components, now a single
MongoTemplate instance is used for all mongo based components. The old
implementations still exist, but are deprecated and will be removed
in a future release.

This commit also contains some improvements on the implementation of
the MongoTokenStore.
  • Loading branch information
abuijze committed Nov 18, 2017
1 parent 03ecdd8 commit 99b17c7
Show file tree
Hide file tree
Showing 15 changed files with 454 additions and 198 deletions.
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2010-2016. Axon Framework * Copyright (c) 2010-2017. Axon Framework
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -55,6 +55,10 @@ protected AbstractMongoTemplate(MongoClient mongo, String databaseName) { // NOS
database = mongo.getDatabase(databaseName); database = mongo.getDatabase(databaseName);
} }


protected AbstractMongoTemplate(MongoDatabase database) {
this.database = database;
}

/** /**
* Returns a reference to the Database with the configured database name. If a username and/or password have been * Returns a reference to the Database with the configured database name. If a username and/or password have been
* provided, these are used to authenticate against the database. * provided, these are used to authenticate against the database.
Expand Down
132 changes: 132 additions & 0 deletions mongo/src/main/java/org/axonframework/mongo/DefaultMongoTemplate.java
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright (c) 2010-2017. 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.mongo;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

/**
* MongoTemplate instance giving direct access to the TrackingToken collection via a given MongoClient instance.
*/
public class DefaultMongoTemplate extends AbstractMongoTemplate implements MongoTemplate {

private static final String DEFAULT_TRACKINGTOKENS_COLLECTION_NAME = "trackingtokens";
private static final String DEFAULT_DOMAINEVENTS_COLLECTION = "domainevents";
private static final String DEFAULT_SNAPSHOTEVENTS_COLLECTION = "snapshotevents";
private static final String DEFAULT_SAGAS_COLLECTION_NAME = "sagas";

private final String domainEventsCollectionName;
private final String snapshotEventsCollectionName;
private final String trackingTokensCollectionName;
private final String sagasCollectionName;

/**
* Initialize a template for the given {@code mongoDb} instance, using default database name ("axonframework")
* and collection names ("domainevents", "snapshotevents", "sagas" and "trackingtokens").
*
* @param mongo The Mongo instance providing access to the database
*/
public DefaultMongoTemplate(MongoClient mongo) {
super(mongo);
this.trackingTokensCollectionName = DEFAULT_TRACKINGTOKENS_COLLECTION_NAME;
this.snapshotEventsCollectionName = DEFAULT_SNAPSHOTEVENTS_COLLECTION;
this.sagasCollectionName = DEFAULT_SAGAS_COLLECTION_NAME;
this.domainEventsCollectionName = DEFAULT_DOMAINEVENTS_COLLECTION;
}

/**
* Initialize a template for the given {@code mongoDb} instance, using given {@code databaseName} and default
* collection names ("domainevents", "snapshotevents", "sagas" and "trackingtokens").
*
* @param mongo The Mongo instance providing access to the database
* @param databaseName The name of the database to connect to
*/
public DefaultMongoTemplate(MongoClient mongo, String databaseName) {
super(mongo, databaseName);
this.trackingTokensCollectionName = DEFAULT_TRACKINGTOKENS_COLLECTION_NAME;
this.snapshotEventsCollectionName = DEFAULT_SNAPSHOTEVENTS_COLLECTION;
this.sagasCollectionName = DEFAULT_SAGAS_COLLECTION_NAME;
this.domainEventsCollectionName = DEFAULT_DOMAINEVENTS_COLLECTION;
}

private DefaultMongoTemplate(MongoDatabase database,
String domainEventsCollectionName,
String snapshotEventsCollectionName,
String sagasCollectionName,
String trackingTokensCollectionName) {
super(database);
this.domainEventsCollectionName = domainEventsCollectionName;
this.snapshotEventsCollectionName = snapshotEventsCollectionName;
this.sagasCollectionName = sagasCollectionName;
this.trackingTokensCollectionName = trackingTokensCollectionName;
}

public DefaultMongoTemplate withSnapshotCollection(String snapshotEventsCollectionName) {
return new DefaultMongoTemplate(database(),
domainEventsCollectionName,
snapshotEventsCollectionName,
sagasCollectionName,
trackingTokensCollectionName);
}

public DefaultMongoTemplate withDomainEventsCollection(String domainEventsCollectionName) {
return new DefaultMongoTemplate(database(),
domainEventsCollectionName,
snapshotEventsCollectionName,
sagasCollectionName,
trackingTokensCollectionName);
}

public DefaultMongoTemplate withSagasCollection(String sagasCollectionName) {
return new DefaultMongoTemplate(database(),
domainEventsCollectionName,
snapshotEventsCollectionName,
sagasCollectionName,
trackingTokensCollectionName);
}


public DefaultMongoTemplate withTrackingTokenCollection(String trackingTokensCollectionName) {
return new DefaultMongoTemplate(database(),
domainEventsCollectionName,
snapshotEventsCollectionName,
sagasCollectionName,
trackingTokensCollectionName);
}

@Override
public MongoCollection<Document> trackingTokensCollection() {
return database().getCollection(trackingTokensCollectionName);
}

@Override
public MongoCollection<Document> eventCollection() {
return database().getCollection(domainEventsCollectionName);
}

@Override
public MongoCollection<Document> snapshotCollection() {
return database().getCollection(snapshotEventsCollectionName);
}

@Override
public MongoCollection<Document> sagaCollection() {
return database().getCollection(sagasCollectionName);
}
}
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2010-2016. Axon Framework * Copyright (c) 2010-2017. Axon Framework
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
Expand All @@ -14,31 +14,42 @@
* limitations under the License. * limitations under the License.
*/ */


package org.axonframework.mongo.eventsourcing.eventstore; package org.axonframework.mongo;


import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCollection;
import org.bson.Document; import org.bson.Document;


/** /**
* Interface describing a mechanism that provides access to the Database and Collections required by the * Template object providing access to the collections necessary for the Mongo based components.
* MongoEventStore.
*
* @author Jettro Coenradie
* @since 2.0 (in incubator since 0.7)
*/ */
public interface MongoTemplate { public interface MongoTemplate {


/**
* Returns a reference to the collection containing the Tracking Tokens.
*
* @return MongoCollection containing the Tracking Tokens
*/
MongoCollection<Document> trackingTokensCollection();

/** /**
* Returns a reference to the collection containing the events. * Returns a reference to the collection containing the events.
* *
* @return DBCollection containing the domain events * @return MongoCollection containing the domain events
*/ */
MongoCollection<Document> eventCollection(); MongoCollection<Document> eventCollection();


/** /**
* Returns a reference to the collection containing the snapshot events. * Returns a reference to the collection containing the snapshot events.
* *
* @return DBCollection containing the snapshot events * @return MongoCollection containing the snapshot events
*/ */
MongoCollection<Document> snapshotCollection(); MongoCollection<Document> snapshotCollection();

/**
* Returns a reference to the collection containing the saga instances.
*
* @return MongoCollection containing the sagas
*/
MongoCollection<Document> sagaCollection();

} }
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2010-2016. Axon Framework * Copyright (c) 2010-2017. Axon Framework
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCollection;
import org.axonframework.mongo.AbstractMongoTemplate; import org.axonframework.mongo.AbstractMongoTemplate;
import org.axonframework.mongo.MongoTemplate;
import org.bson.Document; import org.bson.Document;


/** /**
Expand All @@ -27,7 +28,9 @@
* @author Jettro Coenradie * @author Jettro Coenradie
* @author Allard Buijze * @author Allard Buijze
* @since 2.0 * @since 2.0
* @deprecated Use {@link org.axonframework.mongo.DefaultMongoTemplate} instead.
*/ */
@Deprecated
public class DefaultMongoTemplate extends AbstractMongoTemplate implements MongoTemplate { public class DefaultMongoTemplate extends AbstractMongoTemplate implements MongoTemplate {


private static final String DEFAULT_SAGAS_COLLECTION_NAME = "sagas"; private static final String DEFAULT_SAGAS_COLLECTION_NAME = "sagas";
Expand Down Expand Up @@ -58,6 +61,21 @@ public DefaultMongoTemplate(MongoClient mongo, String databaseName, String sagas
this.sagasCollectionName = sagasCollectionName; this.sagasCollectionName = sagasCollectionName;
} }


@Override
public MongoCollection<Document> trackingTokensCollection() {
throw new UnsupportedOperationException("Not supported.");
}

@Override
public MongoCollection<Document> eventCollection() {
throw new UnsupportedOperationException("Not supported.");
}

@Override
public MongoCollection<Document> snapshotCollection() {
throw new UnsupportedOperationException("Not supported.");
}

@Override @Override
public MongoCollection<Document> sagaCollection() { public MongoCollection<Document> sagaCollection() {
return database().getCollection(sagasCollectionName); return database().getCollection(sagasCollectionName);
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2010-2016. Axon Framework * Copyright (c) 2010-2017. Axon Framework
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
import org.axonframework.eventhandling.saga.AssociationValues; import org.axonframework.eventhandling.saga.AssociationValues;
import org.axonframework.eventhandling.saga.repository.SagaStore; import org.axonframework.eventhandling.saga.repository.SagaStore;
import org.axonframework.eventsourcing.eventstore.TrackingToken; import org.axonframework.eventsourcing.eventstore.TrackingToken;
import org.axonframework.mongo.MongoTemplate;
import org.axonframework.serialization.Serializer; import org.axonframework.serialization.Serializer;
import org.axonframework.serialization.xml.XStreamSerializer; import org.axonframework.serialization.xml.XStreamSerializer;
import org.bson.Document; import org.bson.Document;
Expand Down

This file was deleted.

Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2010-2016. Axon Framework * Copyright (c) 2010-2017. Axon Framework
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCollection;
import org.axonframework.mongo.AbstractMongoTemplate; import org.axonframework.mongo.AbstractMongoTemplate;
import org.axonframework.mongo.MongoTemplate;
import org.bson.Document; import org.bson.Document;


/** /**
Expand All @@ -29,7 +30,9 @@
* @author Allard Buijze * @author Allard Buijze
* @author Jettro Coenradie * @author Jettro Coenradie
* @since 2.0 (in incubator since 0.7) * @since 2.0 (in incubator since 0.7)
* @deprecated Use {@link org.axonframework.mongo.DefaultMongoTemplate} instead.
*/ */
@Deprecated
public class DefaultMongoTemplate extends AbstractMongoTemplate implements MongoTemplate { public class DefaultMongoTemplate extends AbstractMongoTemplate implements MongoTemplate {


private static final String DEFAULT_DOMAINEVENTS_COLLECTION = "domainevents"; private static final String DEFAULT_DOMAINEVENTS_COLLECTION = "domainevents";
Expand Down Expand Up @@ -74,6 +77,12 @@ public DefaultMongoTemplate(MongoClient mongo, String databaseName, String domai
this.snapshotEventsCollectionName = snapshotEventsCollectionName; this.snapshotEventsCollectionName = snapshotEventsCollectionName;
} }


@Override
public MongoCollection<Document> trackingTokensCollection() {
throw new UnsupportedOperationException("Not supported.");

}

/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
Expand All @@ -89,4 +98,9 @@ public MongoCollection<Document> eventCollection() {
public MongoCollection<Document> snapshotCollection() { public MongoCollection<Document> snapshotCollection() {
return database().getCollection(snapshotEventsCollectionName); return database().getCollection(snapshotEventsCollectionName);
} }

@Override
public MongoCollection<Document> sagaCollection() {
throw new UnsupportedOperationException("Not supported.");
}
} }
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2010-2016. Axon Framework * Copyright (c) 2010-2017. Axon Framework
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
Expand All @@ -25,6 +25,7 @@
import org.axonframework.eventsourcing.eventstore.DomainEventData; import org.axonframework.eventsourcing.eventstore.DomainEventData;
import org.axonframework.eventsourcing.eventstore.TrackedEventData; import org.axonframework.eventsourcing.eventstore.TrackedEventData;
import org.axonframework.eventsourcing.eventstore.TrackingToken; import org.axonframework.eventsourcing.eventstore.TrackingToken;
import org.axonframework.mongo.MongoTemplate;
import org.axonframework.mongo.eventsourcing.eventstore.documentperevent.DocumentPerEventStorageStrategy; import org.axonframework.mongo.eventsourcing.eventstore.documentperevent.DocumentPerEventStorageStrategy;
import org.axonframework.serialization.Serializer; import org.axonframework.serialization.Serializer;
import org.axonframework.serialization.upcasting.event.EventUpcaster; import org.axonframework.serialization.upcasting.event.EventUpcaster;
Expand Down
Loading

0 comments on commit 99b17c7

Please sign in to comment.