blob: f9c6685a95e67cf440d2dada0174d3a392940548 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- NewPage -->
<html lang="en">
<head>
<title>Asynchronous</title>
<link rel="stylesheet" type="text/css" href="../../../../stylesheet.css" title="Style">
<script type="text/javascript" src="../../../../script.js"></script>
<link rel="shortcut icon" href="/img/microprofile-favicon.png">
</head>
<body>
<script type="text/javascript"><!--
try {
if (location.href.indexOf('is-external=true') == -1) {
parent.document.title="Asynchronous";
}
}
catch(err) {
}
//-->
</script>
<noscript>
<div>JavaScript is disabled on your browser.</div>
</noscript>
<!-- ========= START OF TOP NAVBAR ======= -->
<div class="topNav"><a name="navbar.top">
<!-- -->
</a>
<div class="skipNav"><a href="#skip.navbar.top" title="Skip navigation links">Skip navigation links</a></div>
<a name="navbar.top.firstrow">
<!-- -->
</a>
<ul class="navList" title="Navigation">
<li><a href="../../../../overview-summary.html">Overview</a></li>
<li><a href="package-summary.html">Package</a></li>
<li class="navBarCell1Rev">Class</li>
<li><a href="package-tree.html">Tree</a></li>
<li><a href="../../../../deprecated-list.html">Deprecated</a></li>
<li><a href="../../../../index-all.html">Index</a></li>
<li><a href="../../../../help-doc.html">Help</a></li>
</ul>
</div>
<div class="subNav">
<ul class="navList">
<li>Prev&nbsp;Class</li>
<li><a href="../../../../org/eclipse/microprofile/faulttolerance/Bulkhead.html" title="annotation in org.eclipse.microprofile.faulttolerance"><span class="typeNameLink">Next&nbsp;Class</span></a></li>
</ul>
<ul class="navList">
<li><a href="../../../../index.html?org/eclipse/microprofile/faulttolerance/Asynchronous.html" target="_top">Frames</a></li>
<li><a href="Asynchronous.html" target="_top">No&nbsp;Frames</a></li>
</ul>
<ul class="navList" id="allclasses_navbar_top">
<li><a href="../../../../allclasses-noframe.html">All&nbsp;Classes</a></li>
</ul>
<div>
<script type="text/javascript"><!--
allClassesLink = document.getElementById("allclasses_navbar_top");
if(window==top) {
allClassesLink.style.display = "block";
}
else {
allClassesLink.style.display = "none";
}
//-->
</script>
</div>
<div>
<ul class="subNavList">
<li>Summary:&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li>Required&nbsp;|&nbsp;</li>
<li>Optional</li>
</ul>
<ul class="subNavList">
<li>Detail:&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li>Element</li>
</ul>
</div>
<a name="skip.navbar.top">
<!-- -->
</a></div>
<!-- ========= END OF TOP NAVBAR ========= -->
<!-- ======== START OF CLASS DATA ======== -->
<div class="header">
<div class="subTitle">org.eclipse.microprofile.faulttolerance</div>
<h2 title="Annotation Type Asynchronous" class="title">Annotation Type Asynchronous</h2>
</div>
<div class="contentContainer">
<div class="description">
<ul class="blockList">
<li class="blockList">
<hr>
<br>
<pre>@Documented
@Retention(value=RUNTIME)
@Target(value={METHOD,TYPE})
@Inherited
public @interface <span class="memberNameLabel">Asynchronous</span></pre>
<div class="block">Wrap the execution and invoke it asynchronously. The context for <code>jakarta.enterprise.context.RequestScoped</code>
must be active during the method invocation, which means the method with the <code>@Asynchronous</code> annotation is
allowed to use the beans with <code>jakarta.enterprise.context.RequestScoped</code>. Any methods marked with this
annotation must return one of:
<ul>
<li><code>Future</code></li>
<li><code>CompletionStage</code></li>
</ul>
Otherwise, <a href="../../../../org/eclipse/microprofile/faulttolerance/exceptions/FaultToleranceDefinitionException.html" title="class in org.eclipse.microprofile.faulttolerance.exceptions"><code>FaultToleranceDefinitionException</code></a> occurs during
deployment. The return type <code>CompletionStage</code> is preferred over
<code>Future</code> as a <code>Future</code> that completes exceptionally will not
trigger other Fault Tolerance operations even if specified (e.g. Retry), while a
<code>CompletionStage</code> that completes exceptionally will trigger other Fault Tolerance
capabilities if specified (e.g. Retry).
<p>
When a method marked with this annotation is called from one thread (which we will call Thread A), the method call is
intercepted, and execution of the method is submitted to run asynchronously on another thread (which we will call
Thread B).
<p>
On Thread A, a Future or CompletionStage is returned immediately and can be used to get the result of the execution
taking place on Thread B, once it is complete.
<p>
Before the execution on Thread B completes, the Future or CompletionStage returned in Thread A will report itself as
incomplete. At this point, <code>Future.cancel(boolean)</code> can be used to abort the execution.
<p>
Once the execution on Thread B is complete, the Future or CompletionStage returned in Thread A behaves differently
depending on whether the execution in Thread B threw an exception:
<ul>
<li>If the execution threw an exception, the Future or CompletionStage will be completed with that exception.</li>
<li>If the execution returned normally, the Future or CompletionStage returned in Thread A will behave in the same
way as the Future or CompletionStage returned from the execution in Thread B, i.e. it can be:
<ul>
<li>not complete yet</li>
<li>completed successfully with a return value</li>
<li>completed exceptionally</li>
</ul>
<p>
At this point, any calls to the Future or CompletionStage returned in Thread A will be delegated to the Future or
CompletionStage returned from the execution in Thread B.</li>
</ul>
<p>
The call made on Thread A will never throw an exception, even if the method declares that it throws checked
exceptions, because the execution is going to occur on Thread B and hasn't happened yet. To avoid unnecessary
<code>try..catch</code> blocks around these method calls, it's recommended that methods annotated with
<code>@Asynchronous</code> do not declare that they throw checked exceptions.
<p>
Any exception thrown from the execution on Thread B, or raised by another Fault Tolerance component such as
<a href="../../../../org/eclipse/microprofile/faulttolerance/Bulkhead.html" title="annotation in org.eclipse.microprofile.faulttolerance"><code>Bulkhead</code></a> or <a href="../../../../org/eclipse/microprofile/faulttolerance/CircuitBreaker.html" title="annotation in org.eclipse.microprofile.faulttolerance"><code>CircuitBreaker</code></a>, can be retrieved in the following ways:
<ul>
<li>If the method declares <code>Future</code> as the return type, calling
<code>Future.get()</code> on the Future returned in Thread A will throw an
<code>ExecutionException</code> wrapping the original exception.</li>
<li>If the method declares <code>CompletionStage</code> as the return type, the CompletionStage
returned in Thread A is completed exceptionally with the exception.</li>
</ul>
<p>
If a class is annotated with this annotation, all class methods are treated as if they were marked with this
annotation. If one of the methods doesn't return either Future or CompletionStage,
<a href="../../../../org/eclipse/microprofile/faulttolerance/exceptions/FaultToleranceDefinitionException.html" title="class in org.eclipse.microprofile.faulttolerance.exceptions"><code>FaultToleranceDefinitionException</code></a> occurs (at deploy time
if the bean is discovered during deployment).
</p>
<p>
Example usage:
</p>
<pre>
<code>@Asynchronous
public CompletionStage&lt;String&gt; getString() {
return CompletableFuture.completedFuture("hello");
}</code>
</pre>
<p>
Example call with exception handling:
</p>
<pre>
<code>CompletionStage stage = getString().exceptionally(e -&gt; {
handleException(e);
return null;
});</code>
</pre></div>
</li>
</ul>
</div>
</div>
<!-- ========= END OF CLASS DATA ========= -->
<!-- ======= START OF BOTTOM NAVBAR ====== -->
<div class="bottomNav"><a name="navbar.bottom">
<!-- -->
</a>
<div class="skipNav"><a href="#skip.navbar.bottom" title="Skip navigation links">Skip navigation links</a></div>
<a name="navbar.bottom.firstrow">
<!-- -->
</a>
<ul class="navList" title="Navigation">
<li><a href="../../../../overview-summary.html">Overview</a></li>
<li><a href="package-summary.html">Package</a></li>
<li class="navBarCell1Rev">Class</li>
<li><a href="package-tree.html">Tree</a></li>
<li><a href="../../../../deprecated-list.html">Deprecated</a></li>
<li><a href="../../../../index-all.html">Index</a></li>
<li><a href="../../../../help-doc.html">Help</a></li>
</ul>
</div>
<div class="subNav">
<ul class="navList">
<li>Prev&nbsp;Class</li>
<li><a href="../../../../org/eclipse/microprofile/faulttolerance/Bulkhead.html" title="annotation in org.eclipse.microprofile.faulttolerance"><span class="typeNameLink">Next&nbsp;Class</span></a></li>
</ul>
<ul class="navList">
<li><a href="../../../../index.html?org/eclipse/microprofile/faulttolerance/Asynchronous.html" target="_top">Frames</a></li>
<li><a href="Asynchronous.html" target="_top">No&nbsp;Frames</a></li>
</ul>
<ul class="navList" id="allclasses_navbar_bottom">
<li><a href="../../../../allclasses-noframe.html">All&nbsp;Classes</a></li>
</ul>
<div>
<script type="text/javascript"><!--
allClassesLink = document.getElementById("allclasses_navbar_bottom");
if(window==top) {
allClassesLink.style.display = "block";
}
else {
allClassesLink.style.display = "none";
}
//-->
</script>
</div>
<div>
<ul class="subNavList">
<li>Summary:&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li>Required&nbsp;|&nbsp;</li>
<li>Optional</li>
</ul>
<ul class="subNavList">
<li>Detail:&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li>Element</li>
</ul>
</div>
<a name="skip.navbar.bottom">
<!-- -->
</a></div>
<!-- ======== END OF BOTTOM NAVBAR ======= -->
</body>
</html>