org.voltdb.client.SyncCallback Maven / Gradle / Ivy
Show all versions of voltdbclient Show documentation
/* This file is part of VoltDB.
* Copyright (C) 2008-2017 VoltDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with VoltDB. If not, see .
*/
package org.voltdb.client;
import java.util.concurrent.Semaphore;
/**
* A utility class that allows a client to queue a stored procedure invocation asynchronously and then poll
* or join on the response. Useful when invoking multiple stored procedures synchronously
* from a single thread. Queue each of the invocations asynchronously with a different SyncCallback
and
* then call {@link #waitForResponse} on each of the SyncCallback
s to join on the responses.
*
*/
public final class SyncCallback extends AbstractProcedureArgumentCacher implements ProcedureCallback {
private final Semaphore m_lock;
private ClientResponse m_response;
/**
* Create a SyncCallback instance.
*/
public SyncCallback() {
m_response = null;
m_lock = new Semaphore(1);
m_lock.acquireUninterruptibly();
}
@Override
public void clientCallback(ClientResponse clientResponse) {
m_response = clientResponse;
m_lock.release();
}
/**
* Non-blocking poll method that checks for the response to the invocation associated with this callback.
* Call getResponse to retrieve the response or result() to retrieve the just the results.
*
* @return True if the response is available, false otherwise
*/
public boolean checkForResponse() {
return m_lock.tryAcquire();
}
/**
* Retrieve the ClientResponse returned for this procedure invocation.
*
* @return ClientResponse for this invocation
*/
public ClientResponse getResponse() {
return m_response;
}
/**
* Block until a response has been received for the invocation associated with this callback. Call getResponse
* to retrieve the response or result() to retrieve the just the results.
*
* @throws InterruptedException on interruption.
*/
public void waitForResponse() throws InterruptedException {
m_lock.acquire();
m_lock.release();
}
/**
* Return the arguments provided with the procedure invocation.
* @return Object array containing procedure arguments
*/
@Override
public Object[] args() {
return super.args();
}
}