= AsyncControl
:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Modifies the way in which an asynchronous action initiated through the xref:refguide:applib:index/services/wrapper/WrapperFactory.adoc[WrapperFactory] is actually executed.

Executing in a separate thread means that the target and arguments are used in a new _org.apache.causeway.applib.services.iactn.Interaction_ (and transaction). If any of these are entities, they are retrieved from the database afresh; it isn't possible to pass domain entity references from the foreground calling thread to the background threads.

== API

[source,java]
.AsyncControl.java
----
class AsyncControl<R> {
  AsyncControl<Void> returningVoid()     // <.>
  AsyncControl<X> returning(Class<X> cls)     // <.>
  AsyncControl<R> withSkipRules()     // <.>
  AsyncControl<R> with(ExceptionHandler exceptionHandler)     // <.>
  AsyncControl<R> with(ExecutorService executorService)     // <.>
  AsyncControl<R> withClock(VirtualClock clock)
  AsyncControl<R> withLocale(Locale locale)
  AsyncControl<R> withTimeZone(ZoneId timeZone)
  AsyncControl<R> withUser(UserMemento user)     // <.>
  void setFuture(Future<R> future)     // <.>
  R waitForResult(long timeout, TimeUnit unit)     // <.>
}
----

<.> xref:#returningVoid_[returningVoid()]
+
--
Factory method to instantiate a control instance for a void action or a property edit (where there is no need or intention to provide a return value through the `Future`).
--
<.> xref:#returning_Class[returning(Class)]
+
--
Factory method to instantiate for a control instance for an action returning a value of `` (where this value will be returned through the `Future`).
--
<.> xref:#withSkipRules_[withSkipRules()]
+
--
Skip checking business rules (hide/disable/validate) before executing the underlying property or action
--
<.> xref:#with_ExceptionHandler[with(ExceptionHandler)]
+
--
How to handle exceptions if they occur, using the provided xref:refguide:applib:index/services/wrapper/control/ExceptionHandler.adoc[ExceptionHandler] .
--
<.> xref:#with_ExecutorService[with(ExecutorService)]
+
--
Specifies the _ExecutorService_ to use to obtain the thread to invoke the action.
--
<.> xref:#withUser_UserMemento[withUser(UserMemento)]
+
--
Specifies the user for the session used to execute the command asynchronously, in the background.
--
<.> xref:#setFuture_Future[setFuture(Future)]
+
--
For framework use only.
--
<.> xref:#waitForResult_long_TimeUnit[waitForResult(long, TimeUnit)]
+
--
Waits on the callers thread, for a maximum amount of time, for the result of the invocation to become available.
--

== Members

[#returningVoid_]
=== returningVoid()

Factory method to instantiate a control instance for a void action or a property edit (where there is no need or intention to provide a return value through the `Future`).

[#returning_Class]
=== returning(Class)

Factory method to instantiate for a control instance for an action returning a value of `` (where this value will be returned through the `Future`).

[#withSkipRules_]
=== withSkipRules()

Skip checking business rules (hide/disable/validate) before executing the underlying property or action

[#with_ExceptionHandler]
=== with(ExceptionHandler)

How to handle exceptions if they occur, using the provided xref:refguide:applib:index/services/wrapper/control/ExceptionHandler.adoc[ExceptionHandler] .

The default behaviour is to rethrow the exception.

[#with_ExecutorService]
=== with(ExecutorService)

Specifies the _ExecutorService_ to use to obtain the thread to invoke the action.

The default is _null_ , indicating, that its the xref:refguide:applib:index/services/wrapper/WrapperFactory.adoc[WrapperFactory] 's responsibility to provide a suitable _ExecutorService_ .

[#withUser_UserMemento]
=== withUser(UserMemento)

Specifies the user for the session used to execute the command asynchronously, in the background.

If not specified, then the user of the current foreground session is used.

[#setFuture_Future]
=== setFuture(Future)

For framework use only.

[#waitForResult_long_TimeUnit]
=== waitForResult(long, TimeUnit)

Waits on the callers thread, for a maximum amount of time, for the result of the invocation to become available.
