use of co.cask.cdap.api.annotation.TransactionControl in project cdap by caskdata.
the class BodyConsumerAdapter method finished.
@Override
public void finished(HttpResponder responder) {
TransactionControl txCtrl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, HttpContentConsumer.class, delegate, "onFinish", HttpServiceResponder.class);
try {
if (TransactionControl.IMPLICIT == txCtrl) {
transactional.execute(new TxRunnable() {
@Override
public void run(DatasetContext context) throws Exception {
delegate.onFinish(BodyConsumerAdapter.this.responder);
}
});
} else {
delegate.onFinish(BodyConsumerAdapter.this.responder);
}
} catch (Throwable t) {
onError(t, this.responder);
return;
}
// To the HttpContentConsumer, the call is completed even if it fails to send response back to client.
completed = true;
try {
BodyConsumerAdapter.this.responder.execute();
} finally {
if (!this.responder.hasContentProducer()) {
contextReleaser.cancel();
}
}
}
use of co.cask.cdap.api.annotation.TransactionControl in project cdap by caskdata.
the class BodyProducerAdapter method finished.
@Override
public void finished() throws Exception {
TransactionControl txCtrl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, HttpContentProducer.class, delegate, "onFinish");
if (TransactionControl.IMPLICIT == txCtrl) {
serviceContext.execute(new TxRunnable() {
@Override
public void run(DatasetContext context) throws Exception {
delegate.onFinish();
}
});
} else {
delegate.onFinish();
}
try {
serviceContext.dismissTransactionContext();
} finally {
completed = true;
contextReleaser.cancel();
}
}
use of co.cask.cdap.api.annotation.TransactionControl in project cdap by caskdata.
the class BodyProducerAdapter method handleError.
@Override
public void handleError(final Throwable throwable) {
if (completed) {
return;
}
// To the HttpContentProducer, if there is error, no other methods will be triggered
completed = true;
TransactionControl txCtrl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, HttpContentProducer.class, delegate, "onError", Throwable.class);
try {
if (TransactionControl.IMPLICIT == txCtrl) {
serviceContext.execute(new TxRunnable() {
@Override
public void run(DatasetContext context) throws Exception {
delegate.onError(throwable);
}
});
} else {
delegate.onError(throwable);
}
} catch (Throwable t) {
throwable.addSuppressed(t);
// nothing much can be done. Simply emit a debug log.
LOG.warn("Exception in calling HttpContentProducer.onError.", t);
}
try {
serviceContext.dismissTransactionContext();
} finally {
contextReleaser.cancel();
}
}
use of co.cask.cdap.api.annotation.TransactionControl in project cdap by caskdata.
the class SparkRuntimeService method destroy.
/**
* Calls the destroy or onFinish method of {@link ProgramLifecycle}.
*/
private void destroy(final ProgramState state) throws Exception {
final TransactionControl txControl = spark instanceof ProgramLifecycle ? Transactions.getTransactionControl(TransactionControl.IMPLICIT, Spark.class, spark, "destroy") : TransactionControl.IMPLICIT;
TxRunnable runnable = new TxRunnable() {
@Override
public void run(DatasetContext ctxt) throws Exception {
Cancellable cancellable = SparkRuntimeUtils.setContextClassLoader(new SparkClassLoader(runtimeContext));
try {
context.setState(state);
if (spark instanceof ProgramLifecycle) {
((ProgramLifecycle) spark).destroy();
} else {
spark.onFinish(state.getStatus() == ProgramStatus.COMPLETED, context);
}
} finally {
cancellable.cancel();
}
}
};
if (TransactionControl.IMPLICIT == txControl) {
context.execute(runnable);
} else {
runnable.run(context);
}
}
Aggregations