java - 动态配置 Message Driven Bean 中的 MappedName 注解

标签 java jakarta-ee annotations ejb-3.0 message-driven-bean

当使用消息驱动的 BEans 时,接收消息的目的地名称被硬编码在注解中 @MessageDriven(mappedName = "someDestinationName")

有没有办法在运行时添加这些信息? Bellow 是一个示例消息驱动 Bean 类。

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

最佳答案

据我所知,不,你不能那样做。

因为目标(它是一个字符串)和 bean(它是一个类)的耦合在部署时完成一次,您不能以编程方式更改目标。

也许有重新绑定(bind)的技巧;我的意思是强制容器释放 MDB,然后更改目标并重新初始化(通过依赖注入(inject),然后发布构造步骤等),但我怀疑应用程序服务器是否允许这样做。

摘自 JSR-318(EJB 3.1 规范);

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.

关于java - 动态配置 Message Driven Bean 中的 MappedName 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16033568/

相关文章:

java - 将值从 jsp 发送到 Servlet

Java如何在运行时链接库

java - 在 Java 8 PropertyDescriptor 中忽略重写方法上的注解

java - new IndexWriter() 处的 LockObtainFailedException

java - 导入 keystore 以签署小程序

java - JPA @PreUpdate 未被调用

java - Java SE 和 Java EE 之间的主要区别是什么?

java - 使用注释修改方法

groovy - 无法在 groovy 方法上添加多个注释?

java - 通过 Selenium Chromedriver 自动授予对 Chrome 48 中视频和音频的访问权限