nerdexam
Oracle

1Z0-900 · Question #7

Given the code fragment: Which code can be added to register both of these methods to receive BankEvent notifications only if an instance of BankActivityService is already instantiated in the…

The correct answer is A. @Observes(notifyObserver=IF_EXISTS) on line 3 and line 6. Option A correctly uses @Observes(notifyObserver=IF_EXISTS), which maps to the Reception.IF_EXISTS enum in CDI - this tells the container to skip notification if no instance of BankActivityService currently exists in scope, rather than creating a new one just to deliver the…

Use CDI Beans

Question

Given the code fragment:

Which code can be added to register both of these methods to receive BankEvent notifications only if an instance of BankActivityService is already instantiated in the current context?

Options

  • A@Observes(notifyObserver=IF_EXISTS) on line 3 and line 6
  • B@Observes(during=IN_PROGRESS) on line 1
  • C@Observes(during=AFTER_COMPLETION) on line 1
  • D@Observes(notifyObserver=IF_EXISTS) on line 4 and line 7 before method parameter declaration

How the community answered

(26 responses)
  • A
    73% (19)
  • B
    8% (2)
  • C
    15% (4)
  • D
    4% (1)

Explanation

Option A correctly uses @Observes(notifyObserver=IF_EXISTS), which maps to the Reception.IF_EXISTS enum in CDI - this tells the container to skip notification if no instance of BankActivityService currently exists in scope, rather than creating a new one just to deliver the event. Placing this annotation on lines 3 and 6 correctly targets the @Observes annotation position on both observer method parameters, which is where CDI expects it.

Options B and C are wrong because the during attribute controls transactional phases (e.g., only fire after a transaction commits), not conditional instantiation - they have nothing to do with whether a bean instance already exists.

Option D is wrong because it specifies the wrong line numbers (4 and 7 instead of 3 and 6); placing the annotation on incorrect lines would either miss the parameter or cause a compile error.

Memory tip: Think of notifyObserver=IF_EXISTS as a "don't wake me up" flag - the bean says "only tell me if I'm already awake." Contrast it with the default ALWAYS, which will instantiate the bean just to deliver the event. The during attribute is about transaction timing, not bean existence - keep those two axes separate.

Topics

#CDI Observer Methods#Event Handling#Bean Lifecycle Aware#notifyObserver Parameter

Community Discussion

No community discussion yet for this question.

Full 1Z0-900 Practice