简体   繁体   中英

Message Driven Bean Selectors (JMS)

I have recently discovered message selectors

@ActivationConfigProperty(
        propertyName="messageSelector",
        propertyValue="Fragile IS TRUE")

My Question is: How can I make the selector dynamic at runtime?

Lets say a consumer decided they wanted only messages with the property "Fragile IS FALSE"

Could the consumer change the selector somehow without redeploying the MDB?

Note: I am using Glassfish v2.1

To my knowledge, this is not possible. There may be implementations that will allow it via some custom server hooks, but it would be implementation dependent. For one, it requires a change to the deployment descriptor, which is not read after the EAR is deployed.

JMS (Jakarta Messaging) is designed to provide simple means to do simple things and more complicated things to do more complicated but less frequently needed things. Message-driven beans are an example of the first case. To do some dynamic reconfiguration, you need to stop using MDBs and start consuming messages using the programmatic API, using an injected JMSContext and topic or queue. For example:

    @Inject
    private JMSContext context;
    
    @Resource(lookup="jms/queue/thumbnail")
    Queue thumbnailQueue;

    JMSConsumer connectListener(String messageSelector) {
        JMSConsumer consumer = context.createConsumer(logTopic, messageSelector);
        consumer.setMessageListener(message -> {
            // process message
        });
        return consumer;
    }

You can call connectListener during startup, eg in a CDI bean:

public void start(@Observes @Initialized(ApplicationScoped.class) Object startEvent) {
    connectListener("Fragile IS TRUE");
}

Then you can easily reconfigure it by closing the returned consumer and creating it again with a new selector string:

consumer.close();
consumer = connectListener("Fragile IS FALSE");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM