带有数据源的消息驱动 Bean

发布于 2024-07-08 20:15:03 字数 785 浏览 8 评论 0原文

我的问题是如何配置 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 中)。 也许这根本不可能,但值得一问。

My question is how do I configure an EJB 3.0 style message driven bean to use a configured JMS datasource in jboss.

For example, my MDB looks something like:

@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 {
 .....
}

But I would like the bean to attached to a given JMS datasource ( in the case of jboss 4.2.2 this is in deploy/jms/jms-ds.xml). Perhaps this is not even possible but is worth asking.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江湖正好 2024-07-15 20:15:04

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

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

以下示例显示了注入(@Resource)和资源管理(@PostConstruct@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
        }
    }
}

我希望这可以帮助你。

If I understood your problem correctly, MyMDB listens to a topic on WebLogic, and you want to use an additional JMS destination provided by JBoss, defined in a deployed configuration file and identified by its JNDI name (by default, deploy/jms/jms-ds.xml only contains the configuration for the JMS provider and connection factories -- no data sources).

The easiest way is to let the container inject the JMS destination and a connection factory via its JNDI name (in JBoss the JMS destinations are configured by deploying xxx-service.xml files). On startup you can then initialize the connection, and perform cleanup as soon as the MDB is released.

The following examples shows injection (@Resource) and resource managemend (@PostConstruct and @PreDestroy). The JMS connection and destination is used in useJmsDestination(String) to send a text message.

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

I hope this can help you.

℡寂寞咖啡 2024-07-15 20:15:04

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

在这种情况下,答案是:

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

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

I think what you are asking is "How do I specify the JNDI location of the JMS datasource to use for an MDB?"

In which case the answer is:

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

Also, take a look at the following page which provides loads of useful details on configuring MDBs in jBoss:
http://www.jboss.org/community/docs/DOC-9352

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文