Вопрос

When using Message Driven BEans, the destination name from where to receive messages is hard coded in the annotation @MessageDriven(mappedName = "someDestinationName")

Is there a way to add this information at runtime? Bellow is a sample Message Driven Bean class.

package mdb.beans;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;

@MessageDriven(mappedName = "someDestinationName", activationConfig =
{
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue =   "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class MDBSample implements MessageListener 
{    
    public MDBSample() 
    {
        // constructor
    }

    @Override
    public void onMessage(Message message) 
    {
        // logic when message received
    }
}
Это было полезно?

Решение

As far as I know, no, you cannot do that.

Because, the coupling of the destination (which is a String) and the bean (which is a class) is done once in the deploy-time, you cannot change the destination programmatically.

Maybe there is a hack for re-binding; I mean forcing the container to release MDB, then change destination and re-initalize (go through dependency injection, then post construct steps etc.) but I doubt the application servers will allow that.

Excerpt from JSR-318 (EJB 3.1 spec);

5.4.17 Association of a Message-Driven Beanwith a Destination or Endpoint

A message-driven bean is associated with a destination or endpoint when the bean is deployed in the container. It is the responsibility of the Deployer to associate the message-driven bean with a destination or endpoint.

5.4.17.1 JMS Message-Driven Beans

A JMS message-driven bean is associated with a JMS Destination (Queue or Topic) when the bean is deployed in the container. It is the responsibility of the Deployer to associate the message-driven bean with a Queue or Topic.

Другие советы

You can achieve this at application startup time by injecting values from system variables.

With Thorntail (2.6.0) we introduced the following setting in project-defaults.yml:

thorntail:
  ee:
    annotation-property-replacement: true

Then referenced config params from MessageDrivenBean:

@ActivationConfigProperty(
   propertyName = "destination",
   propertyValue = "java:/jms/queue/${configuration.customer}.queue")
   ...

Where configuration.customer is a system var set at startup.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top