2

我的问题是如何配置 EJB 3.0 样式的消息驱动 bean 以在 jboss 中使用已配置的 JMS 数据源。

例如,我的 MDB 看起来像:

@MessageDriven(mappedName = "ExampleMDB", activationConfig = {

        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "MyTopic"),
        @ActivationConfigProperty(propertyName = "channel", propertyValue = "MyChannel"),

})
@ResourceAdapter(value = "wmq.jmsra.rar")
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
@TransactionManagement(TransactionManagementType.BEAN)
public class MyMDB implements MessageListener {
 .....
}

但我希望 bean 附加到给定的 JMS 数据源(对于 jboss 4.2.2,这是在 deploy/jms/jms-ds.xml 中)。也许这甚至是不可能的,但值得一问。

4

2 回答 2

1

如果我正确理解了您的问题,MyMDB在 WebLogic 上收听一个主题,并且您想使用 JBoss 提供的附加 JMS 目标,该目标在已部署的配置文件中定义并由其 JNDI 名称标识(默认情况下,deploy/jms/jms-ds.xml仅包含 JMS 的配置提供者和连接工厂——没有数据源)。

最简单的方法是让容器通过其 JNDI 名称注入 JMS 目的地和连接工厂(在 JBoss 中,JMS 目的地是通过部署 xxx-service.xml 文件来配置的)。在启动时,您可以初始化连接,并在 MDB 释放后立即执行清理。

以下示例显示了注入 ( @Resource) 和资源管理 ( @PostConstructand @PreDestroy)。JMS 连接和目的地用于useJmsDestination(String)发送文本消息。

public class MyMDB implements MessageListener {

    @Resource(mappedName = "queue/YourQueueName") // can be topic too
    private Queue targetDestination;

    @Resource(mappedName = "QueueConnectionFactory") // or ConnectionFactory
    private QueueConnectionFactory factory;

    private Connection conn;

    public void onMessage(Message m) {
        // parse message and do what you need to do
        ...
        // do something with the message and the JBoss JMS destination
        useJmsDestination(messageString);     
    }

    private void useJmsDestination(String text) {
        Session session = null;
        MessageProducer producer = null;

        try {
            session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
            producer = session.createProducer(targetDestination);
            TextMessage msg = session.createTextMessage(text);
            producer.send(msg);
        } catch (JMSException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (producer != null) {
                    producer.close();
                }
                if (session != null) {
                    session.close();
                }
            } catch (JMSException e) {
                // handle error, should be non-fatal, as the message is already sent.
            }
        }
    }


    @PostConstruct
    void init() {
        initConnection();
        // other initialization logic
        ...
    }

    @PreDestroy
    void cleanUp() {
        closeConnection();
        // other cleanup logic
        ...
    }

    private void initConnection() {
        try {
            conn = factory.createConnection();
        } catch (JMSException e) {
            throw new RuntimeException("Could not initialize connection", e);
        }
    }

    private void closeConnection() {
        try {
            conn.close();
        } catch (JMSException e) {
            // handle error, should be non-fatal, as the connection is being closed
        }
    }
}

我希望这可以帮助你。

于 2008-11-05T12:09:37.317 回答
1

我想您要问的是“如何指定 JMS 数据源的 JNDI 位置以用于 MDB?”

在这种情况下,答案是:

@ActivationConfigProperty(propertyName = "providerAdapterJNDI", propertyValue = "java:/DefaultJMSProvider")

此外,请查看以下页面,该页面提供了大量有关在 jBoss 中配置 MDB 的有用详细信息: http ://www.jboss.org/community/docs/DOC-9352

于 2008-11-05T12:29:20.367 回答