tech_memo / java / JMS


tech_memo

HornetQ

MDB

メッセージフィルタリング

  • 1つのJMS Queueに、複数のMDBクラスを対応付けしたい場合で、メッセージを送信したいMDBを指定する方法
  • 送信側
    connect = jmsConnectionFactory.createConnection();
    session = connect.createSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    producer = session.createProducer(queue); // ★ Queue名の指定
    // create a JMS message and send it
    ObjectMessage objMsg = session.createObjectMessage(message);
    // set message selector
    objMsg.setStringProperty("MyFilterKey", "MyFilterValue"); // ★ ここでフィルターをkey, valueで指定(値は任意)
    
    producer.send(objMsg);
    • 上記で指定した、フィルターを受信側でも設定する(下記参照)
  • 受信側
    @MessageDriven(activationConfig = {
            @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), // ★ 接続先送信タイプの指定(Queue or Topic)
            @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/myjmsqueue"), // ★ Queue名の指定。WildFlyからはpropertyNameは、destinationTypeに変更っぽい
            @ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "MyFilterKey = 'MyFilterValue'") }) // ★ ここがフィルター部分 
    public class MyMDB extends implements MessageListener {
    • フィルター指定は、
      • propertyName = "messageSelector"は固定
      • propertyValue = "<FILTER_KEY> = '<FILTER_VALUE>'"
    • のフォーマットで指定する

Transaction timeout値設定

  • 単位は秒で960が設定されている。
    @MessageDriven(activationConfig = {
    		@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    	 	@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/myjmsqueue"),
    		@ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "MyFilterKey = 'MyFilterValue'"),
    		@ActivationConfigProperty(propertyName = "transactionTimeout", propertyValue = 960) })
    public class IdentifyPrepareTemplateEventReceiver extends AbstractEventReceiver

コンシューマプールサイズ設定

  • 上記リンクから抜粋
    JBoss を含むほとんどのアプリケーションサーバーでは、プール内の MDB の数を設定できます。 
    ejb3-interceptors-aop.xml ファイル内の MaxPoolSize パラメーターが、作成されるセッションまたはコンシューマーの数に影響を持たないことを理解することが重要です。
    これは、リソースアダプター実装がアプリケーションサーバー MDB 実装を認識しないためです。 
    たとえば、MDB MaxPoolSize を 1 に設定すると、15 個のセッションまたはコンシューマーが作成されます (15 がデフォルト値)。
    作成されるセッションまたはコンシューマーの数を制限するには、リソースアダプターまたは MDBのActivationConfigProperty アノテーションで maxSession パラメーターを設定します。
    
    @MessageDriven(name = "MDBMessageSendTxExample",
     activationConfig =
       {
         @ActivationConfigProperty
           (propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
         @ActivationConfigProperty
           (propertyName = "destination", propertyValue = "queue/testQueue"),
         @ActivationConfigProperty
           (propertyName = "maxSession", propertyValue = "1")
       })
    @TransactionManagement(value= TransactionManagementType.CONTAINER)
    @TransactionAttribute(value= TransactionAttributeType.REQUIRED)
    public class MyMDB implements MessageListener
    { ....}