Configuring MappedName annotation in Message Drive

2019-04-29 06:51发布

问题:

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
    }
}

回答1:

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.