2

私の質問は、jboss で構成された JMS データソースを使用するように EJB 3.0 スタイルのメッセージ駆動型 Bean を構成する方法です。

たとえば、私の 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によって提供され、デプロイされた構成ファイルで定義され、JNDI名で識別される追加のJMS宛先を使用したい場合(デフォルトでdeploy/jms/jms-ds.xmlは、JMSの構成のみが含まれます)プロバイダーおよび接続ファクトリ-データソースなし)。

最も簡単な方法は、コンテナにJMDI名を介して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
        }
    }
}

これがお役に立てば幸いです。

于 2008-11-05T12:09:37.317 に答える
1

あなたが求めているのは、「 MDB に使用する JMS データソースの JNDI の場所を指定するにはどうすればよいですか?」ということだと思います。

その場合、答えは次のとおりです。

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

また、jBoss での MDB の設定に関する有用な詳細が多数記載されている次のページも参照してください: http://www.jboss.org/community/docs/DOC-9352

于 2008-11-05T12:29:20.367 に答える