Java Reference
In-Depth Information
The event handler for the
billpayment
example,
PaymentHandler
, defines two ob-
server methods, one for each type of
PaymentEvent
:
public void creditPayment(@Observes @Credit PaymentEvent event) {
...
}
public void debitPayment(@Observes @Debit PaymentEvent event) {
...
}
Observer methods can also be conditional or transactional:
• A conditional observer method is notified of an event only if an instance of the
bean that defines the observer method already exists in the current context. To de-
clare a conditional observer method, specify
notifyObserver=IF_EXISTS
as an argument to
@Observes
:
@Observes(notifyObserver=IF_EXISTS)
To
obtain
the
default
unconditional
behavior,
you
can
specify
@Ob-
serves(notifyObserver=ALWAYS)
.
• A transactional observer method is notified of an event during the before-comple-
tion or after-completion phase of the transaction in which the event was fired. You
can also specify that the notification is to occur only after the transaction has com-
pleted successfully or unsuccessfully. To specify a transactional observer method,
use any of the following arguments to
@Observes
:
@Observes(during=BEFORE_COMPLETION)
@Observes(during=AFTER_COMPLETION)
@Observes(during=AFTER_SUCCESS)
@Observes(during=AFTER_FAILURE)
To
obtain
the
default
non-transactional
behavior,
specify
@Ob-
serves(during=IN_PROGRESS)
.
An observer method that is called before completion of a transaction may call the
setRollbackOnly
method on the transaction instance to force a transaction
rollback.