Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
63 views
in Technique[技术] by (71.8m points)

java - Is there any way to assign value to final variable from configuration?

I have a method that will listen to the webhook queue and the method signature needs the webhook name as a final variable. This WEBHOOK_QUEUE variable needs to be different from the environment to the environment. (sandbox, production) Is there any way to solve this without changing the code In a different environment?

@ApplicationScoped
public class WebhookEventObserver {
private static final String WEBHOOK_QUEUE = "V3-SANDBOX-WEBHOOK";


@Inject
private WebhookProcessor processor;

public void onMessage(@Observes @Queue(name = WEBHOOK_QUEUE) MessageEvent event) {
    logger.info("Received [{}] message from [{}]", event, WEBHOOK_QUEUE);
    processor.handleEvent(event);
}
}
question from:https://stackoverflow.com/questions/65869275/is-there-any-way-to-assign-value-to-final-variable-from-configuration

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Change the field from a constant to an injected configuration property:

@ApplicationScoped
public class WebhookEventObserver {

    @Inject @ConfigurationValue("webhookQueue")
    private String webhookQueue;

    @Inject
    private WebhookProcessor processor;

    public void onMessage(@Observes MessageEvent event) {
        // check queue here
        logger.info("Received [{}] message from [{}]", event, webhookQueue);
        processor.handleEvent(event);
    }
}

You can't do the @Queue(name = WEBHOOK_QUEUE) with a dynamic queue name, so remove that event qualifier, and add logic to check it.

Don't know what MessageEvent is, so don't know how to check the queue of the event.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...