[jOOQ/jOOQ#11520] Add CallbackTransactionListener

This commit is contained in:
Lukas Eder 2021-02-25 11:01:06 +01:00
parent 5de6b504d8
commit 91e01b747d
4 changed files with 761 additions and 1 deletions

View File

@ -37,6 +37,11 @@
*/
package org.jooq;
import java.io.Serializable;
import java.util.function.Consumer;
import org.jooq.impl.CallbackRecordListener;
import org.jooq.impl.CallbackTransactionListener;
/**
* The <code>TransactionListener</code> SPI is used to intercept the
@ -44,7 +49,7 @@ package org.jooq;
*
* @author Lukas Eder
*/
public interface TransactionListener {
public interface TransactionListener extends Serializable {
/**
* Called before {@link TransactionProvider#begin(TransactionContext)}.
@ -76,4 +81,51 @@ public interface TransactionListener {
*/
void rollbackEnd(TransactionContext ctx);
/**
* Create a {@link TransactionListener} with a
* {@link #onBeginStart(Consumer)} implementation.
*/
static CallbackTransactionListener onBeginStart(Consumer<? super TransactionContext> onBeginStart) {
return new CallbackTransactionListener().onBeginStart(onBeginStart);
}
/**
* Create a {@link TransactionListener} with a {@link #onBeginEnd(Consumer)}
* implementation.
*/
static CallbackTransactionListener onBeginEnd(Consumer<? super TransactionContext> onBeginEnd) {
return new CallbackTransactionListener().onBeginEnd(onBeginEnd);
}
/**
* Create a {@link TransactionListener} with a
* {@link #onCommitStart(Consumer)} implementation.
*/
static CallbackTransactionListener onCommitStart(Consumer<? super TransactionContext> onCommitStart) {
return new CallbackTransactionListener().onCommitStart(onCommitStart);
}
/**
* Create a {@link TransactionListener} with a
* {@link #onCommitEnd(Consumer)} implementation.
*/
static CallbackTransactionListener onCommitEnd(Consumer<? super TransactionContext> onCommitEnd) {
return new CallbackTransactionListener().onCommitEnd(onCommitEnd);
}
/**
* Create a {@link TransactionListener} with a
* {@link #onRollbackStart(Consumer)} implementation.
*/
static CallbackTransactionListener onRollbackStart(Consumer<? super TransactionContext> onRollbackStart) {
return new CallbackTransactionListener().onRollbackStart(onRollbackStart);
}
/**
* Create a {@link TransactionListener} with a
* {@link #onRollbackEnd(Consumer)} implementation.
*/
static CallbackTransactionListener onRollbackEnd(Consumer<? super TransactionContext> onRollbackEnd) {
return new CallbackTransactionListener().onRollbackEnd(onRollbackEnd);
}
}

View File

@ -0,0 +1,508 @@
/*
* 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.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import java.util.function.Consumer;
import org.jooq.RecordContext;
import org.jooq.RecordListener;
/**
* A {@link RecordListener} that allows for functional composition.
* <p>
* For example: <code><pre>
* ParseListener listener = RecordListener
* .onLoadStart(ctx -&gt; something())
* .onLoadEnd(ctx -&gt; something());
* </pre></code>
*
* @author Lukas Eder
*/
public final class CallbackRecordListener implements RecordListener {
/**
* Generated UID
*/
private static final long serialVersionUID = -4135358887698253754L;
private final Consumer<? super RecordContext> onStoreStart;
private final Consumer<? super RecordContext> onStoreEnd;
private final Consumer<? super RecordContext> onInsertStart;
private final Consumer<? super RecordContext> onInsertEnd;
private final Consumer<? super RecordContext> onUpdateStart;
private final Consumer<? super RecordContext> onUpdateEnd;
private final Consumer<? super RecordContext> onMergeStart;
private final Consumer<? super RecordContext> onMergeEnd;
private final Consumer<? super RecordContext> onDeleteStart;
private final Consumer<? super RecordContext> onDeleteEnd;
private final Consumer<? super RecordContext> onLoadStart;
private final Consumer<? super RecordContext> onLoadEnd;
private final Consumer<? super RecordContext> onRefreshStart;
private final Consumer<? super RecordContext> onRefreshEnd;
private final Consumer<? super RecordContext> onException;
public CallbackRecordListener() {
this(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
}
private CallbackRecordListener(
Consumer<? super RecordContext> onStoreStart,
Consumer<? super RecordContext> onStoreEnd,
Consumer<? super RecordContext> onInsertStart,
Consumer<? super RecordContext> onInsertEnd,
Consumer<? super RecordContext> onUpdateStart,
Consumer<? super RecordContext> onUpdateEnd,
Consumer<? super RecordContext> onMergeStart,
Consumer<? super RecordContext> onMergeEnd,
Consumer<? super RecordContext> onDeleteStart,
Consumer<? super RecordContext> onDeleteEnd,
Consumer<? super RecordContext> onLoadStart,
Consumer<? super RecordContext> onLoadEnd,
Consumer<? super RecordContext> onRefreshStart,
Consumer<? super RecordContext> onRefreshEnd,
Consumer<? super RecordContext> onException
) {
this.onStoreStart = onStoreStart;
this.onStoreEnd = onStoreEnd;
this.onInsertStart = onInsertStart;
this.onInsertEnd = onInsertEnd;
this.onUpdateStart = onUpdateStart;
this.onUpdateEnd = onUpdateEnd;
this.onMergeStart = onMergeStart;
this.onMergeEnd = onMergeEnd;
this.onDeleteStart = onDeleteStart;
this.onDeleteEnd = onDeleteEnd;
this.onLoadStart = onLoadStart;
this.onLoadEnd = onLoadEnd;
this.onRefreshStart = onRefreshStart;
this.onRefreshEnd = onRefreshEnd;
this.onException = onException;
}
@Override
public final void storeStart(RecordContext ctx) {
if (onStoreStart != null)
onStoreStart.accept(ctx);
}
@Override
public final void storeEnd(RecordContext ctx) {
if (onStoreEnd != null)
onStoreEnd.accept(ctx);
}
@Override
public final void insertStart(RecordContext ctx) {
if (onInsertStart != null)
onInsertStart.accept(ctx);
}
@Override
public final void insertEnd(RecordContext ctx) {
if (onInsertEnd != null)
onInsertEnd.accept(ctx);
}
@Override
public final void updateStart(RecordContext ctx) {
if (onUpdateStart != null)
onUpdateStart.accept(ctx);
}
@Override
public final void updateEnd(RecordContext ctx) {
if (onUpdateEnd != null)
onUpdateEnd.accept(ctx);
}
@Override
public final void mergeStart(RecordContext ctx) {
if (onMergeStart != null)
onMergeStart.accept(ctx);
}
@Override
public final void mergeEnd(RecordContext ctx) {
if (onMergeEnd != null)
onMergeEnd.accept(ctx);
}
@Override
public final void deleteStart(RecordContext ctx) {
if (onDeleteStart != null)
onDeleteStart.accept(ctx);
}
@Override
public final void deleteEnd(RecordContext ctx) {
if (onDeleteEnd != null)
onDeleteEnd.accept(ctx);
}
@Override
public final void loadStart(RecordContext ctx) {
if (onLoadStart != null)
onLoadStart.accept(ctx);
}
@Override
public final void loadEnd(RecordContext ctx) {
if (onLoadEnd != null)
onLoadEnd.accept(ctx);
}
@Override
public final void refreshStart(RecordContext ctx) {
if (onRefreshStart != null)
onRefreshStart.accept(ctx);
}
@Override
public final void refreshEnd(RecordContext ctx) {
if (onRefreshEnd != null)
onRefreshEnd.accept(ctx);
}
@Override
public final void exception(RecordContext ctx) {
if (onException != null)
onException.accept(ctx);
}
public final CallbackRecordListener onStoreStart(Consumer<? super RecordContext> newOnStoreStart) {
return new CallbackRecordListener(
newOnStoreStart,
onStoreEnd,
onInsertStart,
onInsertEnd,
onUpdateStart,
onUpdateEnd,
onMergeStart,
onMergeEnd,
onDeleteStart,
onDeleteEnd,
onLoadStart,
onLoadEnd,
onRefreshStart,
onRefreshEnd,
onException
);
}
public final CallbackRecordListener onStoreEnd(Consumer<? super RecordContext> newOnStoreEnd) {
return new CallbackRecordListener(
onStoreStart,
newOnStoreEnd,
onInsertStart,
onInsertEnd,
onUpdateStart,
onUpdateEnd,
onMergeStart,
onMergeEnd,
onDeleteStart,
onDeleteEnd,
onLoadStart,
onLoadEnd,
onRefreshStart,
onRefreshEnd,
onException
);
}
public final CallbackRecordListener onInsertStart(Consumer<? super RecordContext> newOnInsertStart) {
return new CallbackRecordListener(
onStoreStart,
onStoreEnd,
newOnInsertStart,
onInsertEnd,
onUpdateStart,
onUpdateEnd,
onMergeStart,
onMergeEnd,
onDeleteStart,
onDeleteEnd,
onLoadStart,
onLoadEnd,
onRefreshStart,
onRefreshEnd,
onException
);
}
public final CallbackRecordListener onInsertEnd(Consumer<? super RecordContext> newOnInsertEnd) {
return new CallbackRecordListener(
onStoreStart,
onStoreEnd,
onInsertStart,
newOnInsertEnd,
onUpdateStart,
onUpdateEnd,
onMergeStart,
onMergeEnd,
onDeleteStart,
onDeleteEnd,
onLoadStart,
onLoadEnd,
onRefreshStart,
onRefreshEnd,
onException
);
}
public final CallbackRecordListener onUpdateStart(Consumer<? super RecordContext> newOnUpdateStart) {
return new CallbackRecordListener(
onStoreStart,
onStoreEnd,
onInsertStart,
onInsertEnd,
newOnUpdateStart,
onUpdateEnd,
onMergeStart,
onMergeEnd,
onDeleteStart,
onDeleteEnd,
onLoadStart,
onLoadEnd,
onRefreshStart,
onRefreshEnd,
onException
);
}
public final CallbackRecordListener onUpdateEnd(Consumer<? super RecordContext> newOnUpdateEnd) {
return new CallbackRecordListener(
onStoreStart,
onStoreEnd,
onInsertStart,
onInsertEnd,
onUpdateStart,
newOnUpdateEnd,
onMergeStart,
onMergeEnd,
onDeleteStart,
onDeleteEnd,
onLoadStart,
onLoadEnd,
onRefreshStart,
onRefreshEnd,
onException
);
}
public final CallbackRecordListener onMergeStart(Consumer<? super RecordContext> newOnMergeStart) {
return new CallbackRecordListener(
onStoreStart,
onStoreEnd,
onInsertStart,
onInsertEnd,
onUpdateStart,
onUpdateEnd,
newOnMergeStart,
onMergeEnd,
onDeleteStart,
onDeleteEnd,
onLoadStart,
onLoadEnd,
onRefreshStart,
onRefreshEnd,
onException
);
}
public final CallbackRecordListener onMergeEnd(Consumer<? super RecordContext> newOnMergeEnd) {
return new CallbackRecordListener(
onStoreStart,
onStoreEnd,
onInsertStart,
onInsertEnd,
onUpdateStart,
onUpdateEnd,
onMergeStart,
newOnMergeEnd,
onDeleteStart,
onDeleteEnd,
onLoadStart,
onLoadEnd,
onRefreshStart,
onRefreshEnd,
onException
);
}
public final CallbackRecordListener onDeleteStart(Consumer<? super RecordContext> newOnDeleteStart) {
return new CallbackRecordListener(
onStoreStart,
onStoreEnd,
onInsertStart,
onInsertEnd,
onUpdateStart,
onUpdateEnd,
onMergeStart,
onMergeEnd,
newOnDeleteStart,
onDeleteEnd,
onLoadStart,
onLoadEnd,
onRefreshStart,
onRefreshEnd,
onException
);
}
public final CallbackRecordListener onDeleteEnd(Consumer<? super RecordContext> newOnDeleteEnd) {
return new CallbackRecordListener(
onStoreStart,
onStoreEnd,
onInsertStart,
onInsertEnd,
onUpdateStart,
onUpdateEnd,
onMergeStart,
onMergeEnd,
onDeleteStart,
newOnDeleteEnd,
onLoadStart,
onLoadEnd,
onRefreshStart,
onRefreshEnd,
onException
);
}
public final CallbackRecordListener onLoadStart(Consumer<? super RecordContext> newOnLoadStart) {
return new CallbackRecordListener(
onStoreStart,
onStoreEnd,
onInsertStart,
onInsertEnd,
onUpdateStart,
onUpdateEnd,
onMergeStart,
onMergeEnd,
onDeleteStart,
onDeleteEnd,
newOnLoadStart,
onLoadEnd,
onRefreshStart,
onRefreshEnd,
onException
);
}
public final CallbackRecordListener onLoadEnd(Consumer<? super RecordContext> newOnLoadEnd) {
return new CallbackRecordListener(
onStoreStart,
onStoreEnd,
onInsertStart,
onInsertEnd,
onUpdateStart,
onUpdateEnd,
onMergeStart,
onMergeEnd,
onDeleteStart,
onDeleteEnd,
onLoadStart,
newOnLoadEnd,
onRefreshStart,
onRefreshEnd,
onException
);
}
public final CallbackRecordListener onRefreshStart(Consumer<? super RecordContext> newOnRefreshStart) {
return new CallbackRecordListener(
onStoreStart,
onStoreEnd,
onInsertStart,
onInsertEnd,
onUpdateStart,
onUpdateEnd,
onMergeStart,
onMergeEnd,
onDeleteStart,
onDeleteEnd,
onLoadStart,
onLoadEnd,
newOnRefreshStart,
onRefreshEnd,
onException
);
}
public final CallbackRecordListener onRefreshEnd(Consumer<? super RecordContext> newOnRefreshEnd) {
return new CallbackRecordListener(
onStoreStart,
onStoreEnd,
onInsertStart,
onInsertEnd,
onUpdateStart,
onUpdateEnd,
onMergeStart,
onMergeEnd,
onDeleteStart,
onDeleteEnd,
onLoadStart,
onLoadEnd,
onRefreshStart,
newOnRefreshEnd,
onException
);
}
public final CallbackRecordListener onException(Consumer<? super RecordContext> newOnException) {
return new CallbackRecordListener(
onStoreStart,
onStoreEnd,
onInsertStart,
onInsertEnd,
onUpdateStart,
onUpdateEnd,
onMergeStart,
onMergeEnd,
onDeleteStart,
onDeleteEnd,
onLoadStart,
onLoadEnd,
onRefreshStart,
onRefreshEnd,
newOnException
);
}
}

View File

@ -0,0 +1,195 @@
/*
* 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.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import java.util.function.Consumer;
import org.jooq.RecordContext;
import org.jooq.RecordListener;
import org.jooq.TransactionContext;
import org.jooq.TransactionListener;
/**
* A {@link TransactionListener} that allows for functional composition.
* <p>
* For example: <code><pre>
* TransactionListener listener = TransactionListener
* .onCommitStart(ctx -&gt; something())
* .onCommitEnd(ctx -&gt; something());
* </pre></code>
*
* @author Lukas Eder
*/
public final class CallbackTransactionListener implements TransactionListener {
/**
* Generated UID
*/
private static final long serialVersionUID = -4135358887698253754L;
private final Consumer<? super TransactionContext> onBeginStart;
private final Consumer<? super TransactionContext> onBeginEnd;
private final Consumer<? super TransactionContext> onCommitStart;
private final Consumer<? super TransactionContext> onCommitEnd;
private final Consumer<? super TransactionContext> onRollbackStart;
private final Consumer<? super TransactionContext> onRollbackEnd;
public CallbackTransactionListener() {
this(null, null, null, null, null, null);
}
private CallbackTransactionListener(
Consumer<? super TransactionContext> onBeginStart,
Consumer<? super TransactionContext> onBeginEnd,
Consumer<? super TransactionContext> onCommitStart,
Consumer<? super TransactionContext> onCommitEnd,
Consumer<? super TransactionContext> onRollbackStart,
Consumer<? super TransactionContext> onRollbackEnd
) {
this.onBeginStart = onBeginStart;
this.onBeginEnd = onBeginEnd;
this.onCommitStart = onCommitStart;
this.onCommitEnd = onCommitEnd;
this.onRollbackStart = onRollbackStart;
this.onRollbackEnd = onRollbackEnd;
}
@Override
public final void beginStart(TransactionContext ctx) {
if (onBeginStart != null)
onBeginStart.accept(ctx);
}
@Override
public final void beginEnd(TransactionContext ctx) {
if (onBeginEnd != null)
onBeginEnd.accept(ctx);
}
@Override
public final void commitStart(TransactionContext ctx) {
if (onCommitStart != null)
onCommitStart.accept(ctx);
}
@Override
public final void commitEnd(TransactionContext ctx) {
if (onCommitEnd != null)
onCommitEnd.accept(ctx);
}
@Override
public final void rollbackStart(TransactionContext ctx) {
if (onRollbackStart != null)
onRollbackStart.accept(ctx);
}
@Override
public final void rollbackEnd(TransactionContext ctx) {
if (onRollbackEnd != null)
onRollbackEnd.accept(ctx);
}
public final CallbackTransactionListener onBeginStart(Consumer<? super TransactionContext> newOnBeginStart) {
return new CallbackTransactionListener(
newOnBeginStart,
onBeginEnd,
onCommitStart,
onCommitEnd,
onRollbackStart,
onRollbackEnd
);
}
public final CallbackTransactionListener onBeginEnd(Consumer<? super TransactionContext> newOnBeginEnd) {
return new CallbackTransactionListener(
onBeginStart,
newOnBeginEnd,
onCommitStart,
onCommitEnd,
onRollbackStart,
onRollbackEnd
);
}
public final CallbackTransactionListener onCommitStart(Consumer<? super TransactionContext> newOnCommitStart) {
return new CallbackTransactionListener(
onBeginStart,
onBeginEnd,
newOnCommitStart,
onCommitEnd,
onRollbackStart,
onRollbackEnd);
}
public final CallbackTransactionListener onCommitEnd(Consumer<? super TransactionContext> newOnCommitEnd) {
return new CallbackTransactionListener(
onBeginStart,
onBeginEnd,
onCommitStart,
newOnCommitEnd,
onRollbackStart,
onRollbackEnd
);
}
public final CallbackTransactionListener onRollbackStart(Consumer<? super TransactionContext> newOnRollbackStart) {
return new CallbackTransactionListener(
onBeginStart,
onBeginEnd,
onCommitStart,
onCommitEnd,
newOnRollbackStart,
onRollbackEnd
);
}
public final CallbackTransactionListener onRollbackEnd(Consumer<? super TransactionContext> newOnRollbackEnd) {
return new CallbackTransactionListener(
onBeginStart,
onBeginEnd,
onCommitStart,
onCommitEnd,
onRollbackStart,
newOnRollbackEnd
);
}
}

View File

@ -50,6 +50,11 @@ import org.jooq.TransactionListener;
*/
public class DefaultTransactionListener implements TransactionListener {
/**
* Generated UID
*/
private static final long serialVersionUID = -1836373111252221549L;
@Override
public void beginStart(TransactionContext ctx) {}