-
Notifications
You must be signed in to change notification settings - Fork 310
Add configuration to disable entity lifecycle events #1291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
import java.util.List; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Stream; | ||
|
||
import org.apache.commons.logging.Log; | ||
|
@@ -118,7 +119,7 @@ public class CassandraTemplate implements CassandraOperations, ApplicationEventP | |
|
||
private final StatementFactory statementFactory; | ||
|
||
private @Nullable ApplicationEventPublisher eventPublisher; | ||
private final EntityLifecycleEventDelegate eventDelegate; | ||
|
||
private @Nullable EntityCallbacks entityCallbacks; | ||
|
||
|
@@ -183,6 +184,7 @@ public CassandraTemplate(CqlOperations cqlOperations, CassandraConverter convert | |
this.cqlOperations = cqlOperations; | ||
this.entityOperations = new EntityOperations(converter); | ||
this.statementFactory = new StatementFactory(new QueryMapper(converter), new UpdateMapper(converter)); | ||
this.eventDelegate = new EntityLifecycleEventDelegate(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again: I'd prefer initialization at declaration time when possible. |
||
} | ||
|
||
@Override | ||
|
@@ -192,7 +194,7 @@ public CassandraBatchOperations batchOps(BatchType batchType) { | |
|
||
@Override | ||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { | ||
this.eventPublisher = applicationEventPublisher; | ||
this.eventDelegate.setPublisher(applicationEventPublisher); | ||
} | ||
|
||
@Override | ||
|
@@ -212,6 +214,18 @@ public void setEntityCallbacks(@Nullable EntityCallbacks entityCallbacks) { | |
this.entityCallbacks = entityCallbacks; | ||
} | ||
|
||
/** | ||
* Configure whether lifecycle events such as {@link AfterLoadEvent}, {@link BeforeSaveEvent}, etc. should be | ||
* published or whether emission should be suppressed. Enabled by default. | ||
* | ||
* @param enabled {@code true} to enable entity lifecycle events; {@code false} to disable entity lifecycle events. | ||
* @since 4.0 | ||
* @see CassandraMappingEvent | ||
*/ | ||
public void setEntityLifecycleEventsEnabled(boolean enabled) { | ||
this.eventDelegate.setEventsEnabled(enabled); | ||
} | ||
|
||
@Override | ||
public CqlOperations getCqlOperations() { | ||
return this.cqlOperations; | ||
|
@@ -488,11 +502,11 @@ WriteResult doDelete(Query query, Class<?> entityClass, CqlIdentifier tableName) | |
tableName); | ||
SimpleStatement statement = delete.build(); | ||
|
||
maybeEmitEvent(new BeforeDeleteEvent<>(statement, entityClass, tableName)); | ||
maybeEmitEvent(() -> new BeforeDeleteEvent<>(statement, entityClass, tableName)); | ||
|
||
WriteResult writeResult = doExecute(statement); | ||
|
||
maybeEmitEvent(new AfterDeleteEvent<>(statement, entityClass, tableName)); | ||
maybeEmitEvent(() -> new AfterDeleteEvent<>(statement, entityClass, tableName)); | ||
|
||
return writeResult; | ||
} | ||
|
@@ -700,8 +714,8 @@ private WriteResult doDeleteVersioned(SimpleStatement statement, Object entity, | |
|
||
if (!result.wasApplied()) { | ||
throw new OptimisticLockingFailureException( | ||
String.format("Cannot delete entity %s with version %s in table %s; Has it been modified meanwhile", | ||
entity, source.getVersion(), tableName)); | ||
String.format("Cannot delete entity %s with version %s in table %s; Has it been modified meanwhile", entity, | ||
source.getVersion(), tableName)); | ||
} | ||
}); | ||
} | ||
|
@@ -722,11 +736,11 @@ public boolean deleteById(Object id, Class<?> entityClass) { | |
StatementBuilder<Delete> delete = getStatementFactory().deleteById(id, entity, tableName); | ||
SimpleStatement statement = delete.build(); | ||
|
||
maybeEmitEvent(new BeforeDeleteEvent<>(statement, entityClass, tableName)); | ||
maybeEmitEvent(() -> new BeforeDeleteEvent<>(statement, entityClass, tableName)); | ||
|
||
boolean result = doExecute(statement).wasApplied(); | ||
|
||
maybeEmitEvent(new AfterDeleteEvent<>(statement, entityClass, tableName)); | ||
maybeEmitEvent(() -> new AfterDeleteEvent<>(statement, entityClass, tableName)); | ||
|
||
return result; | ||
} | ||
|
@@ -740,11 +754,11 @@ public void truncate(Class<?> entityClass) { | |
Truncate truncate = QueryBuilder.truncate(tableName); | ||
SimpleStatement statement = truncate.build(); | ||
|
||
maybeEmitEvent(new BeforeDeleteEvent<>(statement, entityClass, tableName)); | ||
maybeEmitEvent(() -> new BeforeDeleteEvent<>(statement, entityClass, tableName)); | ||
|
||
doExecute(statement); | ||
|
||
maybeEmitEvent(new AfterDeleteEvent<>(statement, entityClass, tableName)); | ||
maybeEmitEvent(() -> new AfterDeleteEvent<>(statement, entityClass, tableName)); | ||
} | ||
|
||
// ------------------------------------------------------------------------- | ||
|
@@ -795,27 +809,27 @@ private <T> EntityWriteResult<T> executeSave(T entity, CqlIdentifier tableName, | |
private <T> EntityWriteResult<T> executeSave(T entity, CqlIdentifier tableName, SimpleStatement statement, | ||
Consumer<WriteResult> resultConsumer) { | ||
|
||
maybeEmitEvent(new BeforeSaveEvent<>(entity, tableName, statement)); | ||
maybeEmitEvent(() -> new BeforeSaveEvent<>(entity, tableName, statement)); | ||
T entityToSave = maybeCallBeforeSave(entity, tableName, statement); | ||
|
||
WriteResult result = doExecute(statement); | ||
resultConsumer.accept(result); | ||
|
||
maybeEmitEvent(new AfterSaveEvent<>(entityToSave, tableName)); | ||
maybeEmitEvent(() -> new AfterSaveEvent<>(entityToSave, tableName)); | ||
|
||
return EntityWriteResult.of(result, entityToSave); | ||
} | ||
|
||
private WriteResult executeDelete(Object entity, CqlIdentifier tableName, SimpleStatement statement, | ||
Consumer<WriteResult> resultConsumer) { | ||
|
||
maybeEmitEvent(new BeforeDeleteEvent<>(statement, entity.getClass(), tableName)); | ||
maybeEmitEvent(() -> new BeforeDeleteEvent<>(statement, entity.getClass(), tableName)); | ||
|
||
WriteResult result = doExecute(statement); | ||
|
||
resultConsumer.accept(result); | ||
|
||
maybeEmitEvent(new AfterDeleteEvent<>(statement, entity.getClass(), tableName)); | ||
maybeEmitEvent(() -> new AfterDeleteEvent<>(statement, entity.getClass(), tableName)); | ||
|
||
return result; | ||
} | ||
|
@@ -907,12 +921,12 @@ private <T> Function<Row, T> getMapper(EntityProjection<T, ?> projection, CqlIde | |
|
||
return row -> { | ||
|
||
maybeEmitEvent(new AfterLoadEvent<>(row, targetType, tableName)); | ||
maybeEmitEvent(() -> new AfterLoadEvent<>(row, targetType, tableName)); | ||
|
||
T result = getConverter().project(projection, row); | ||
|
||
if (result != null) { | ||
maybeEmitEvent(new AfterConvertEvent<>(row, result, tableName)); | ||
maybeEmitEvent(() -> new AfterConvertEvent<>(row, result, tableName)); | ||
} | ||
|
||
return result; | ||
|
@@ -930,11 +944,8 @@ private static MappingCassandraConverter newConverter(CqlSession session) { | |
return converter; | ||
} | ||
|
||
protected <E extends CassandraMappingEvent<T>, T> void maybeEmitEvent(E event) { | ||
|
||
if (this.eventPublisher != null) { | ||
this.eventPublisher.publishEvent(event); | ||
} | ||
protected <E extends CassandraMappingEvent<T>, T> void maybeEmitEvent(Supplier<E> event) { | ||
this.eventDelegate.publishEvent(event); | ||
} | ||
|
||
protected <T> T maybeCallBeforeConvert(T object, CqlIdentifier tableName) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpicking: I'd prefer to initialize this on declaration since it does not depend on anything in the constructor.