用于远程ActiveMQ队列的Wildfly内部jndi绑定

时间:2017-04-27 19:52:44

标签: jms wildfly

我能够使用Wildfly 10中使用https://developer.jboss.org/wiki/HowToUseOutOfProcessActiveMQWithWildFly中定义的资源适配器从Remote ActiveMQ Queue成功读取它。它独立引用了以下配置

<subsystem xmlns="urn:jboss:domain:resource-adapters:2.0">  
  <resource-adapters>  
    <resource-adapter id="activemq-rar.rar">  
      <module slot="main" id="org.apache.activemq.ra" />  
      <transaction-support>XATransaction</transaction-support>  
        <config-property name="UseInboundSession">  
          false  
        </config-property>  
        <config-property name="Password">  
          mypassword
        </config-property>  
        <config-property name="UserName">  
          myuser  
        </config-property>  
        <config-property name="ServerUrl">  
          ssl://hostname:61617
        </config-property>  
        <connection-definitions>  
          <connection-definition class-name="org.apache.activemq.ra.ActiveMQManagedConnectionFactory" jndi-name="java:/ConnectionFactory" enabled="true" pool-name="ConnectionFactory">  
            <xa-pool>  
            <min-pool-size>1</min-pool-size>  
            <max-pool-size>20</max-pool-size>  
            <prefill>false</prefill>  
            <is-same-rm-override>false</is-same-rm-override>  
          </xa-pool>  
        </connection-definition>  
      </connection-definitions>  
      <admin-objects>  
        <admin-object class-name="org.apache.activemq.command.ActiveMQQueue" jndi-name="java:/activemq/projectName.queue.projectQueue" use-java-context="true" pool-name="PoolName">  
          <config-property name="PhysicalName">  
            ProjectQueue  
          </config-property>  
        </admin-object>  
      </admin-objects>  
    </resource-adapter>  
  </resource-adapters>  
</subsystem>

为了使代码生效,我在Listener上使用了以下MessageDriven注释

@MessageDriven(activationConfig = {
  @ActivationConfigProperty(propertyName = "destination", propertyValue = "projectName.queue.projectQueue"),
  @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
  @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")})
public class MyMDB implements MessageListener {
   ...
}

这是有效的,因为您基本上将远程jndi名称硬编码到@MessageDriven注释中。我更喜欢使用物理名称,以便远程队列名称可以更改,而无需更新代码中的注释。当我使用物理名称作为destiation值时,我得到以下错误

Failed to connect to broker [ssl://hostname:61617]: User myuser is not authorized to read from: queue://ProjectQueue: javax.jms.JMSSecurityException: User myuser is not authorized to read from: queue://ProjectQueue
  at org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:52)
  at org.apache.activemq.ActiveMQConnection.syncSendPacket(ActiveMQConnection.java:1399)
  at org.apache.activemq.ActiveMQConnection.syncSendPacket(ActiveMQConnection.java:1428)
  at org.apache.activemq.ActiveMQConnectionConsumer.<init>(ActiveMQConnectionConsumer.java:82)
  at org.apache.activemq.ActiveMQConnection.createConnectionConsumer(ActiveMQConnection.java:1244)
  at org.apache.activemq.ra.ActiveMQEndpointWorker$1.run(ActiveMQEndpointWorker.java:140)
  at org.jboss.jca.core.workmanager.WorkWrapper.run(WorkWrapper.java:223)
  at org.jboss.threads.SimpleDirectExecutor.execute(SimpleDirectExecutor.java:33)
  at org.jboss.threads.QueueExecutor.runTask(QueueExecutor.java:808)
  at org.jboss.threads.QueueExecutor.access$100(QueueExecutor.java:45)
  at org.jboss.threads.QueueExecutor$Worker.run(QueueExecutor.java:828)
  at java.lang.Thread.run(Thread.java:745)
  at org.jboss.threads.JBossThread.run(JBossThread.java:320)

是否可以创建一些内部JNDI绑定,以便我的@MessageDriven注释可以绑定到该名称或admin-object上的某个config-property?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。诀窍是“useJndi”ActivationConfigProperty

@MessageDriven(activationConfig = {
  @ActivationConfigProperty(propertyName = "destination", propertyValue = "java:/activemq/projectName.queue.projectQueue"),
  @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
  @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
  @ActivationConfigProperty(propertyName = "useJndi", propertyValue = "true")})
public class MyMDB implements MessageListener {
   ...
}

这允许JNDI看起来会在standalone.xml中找到资源适配器配置中定义的管理对象

<resource-adapters> 
  ...
  <admin-objects>  
    <admin-object class-name="org.apache.activemq.command.ActiveMQQueue" jndi-name="java:/activemq/projectName.queue.projectQueue" use-java-context="true" pool-name="PoolName">  
      <config-property name="PhysicalName">  
        ProjectQueue  
      </config-property>  
    </admin-object>  
  </admin-objects>
</resource-adapters>