swaroop c
swaroop c

Reputation: 11

Quarkus - service bean need to selected from an api value

PaymentMethodApiService have five implementations

Initially we had taking value from applicationConfig.yaml cm: ext-payment-gw: gw-selected: stripe and it is used by LookUpIfProperty @Slf4j @ApplicationScoped @LookupIfProperty(name = "cm.ext-payment-gw.gw-selected", stringValue = "stripe") public class StripePaymentGatewayService implements PaymentMethodApiService {

Now I need multiple paymentgateway to be selected in same deployment ext-payment-gw: gw-selected: - paypal - stripe

so @LookupIfProperty cannot initiate bean from a list it can do only one

Hence we need to make custom qualifer to pick beans with match of api value

    @ConfigProperty(name = "cm.ext-payment-gw.gw-selected")
    Map<String, Boolean> configuredGateways;

    @Inject
    ServiceProviderService serviceProviderService;

    @Inject
    Instance<PaymentMethodApiService> paymentMethodApiServiceProvider;


    private PaymentMethodApiService getPaymentGatewayForRequest() {
        var paymentGateway = serviceProviderService.retrievePaymentGatewayConfig().getProvider().toLowerCase();

        logger.info("Looking for gateway implementation: {}", paymentGateway);
        logger.info("Configured gateways: {}", configuredGateways.keySet());

        if (!Boolean.TRUE.equals(configuredGateways.get(paymentGateway))) {
            throw new IllegalStateException("Payment gateway " + paymentGateway +
                    " is not enabled in the configuration: " + configuredGateways);
        }

        for (PaymentMethodApiService service : paymentMethodApiServiceProvider) {
            Class<?> beanClass = service.getClass();
            logger.info("Discovered service: {}", beanClass.getSimpleName());

            // Handle proxies correctly by using the superclass if needed
            if (beanClass.getName().contains("_ClientProxy")) {
                beanClass = beanClass.getSuperclass(); // Get the actual class of the bean
            }

            SupportedPaymentGateway annotation = beanClass.getAnnotation(SupportedPaymentGateway.class);
            if (annotation != null) {
                logger.info("Service: {} has gateway: {}", beanClass.getSimpleName(), annotation.value());
                if (annotation.value().equalsIgnoreCase(paymentGateway)) {
                    return service;
                }
            } else {
                logger.info("Service: {} has no @SupportedPaymentGateway annotation", beanClass.getSimpleName());
            }
        }

        throw new IllegalStateException("No implementation found for gateway: " + paymentGateway);
    }```

I have made @SupportedPaymentGateway("stripe") for stripe service 
            @SupportedPaymentGateway("paypal") for paypal service 

but its straight away picking expection, that means there is no Instamces of PaymentMethodServiceProvider

2025-02-21 14:32:18,683 ERROR [com.obp.cms.exc.GenericExceptionMapper] (executor-thread-0) Exception occurred No implementation found for gateway: telus: java.lang.Illeg
alStateException: No implementation found for gateway: stripe

Upvotes: 0

Views: 23

Answers (0)

Related Questions