Skip to content

Commit b06d0b8

Browse files
committed
chore: inject and fire PaymentSuccessEvent and PaymentErrorEvent in PaymentProvider
1 parent 1a6904f commit b06d0b8

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

src/main/java/com/acme/payment/provider/PaymentProvider.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.acme.payment.provider;
22

3+
import com.acme.payment.PaymentErrorEvent;
34
import com.acme.payment.PaymentRequestedEvent;
5+
import com.acme.payment.PaymentSuccessEvent;
46
import jakarta.enterprise.context.ApplicationScoped;
7+
import jakarta.enterprise.event.Event;
58
import jakarta.enterprise.event.ObservesAsync;
9+
import jakarta.inject.Inject;
610

711
import java.util.concurrent.atomic.AtomicLong;
812
import 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

Comments
 (0)