Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@

import java.sql.SQLException;

/**
* Exclusive operation callback.
*
* @param <T> type of return value
*/
@FunctionalInterface
public interface ExclusiveOperationCallback {
public interface ExclusiveOperationCallback<T> {

/**
* Execute.
*
* @return execution result
* @throws SQLException SQL exception
*/
void execute() throws SQLException;
T execute() throws SQLException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.shardingsphere.mode.exclusive;

import java.sql.SQLException;

/**
* Exclusive operation void callback.
*/
@FunctionalInterface
public interface ExclusiveOperationVoidCallback {

/**
* Execute.
*
* @throws SQLException SQL exception
*/
void execute() throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,42 @@ public final class ExclusiveOperatorEngine {
private final ExclusiveOperatorContext exclusiveOperatorContext;

/**
* Operate with exclusive lock.
* Operate with exclusive operation and void callback.
*
* @param operation exclusive operation
* @param timeoutMillis timeout millis
* @param voidCallback void callback
* @throws SQLException SQL exception
*/
public void operate(final ExclusiveOperation operation, final long timeoutMillis, final ExclusiveOperationVoidCallback voidCallback) throws SQLException {
operateWithResult(operation, timeoutMillis, asVoidCallback(voidCallback));
}

private ExclusiveOperationCallback<Void> asVoidCallback(final ExclusiveOperationVoidCallback voidCallback) {
return () -> {
voidCallback.execute();
return null;
};
}

/**
* Operate with exclusive operation and return result.
*
* @param operation exclusive operation
* @param timeoutMillis timeout millis
* @param callback callback
* @param <T> type of return value
* @return execution result
* @throws SQLException SQL exception
*/
public void operate(final ExclusiveOperation operation, final long timeoutMillis, final ExclusiveOperationCallback callback) throws SQLException {
public <T> T operateWithResult(final ExclusiveOperation operation, final long timeoutMillis, final ExclusiveOperationCallback<T> callback) throws SQLException {
if (exclusiveOperatorContext.start(operation, timeoutMillis)) {
try {
callback.execute();
return callback.execute();
} finally {
exclusiveOperatorContext.stop(operation);
}
}
return null;
}
}