List of usage examples for org.apache.thrift.async AsyncMethodCallback onComplete
void onComplete(T response);
From source file:com.kurento.kmf.thrift.pool.AsyncClientWithValidation.java
License:Open Source License
@Override public void invokeJsonRpc(String request, final AsyncMethodCallback<invokeJsonRpc_call> resultHandler) throws TException { // FIXME: Implement retries super.invokeJsonRpc(request, new AsyncMethodCallback<KmsMediaServerService.AsyncClient.invokeJsonRpc_call>() { @Override/*w ww . ja va2s . c o m*/ public void onComplete(KmsMediaServerService.AsyncClient.invokeJsonRpc_call response) { resultHandler.onComplete(response); } @Override public void onError(Exception exception) { resultHandler.onError(exception); } }); }
From source file:com.linecorp.armeria.client.thrift.ThriftClientCodec.java
License:Apache License
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override//w w w. ja v a2 s .c om
public <T> void prepareRequest(Method method, Object[] args, Promise<T> resultPromise) {
requireNonNull(method, "method");
requireNonNull(resultPromise, "resultPromise");
final ThriftMethod thriftMethod = methodMap.get(method.getName());
if (thriftMethod == null) {
throw new IllegalStateException("Thrift method not found: " + method.getName());
}
if (isAsyncClient) {
AsyncMethodCallback callback = ThriftMethod.asyncCallback(args);
if (callback != null) {
resultPromise.addListener(future -> {
if (future.isSuccess()) {
callback.onComplete(future.getNow());
} else {
Exception decodedException = decodeException(future.cause(),
thriftMethod.declaredThrowableException());
callback.onError(decodedException);
}
});
}
}
}
From source file:com.linecorp.armeria.client.thrift.ThriftClientInvocationHandler.java
License:Apache License
private Object invokeClientMethod(Method method, Object[] args) throws Throwable { final AsyncMethodCallback<Object> callback; if (args == null) { args = NO_ARGS;//from w w w .j av a 2s . c om callback = null; } else { final int lastIdx = args.length - 1; if (args.length > 0 && args[lastIdx] instanceof AsyncMethodCallback) { @SuppressWarnings("unchecked") final AsyncMethodCallback<Object> lastArg = (AsyncMethodCallback<Object>) args[lastIdx]; callback = lastArg; args = Arrays.copyOfRange(args, 0, lastIdx); } else { callback = null; } } try { final ThriftReply reply = thriftClient.execute(path, clientType, method.getName(), args); if (callback != null) { reply.handle(voidFunction((result, cause) -> { if (cause == null) { callback.onComplete(result); } else { invokeOnError(callback, cause); } })).exceptionally(CompletionActions::log); return null; } else { try { return reply.get(); } catch (ExecutionException e) { throw e.getCause(); } } } catch (Throwable cause) { if (callback != null) { invokeOnError(callback, cause); return null; } else { throw cause; } } }
From source file:com.linecorp.armeria.client.thrift.THttpClientInvocationHandler.java
License:Apache License
private Object invokeClientMethod(Method method, Object[] args) throws Throwable { final AsyncMethodCallback<Object> callback; if (args == null) { args = NO_ARGS;//from www . j a v a2 s . c om callback = null; } else { final int lastIdx = args.length - 1; if (args.length > 0 && args[lastIdx] instanceof AsyncMethodCallback) { @SuppressWarnings("unchecked") final AsyncMethodCallback<Object> lastArg = (AsyncMethodCallback<Object>) args[lastIdx]; callback = lastArg; args = Arrays.copyOfRange(args, 0, lastIdx); } else { callback = null; } } try { final RpcResponse reply = thriftClient.executeMultiplexed(path, params.clientType(), fragment, method.getName(), args); if (callback != null) { reply.handle(voidFunction((result, cause) -> { if (cause == null) { callback.onComplete(result); } else { invokeOnError(callback, cause); } })).exceptionally(CompletionActions::log); return null; } else { try { return reply.get(); } catch (ExecutionException e) { throw e.getCause(); } } } catch (Throwable cause) { if (callback != null) { invokeOnError(callback, cause); return null; } else { throw cause; } } }
From source file:com.vmware.photon.controller.deployer.dcp.mock.AgentControlClientMock.java
License:Open Source License
@Override public void provision(List<String> datastores, Set<String> imageDatastoreNames, boolean usedForVMs, List<String> networks, String hostAddress, int hostPort, double memoryOvercommit, String loggingEndpoint, String logLevel, StatsPluginConfig statsPluginConfig, boolean managementOnly, String ntpEndpoint, String hostId, String deploymentId, AsyncMethodCallback<AgentControl.AsyncClient.provision_call> handler) { logger.info("Host provision complete invocation"); if (null != provisionFailure) { handler.onError(provisionFailure); } else if (null != provisionResultCode) { AgentControl.AsyncClient.provision_call provisionCall = mock( AgentControl.AsyncClient.provision_call.class); ProvisionResponse provisionResponse = new ProvisionResponse(); provisionResponse.setResult(provisionResultCode); try {/* w w w . ja v a 2s .com*/ when(provisionCall.getResult()).thenReturn(provisionResponse); } catch (TException e) { throw new RuntimeException("Failed to mock provisionCall.getResult"); } handler.onComplete(provisionCall); } else { throw new IllegalStateException("No result or failure specified for provision"); } }
From source file:com.vmware.photon.controller.deployer.dcp.mock.AgentControlClientMock.java
License:Open Source License
@Override public void upgrade(AsyncMethodCallback<AgentControl.AsyncClient.upgrade_call> handler) { logger.info("Host upgrade complete invocation"); if (null != upgradeFailure) { handler.onError(upgradeFailure); } else if (null != upgradeResultCode) { AgentControl.AsyncClient.upgrade_call upgradeCall = mock(AgentControl.AsyncClient.upgrade_call.class); UpgradeResponse upgradeResponse = new UpgradeResponse(); upgradeResponse.setResult(upgradeResultCode); try {/*w w w . ja va2s .c o m*/ when(upgradeCall.getResult()).thenReturn(upgradeResponse); } catch (TException e) { throw new RuntimeException("Failed to mock upgradeCall.getResult"); } handler.onComplete(upgradeCall); } else { throw new IllegalStateException("No result or failure specified for upgrade"); } }
From source file:com.vmware.photon.controller.deployer.dcp.mock.AgentControlClientMock.java
License:Open Source License
@Override public void getAgentStatus(AsyncMethodCallback<AgentControl.AsyncClient.get_agent_status_call> handler) { logger.info("Agent get status complete invocation"); if (null != getAgentStatusFailure) { handler.onError(getAgentStatusFailure); } else if (null != agentStatusCode) { AgentControl.AsyncClient.get_agent_status_call getAgentStatusCall = mock( AgentControl.AsyncClient.get_agent_status_call.class); AgentStatusResponse agentStatusResponse = new AgentStatusResponse(); agentStatusResponse.setStatus(agentStatusCode); agentStatusResponse.setStatusIsSet(true); try {//ww w . j a va 2 s. c o m when(getAgentStatusCall.getResult()).thenReturn(agentStatusResponse); } catch (TException e) { throw new RuntimeException("Failed to mock getAgentStatusCall.getResult"); } handler.onComplete(getAgentStatusCall); } else { throw new IllegalStateException("No result or failure specified for getAgentStatus"); } }
From source file:com.vmware.photon.controller.deployer.dcp.mock.HostClientMock.java
License:Open Source License
@Override public void getHostConfig(AsyncMethodCallback<Host.AsyncClient.get_host_config_call> handler) { logger.info("Host get config complete invocation"); if (null != getConfigFailure) { handler.onError(getConfigFailure); } else if (null != getConfigResultCode) { Host.AsyncClient.get_host_config_call getHostConfigCall = mock( Host.AsyncClient.get_host_config_call.class); GetConfigResponse getConfigResponse = new GetConfigResponse(); getConfigResponse.setResult(getConfigResultCode); getConfigResponse.setHostConfig(hostConfig); try {//ww w . ja va2 s .c o m when(getHostConfigCall.getResult()).thenReturn(getConfigResponse); } catch (TException e) { throw new RuntimeException("Failed to mock getHostConfigCall.getResult"); } handler.onComplete(getHostConfigCall); } else { throw new IllegalStateException("No result or failure specified for getHostConfig"); } }
From source file:com.vmware.photon.controller.deployer.dcp.mock.HostClientMock.java
License:Open Source License
@Override public void setHostMode(HostMode hostMode, AsyncMethodCallback<Host.AsyncClient.set_host_mode_call> callback) { if (this.setHostModeFailure != null) { callback.onError(this.setHostModeFailure); } else if (this.setHostModeResultCode != null) { Host.AsyncClient.set_host_mode_call call = mock(Host.AsyncClient.set_host_mode_call.class); SetHostModeResponse response = new SetHostModeResponse(); response.setResult(this.setHostModeResultCode); try {//from w w w. ja va2 s.c o m when(call.getResult()).thenReturn(response); } catch (TException e) { throw new RuntimeException("Failed to mock setHostMode.getResult"); } callback.onComplete(call); } else { throw new IllegalStateException("No result or failure specified for setHostMode"); } }
From source file:com.vmware.photon.controller.deployer.helpers.xenon.MockHelper.java
License:Open Source License
@SuppressWarnings("unchecked") public static Answer<AsyncMethodCallback<AgentControl.AsyncClient.provision_call>> mockProvisionAgent( ProvisionResultCode resultCode) { return (invocation) -> { AsyncMethodCallback<AgentControl.AsyncClient.provision_call> callback = ((AsyncMethodCallback<AgentControl.AsyncClient.provision_call>) invocation .getArguments()[13]);//from w w w . j ava 2s . c o m AgentControl.AsyncClient.provision_call provisionCall = mock( AgentControl.AsyncClient.provision_call.class); ProvisionResponse provisionResponse = new ProvisionResponse(resultCode); doReturn(provisionResponse).when(provisionCall).getResult(); callback.onComplete(provisionCall); return null; }; }