È possibile eseguire materiale tramite i metodi JdbcTemplate
che consentono di passare in un PreparedStatementCreator
. Si può sempre usare questo per intercettare le invocazioni (magari usando uno Proxy
) che ha causato un cancel
che si verifica su un thread separato da qualche cond
diventato true
.
public Results respondToUseRequest(Request req) {
final AtomicBoolean cond = new AtomicBoolean(false);
requestRegister.put(req, cond);
return jdbcTemplate.query(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection conn) {
PreparedStatement stmt = conn.prepareStatement();
return proxyPreparedStatement(stmt, cond);
}
},
new ResultSetExtractor() { ... });
}
Questo canceller
potrebbe essere annullato in caso di completamento con esito positivo; per esempio
private final static ScheduledExecutorService scheduler =
Executors.newSingleThreadedScheduledExecutor();
PreparedStatement proxyPreparedStatement(final PreparedStatement s, AtomicBoolean cond) {
//InvocationHandler delegates invocations to the underlying statement
//but intercepts a query
InvocationHandler h = new InvocationHandler() {
public Object invoke(Object proxy, Method m, Object[] args) {
if (m.getName().equals("executeQuery") {
Runnable cancel = new Runnable() {
public void run() {
try {
synchronized (cond) {
while (!cond.get()) cond.wait();
s.cancel();
}
} catch (InterruptedException e) { }
}
}
Future<?> f = scheduler.submit(cancel);
try {
return m.invoke(s, args);
} finally {
//cancel the canceller upon succesful completion
if (!f.isDone()) f.cancel(true); //will cause interrupt
}
}
else {
return m.invoke(s, args);
}
}
}
return (PreparedStatement) Proxy.newProxyInstance(
getClass().getClassLoader(),
new Class[]{PreparedStatement.class},
h);
Così ora il codice che risponde a cancellazione di un utente sarebbe simile:
cond.set(true);
synchronized (cond) { cond.notifyAll(); }
fonte
2009-06-28 12:17:49
Un esempio interessante e strano, non del tutto sicuro di come possa essere applicato al problema in questione. Ovviamente l'annullamento automatico di 10 secondi dovrebbe essere sostituito con qualcosa attivato esternamente. – skaffman
Perché dovrebbe essere attivato esternamente? L'OP non ha menzionato nulla riguardo ad esso, ad esempio, l'OP –
definito dall'utente: in realtà, desidero che l'annullamento venga attivato in risposta all'azione dell'utente. –