11package com .acme .payment .provider ;
22
3+ import com .acme .payment .PaymentErrorEvent ;
34import com .acme .payment .PaymentRequestedEvent ;
5+ import com .acme .payment .PaymentSuccessEvent ;
46import jakarta .enterprise .context .ApplicationScoped ;
7+ import jakarta .enterprise .event .Event ;
58import jakarta .enterprise .event .ObservesAsync ;
9+ import jakarta .inject .Inject ;
610
711import java .util .concurrent .atomic .AtomicLong ;
812import java .util .logging .Logger ;
@@ -14,7 +18,20 @@ class PaymentProvider {
1418
1519 private final AtomicLong counter = new AtomicLong ();
1620
17- private
21+ private final Event <PaymentSuccessEvent > successEvent ;
22+
23+ private final Event <PaymentErrorEvent > errorEvent ;
24+
25+ @ Inject
26+ PaymentProvider (Event <PaymentSuccessEvent > successEvent , Event <PaymentErrorEvent > errorEvent ) {
27+ this .successEvent = successEvent ;
28+ this .errorEvent = errorEvent ;
29+ }
30+
31+ PaymentProvider () {
32+ this .successEvent = null ;
33+ this .errorEvent = null ;
34+ }
1835
1936 void receivePayment (@ ObservesAsync PaymentRequestedEvent event ) {
2037 LOGGER .info ("Processing payment: " + event .payment ());
@@ -25,9 +42,11 @@ void receivePayment(@ObservesAsync PaymentRequestedEvent event) {
2542 }
2643 if (counter .getAndDecrement () % 2 == 0 ) {
2744 LOGGER .warning ("Payment failed: " + event .payment ());
45+ successEvent .fire (new PaymentSuccessEvent (event .payment ()));
2846
2947 } else {
3048 LOGGER .info ("Payment successful: " + event .payment ());
49+ errorEvent .fire (new PaymentErrorEvent (event .payment ()));
3150 }
3251 LOGGER .info ("Payment processed: " + event .payment () + " - Confirmation #" + counter .incrementAndGet ());
3352 }
0 commit comments