-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(RequestFuture): new request api RequestFuture.
- Loading branch information
1 parent
20d39dc
commit d7bf94b
Showing
10 changed files
with
179 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.opc.rpc.api; | ||
|
||
import io.opc.rpc.api.response.Response; | ||
import java.util.concurrent.Future; | ||
|
||
/** | ||
* Future for request, who async waiting a Response. | ||
* | ||
* @author caihongwen | ||
* @version Id: RequestFuture.java, v 0.1 2022年06月16日 14:05 caihongwen Exp $ | ||
*/ | ||
public interface RequestFuture<R extends Response> extends Future<R> { | ||
|
||
} |
This file contains 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
85 changes: 85 additions & 0 deletions
85
rpc-core/src/main/java/io/opc/rpc/core/RequestFutureTask.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.opc.rpc.core; | ||
|
||
import io.opc.rpc.api.RequestCallback; | ||
import io.opc.rpc.api.RequestFuture; | ||
import io.opc.rpc.api.exception.OpcRpcRuntimeException; | ||
import io.opc.rpc.api.response.ErrorResponse; | ||
import io.opc.rpc.api.response.Response; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.FutureTask; | ||
import java.util.concurrent.TimeUnit; | ||
import javax.annotation.Nonnull; | ||
import lombok.AllArgsConstructor; | ||
|
||
/** | ||
* RequestFutureTask. Default impl for RequestFuture. | ||
* | ||
* @author caihongwen | ||
* @version Id: RequestFutureTask.java, v 0.1 2022年06月16日 12:08 caihongwen Exp $ | ||
*/ | ||
public class RequestFutureTask<R extends Response> extends FutureTask<R> implements RequestFuture<R> { | ||
|
||
protected String connectionId; | ||
|
||
protected String requestId; | ||
|
||
@SuppressWarnings("rawtypes") | ||
private static final Callable EMPTY = () -> null; | ||
|
||
public RequestFutureTask(@Nonnull String connectionId, @Nonnull String requestId) { | ||
//noinspection unchecked | ||
super(EMPTY); | ||
this.connectionId = connectionId; | ||
this.requestId = requestId; | ||
} | ||
|
||
@Override | ||
public boolean cancel(boolean mayInterruptIfRunning) { | ||
final boolean cancel = super.cancel(mayInterruptIfRunning); | ||
RequestCallbackSupport.clearCallback(connectionId, requestId); | ||
return cancel; | ||
} | ||
|
||
/** | ||
* Only for Opc Inner. | ||
*/ | ||
public InvokeByRequestCallback<R> buildInvokeByRequestCallback(@Nonnull Executor executor) { | ||
return new InvokeByRequestCallback<>(executor, this); | ||
} | ||
|
||
@AllArgsConstructor | ||
static class InvokeByRequestCallback<R extends Response> implements RequestCallback<R> { | ||
|
||
private Executor executor; | ||
|
||
private RequestFutureTask<R> futureTask; | ||
|
||
@Override | ||
public Executor getExecutor() { | ||
return executor; | ||
} | ||
|
||
@Override | ||
public long getTimeout() { | ||
// means permanent | ||
return TimeUnit.HOURS.toMillis(23); | ||
} | ||
|
||
@Override | ||
public void onTimeout() { | ||
// Should not be triggered. | ||
} | ||
|
||
@Override | ||
public void onResponse(R response) { | ||
futureTask.set(response); | ||
} | ||
|
||
@Override | ||
public void onError(ErrorResponse errResp) { | ||
futureTask.setException(new OpcRpcRuntimeException(errResp.getResultCode(), errResp.getMessage())); | ||
} | ||
} | ||
|
||
} |
This file contains 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 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 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