无法在无状态 EJB 中注入 JMS 队列

Posted

技术标签:

【中文标题】无法在无状态 EJB 中注入 JMS 队列【英文标题】:Cannot inject JMS Queue in Stateless EJB 【发布时间】:2017-03-31 16:56:53 【问题描述】:

我正在尝试将 JMS 队列注入无状态 EJB。我的代码看起来像这样:

@Stateless
public class JmsSender  implements Serializable 

    private static final long serialVersionUID = 1L;

    @Resource(mappedName="java:/ConnectionFactory")
    private ConnectionFactory connectionFactory;

    @Resource(mappedName="queue/TestQueue")
    private Queue queue;

    public void doIt(String nachricht)

        try
            Connection connection = connectionFactory.createConnection();
            Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer=session.createProducer(queue);
            TextMessage message = session.createTextMessage("Queue-Message: " +nachricht);
            messageProducer.send(message);
        catch (JMSException e) 
            e.printStackTrace();
        

           


当我尝试将其部署到应用程序服务器 (Wildfly 9.0.2) 时引发以下异常

09:05:11,409 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "JMSProject.war")]) - failure description: "WFLYCTL0180: Services with missing/unavailable dependencies" => ["jboss.naming.context.java.module.JMSProject.JMSProject.env.\"abc.jms.JmsSender\".queue is missing [jboss.naming.context.java.module.JMSProject.JMSProject.env.queue.TestQueue]"]
09:05:11,455 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 37) WFLYSRV0010: Deployed "JMSProject.war" (runtime-name : "JMSProject.war")
09:05:11,456 INFO  [org.jboss.as.controller] (Controller Boot Thread) WFLYCTL0183: Service status report
WFLYCTL0184:    New missing/unsatisfied dependencies:
      service jboss.naming.context.java.module.JMSProject.JMSProject.env.queue.TestQueue (missing) dependents: [service jboss.naming.context.java.module.JMSProject.JMSProject.env."abc.jms.JmsSender".queue] 

09:05:11,582 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
09:05:11,583 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
09:05:11,583 ERROR [org.jboss.as] (Controller Boot Thread) WFLYSRV0026: WildFly Full 9.0.2.Final (WildFly Core 1.0.2.Final) started (with errors) in 4043ms - Started 360 of 557 services (26 services failed or missing dependencies, 242 services are lazy, passive or on-demand)
09:05:11,616 INFO  [org.jboss.weld.deployer] (MSC service thread 1-7) WFLYWELD0010: Stopping weld service for deployment JMSProject.war
09:05:11,656 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-7) WFLYSRV0028: Stopped deployment JMSProject.war (runtime-name: JMSProject.war) in 46ms
09:05:11,700 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) WFLYSRV0009: Undeployed "JMSProject.war" (runtime-name: "JMSProject.war")
09:05:11,705 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 1) WFLYCTL0183: Service status report
WFLYCTL0184:    New missing/unsatisfied dependencies:
      service jboss.deployment.unit."JMSProject.war".WeldBootstrapService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldTerminalListener".WeldInstantiator, service jboss.deployment.unit."JMSProject.war".component.JmsSender.WeldInstantiator, service jboss.deployment.unit."JMSProject.war".component.JmsSender.WeldInterceptorBindingsService, service jboss.deployment.unit."JMSProject.war".WeldStartService, WFLYCTL0208: ... and 7 more ] 
      service jboss.deployment.unit."JMSProject.war".WeldStartService (missing) dependents: [service jboss.undertow.deployment.default-server.default-host./JMSProject, service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldTerminalListener".WeldInstantiator, service jboss.deployment.unit."JMSProject.war".component.JmsSender.WeldInstantiator, service jboss.deployment.unit."JMSProject.war".component."com.sun.faces.config.ConfigureListener".WeldInstantiator, WFLYCTL0208: ... and 9 more ] 
      service jboss.deployment.unit."JMSProject.war".component.JmsReceiver.CREATE (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component.JmsReceiver.START] 
      service jboss.deployment.unit."JMSProject.war".component.JmsReceiver.JndiBindingsService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.deployment.unit."JMSProject.war".component.JmsReceiver.START (missing) dependents: [service jboss.undertow.deployment.default-server.default-host./JMSProject, service jboss.deployment.unit."JMSProject.war".deploymentCompleteService, service jboss.deployment.unit."JMSProject.war".moduleDeploymentRuntimeInformationStart] 
      service jboss.deployment.unit."JMSProject.war".component.JmsReceiver.VIEW."abc.jms.JmsReceiver".MESSAGE_ENDPOINT (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component.JmsReceiver.START] 
      service jboss.deployment.unit."JMSProject.war".component.JmsReceiver.WeldInstantiator (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component.JmsReceiver.START] 
      service jboss.deployment.unit."JMSProject.war".component.JmsReceiver.WeldInterceptorBindingsService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component.JmsReceiver.WeldInstantiator] 
      service jboss.deployment.unit."JMSProject.war".component.JmsReceiver.ejb.non-functional-timerservice (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component.JmsReceiver.START] 
      service jboss.deployment.unit."JMSProject.war".component.JmsSender.CREATE (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component.JmsSender.START] 
      service jboss.deployment.unit."JMSProject.war".component.JmsSender.JndiBindingsService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.deployment.unit."JMSProject.war".component.JmsSender.START (missing) dependents: [service jboss.undertow.deployment.default-server.default-host./JMSProject, service jboss.deployment.unit."JMSProject.war".deploymentCompleteService] 
      service jboss.deployment.unit."JMSProject.war".component.JmsSender.VIEW."abc.jms.JmsSender".LOCAL (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component.JmsSender.START] 
      service jboss.deployment.unit."JMSProject.war".component.JmsSender.WeldInstantiator (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component.JmsSender.START] 
      service jboss.deployment.unit."JMSProject.war".component.JmsSender.WeldInterceptorBindingsService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component.JmsSender.WeldInstantiator] 
      service jboss.deployment.unit."JMSProject.war".component.JmsSender.ejb.non-functional-timerservice (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component.JmsSender.START] 
      service jboss.deployment.unit."JMSProject.war".component."com.sun.faces.config.ConfigureListener".CREATE (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component."com.sun.faces.config.ConfigureListener".START] 
      service jboss.deployment.unit."JMSProject.war".component."com.sun.faces.config.ConfigureListener".JndiBindingsService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.deployment.unit."JMSProject.war".component."com.sun.faces.config.ConfigureListener".START (missing) dependents: [service jboss.undertow.deployment.default-server.default-host./JMSProject, service jboss.undertow.deployment.default-server.default-host./JMSProject.UndertowDeploymentInfoService, service jboss.deployment.unit."JMSProject.war".deploymentCompleteService] 
      service jboss.deployment.unit."JMSProject.war".component."javax.faces.webapp.FacetTag".CREATE (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component."javax.faces.webapp.FacetTag".START] 
      service jboss.deployment.unit."JMSProject.war".component."javax.faces.webapp.FacetTag".JndiBindingsService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.deployment.unit."JMSProject.war".component."javax.faces.webapp.FacetTag".START (missing) dependents: [service jboss.undertow.deployment.default-server.default-host./JMSProject, service jboss.deployment.unit."JMSProject.war".deploymentCompleteService] 
      service jboss.deployment.unit."JMSProject.war".component."javax.faces.webapp.FacetTag".WeldInstantiator (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component."javax.faces.webapp.FacetTag".START] 
      service jboss.deployment.unit."JMSProject.war".component."javax.servlet.jsp.jstl.tlv.PermittedTaglibsTLV".JndiBindingsService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.deployment.unit."JMSProject.war".component."javax.servlet.jsp.jstl.tlv.PermittedTaglibsTLV".START (missing) dependents: [service jboss.undertow.deployment.default-server.default-host./JMSProject, service jboss.undertow.deployment.default-server.default-host./JMSProject.UndertowDeploymentInfoService, service jboss.deployment.unit."JMSProject.war".deploymentCompleteService] 
      service jboss.deployment.unit."JMSProject.war".component."javax.servlet.jsp.jstl.tlv.ScriptFreeTLV".JndiBindingsService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.deployment.unit."JMSProject.war".component."javax.servlet.jsp.jstl.tlv.ScriptFreeTLV".START (missing) dependents: [service jboss.undertow.deployment.default-server.default-host./JMSProject, service jboss.undertow.deployment.default-server.default-host./JMSProject.UndertowDeploymentInfoService, service jboss.deployment.unit."JMSProject.war".deploymentCompleteService] 
      service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldInitialListener".CREATE (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldInitialListener".START] 
      service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldInitialListener".JndiBindingsService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldInitialListener".START (missing) dependents: [service jboss.undertow.deployment.default-server.default-host./JMSProject, service jboss.undertow.deployment.default-server.default-host./JMSProject.UndertowDeploymentInfoService, service jboss.deployment.unit."JMSProject.war".deploymentCompleteService] 
      service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldInitialListener".WeldInstantiator (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldInitialListener".START] 
      service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldTerminalListener".CREATE (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldTerminalListener".START] 
      service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldTerminalListener".JndiBindingsService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldTerminalListener".START (missing) dependents: [service jboss.undertow.deployment.default-server.default-host./JMSProject, service jboss.deployment.unit."JMSProject.war".deploymentCompleteService] 
      service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldTerminalListener".WeldInstantiator (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldTerminalListener".START] 
      service jboss.deployment.unit."JMSProject.war".jndiDependencyService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldTerminalListener".START, service jboss.deployment.unit."JMSProject.war".component."org.jboss.weld.servlet.WeldInitialListener".START, service jboss.deployment.unit."JMSProject.war".WeldStartService, service jboss.deployment.unit."JMSProject.war".component.JmsReceiver.START, WFLYCTL0208: ... and 3 more ] 
      service jboss.deployment.unit."JMSProject.war".moduleDeploymentRuntimeInformation (missing) dependents: [service jboss.deployment.unit."JMSProject.war".moduleDeploymentRuntimeInformationStart, service jboss.deployment.unit."JMSProject.war".component.JmsReceiver.START, service jboss.deployment.unit."JMSProject.war".component.JmsSender.START] 
      service jboss.naming.context.java.app.JMSProject.env (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.naming.context.java.module.JMSProject.JMSProject.BeanManager (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.naming.context.java.module.JMSProject.JMSProject.DefaultContextService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.naming.context.java.module.JMSProject.JMSProject.DefaultDataSource (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.naming.context.java.module.JMSProject.JMSProject.DefaultJMSConnectionFactory (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.naming.context.java.module.JMSProject.JMSProject.DefaultManagedExecutorService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.naming.context.java.module.JMSProject.JMSProject.DefaultManagedScheduledExecutorService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.naming.context.java.module.JMSProject.JMSProject.DefaultManagedThreadFactory (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.naming.context.java.module.JMSProject.JMSProject.EJBContext (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.naming.context.java.module.JMSProject.JMSProject.TimerService (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.naming.context.java.module.JMSProject.JMSProject.TransactionSynchronizationRegistry (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.naming.context.java.module.JMSProject.JMSProject.UserTransaction (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.naming.context.java.module.JMSProject.JMSProject.env (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService] 
      service jboss.naming.context.java.module.JMSProject.JMSProject.env."abc.jms.JmsSender".connectionFactory (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService, service jboss.deployment.unit."JMSProject.war".component.JmsSender.START] 
      service jboss.naming.context.java.module.JMSProject.JMSProject.env."abc.jms.JmsSender".queue (missing) dependents: [service jboss.deployment.unit."JMSProject.war".jndiDependencyService, service jboss.deployment.unit."JMSProject.war".component.JmsSender.START] 
      service jboss.server.global-request-controller.control-point."JMSProject.war".undertow (missing) dependents: [service jboss.undertow.deployment.default-server.default-host./JMSProject.UndertowDeploymentInfoService] 
      service jboss.undertow.deployment.default-server.default-host./JMSProject (missing) dependents: [service jboss.deployment.unit."JMSProject.war".deploymentCompleteService] 
      service jboss.undertow.deployment.default-server.default-host./JMSProject.UndertowDeploymentInfoService (missing) dependents: [service jboss.undertow.deployment.default-server.default-host./JMSProject] 
      service jboss.undertow.deployment.default-server.default-host./JMSProject.codec (missing) dependents: [service jboss.undertow.deployment.default-server.default-host./JMSProject.UndertowDeploymentInfoService] 
      service jboss.undertow.deployment.default-server.default-host./JMSProject.session (missing) dependents: [service jboss.undertow.deployment.default-server.default-host./JMSProject.UndertowDeploymentInfoService] 

09:05:16,479 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) WFLYDS0004: Found JMSProject.war in deployment directory. To trigger deployment create a file called JMSProject.war.dodeploy
09:15:08,434 INFO  [org.jboss.as.repository] (ServerService Thread Pool -- 71) WFLYDR0009: Content /Users/cwi/java/wildfly-9.0.2.Final/standalone/data/content/08/0c5a481cd7abf27bfd4b48edf73b1cb214085e is obsolete and will be removed
09:15:08,443 INFO  [org.jboss.as.repository] (ServerService Thread Pool -- 71) WFLYDR0002: Content removed from location /Users/cwi/java/wildfly-9.0.2.Final/standalone/data/content/08/0c5a481cd7abf27bfd4b48edf73b1cb214085e/content

如果我将 mappedName 更改为远程 JNDI 名称,它就可以工作

@Stateless
public class JmsSender  implements Serializable 

    @Resource(mappedName="java:jboss/exported/jms/queue/TestQueue")
    private Queue queue;

或者如果我将无状态 EJB 切换到依赖 CDI Bean

@Dependent
public class JmsSender  implements Serializable 

    @Resource(mappedName="queue/TestQueue")
    private Queue queue;

Wildfly 配置如下所示:

<subsystem xmlns="urn:jboss:domain:messaging:3.0">
            <hornetq-server>
                <security-enabled>false</security-enabled>
                <journal-file-size>102400</journal-file-size>

                <connectors>
                    <http-connector name="http-connector" socket-binding="http">
                        <param key="http-upgrade-endpoint" value="http-acceptor"/>
                    </http-connector>
                    <http-connector name="http-connector-throughput" socket-binding="http">
                        <param key="http-upgrade-endpoint" value="http-acceptor-throughput"/>
                        <param key="batch-delay" value="50"/>
                    </http-connector>
                    <in-vm-connector name="in-vm" server-id="0"/>
                </connectors>

                <acceptors>
                    <http-acceptor http-listener="default" name="http-acceptor"/>
                    <http-acceptor http-listener="default" name="http-acceptor-throughput">
                        <param key="batch-delay" value="50"/>
                        <param key="direct-deliver" value="false"/>
                    </http-acceptor>
                    <in-vm-acceptor name="in-vm" server-id="0"/>
                </acceptors>

                <security-settings>
                    <security-setting match="#">
                        <permission type="send" roles="guest"/>
                        <permission type="consume" roles="guest"/>
                        <permission type="createNonDurableQueue" roles="guest"/>
                        <permission type="deleteNonDurableQueue" roles="guest"/>
                    </security-setting>
                </security-settings>

                <address-settings>
                    <address-setting match="#">
                        <dead-letter-address>jms.queue.DLQ</dead-letter-address>
                        <expiry-address>jms.queue.ExpiryQueue</expiry-address>
                        <max-size-bytes>10485760</max-size-bytes>
                        <page-size-bytes>2097152</page-size-bytes>
                        <message-counter-history-day-limit>10</message-counter-history-day-limit>
                    </address-setting>
                </address-settings>

                <jms-connection-factories>
                    <connection-factory name="InVmConnectionFactory">
                        <connectors>
                            <connector-ref connector-name="in-vm"/>
                        </connectors>
                        <entries>
                            <entry name="java:/ConnectionFactory"/>
                        </entries>
                    </connection-factory>
                    <connection-factory name="RemoteConnectionFactory">
                        <connectors>
                            <connector-ref connector-name="http-connector"/>
                        </connectors>
                        <entries>
                            <entry name="java:jboss/exported/jms/RemoteConnectionFactory"/>
                        </entries>
                    </connection-factory>
                    <pooled-connection-factory name="hornetq-ra">
                        <transaction mode="xa"/>
                        <connectors>
                            <connector-ref connector-name="in-vm"/>
                        </connectors>
                        <entries>
                            <entry name="java:/JmsXA"/>
                            <entry name="java:jboss/DefaultJMSConnectionFactory"/>
                        </entries>
                    </pooled-connection-factory>
                </jms-connection-factories>

                <jms-destinations>
                    <jms-queue name="ExpiryQueue">
                        <entry name="java:/jms/queue/ExpiryQueue"/>
                    </jms-queue>
                    <jms-queue name="DLQ">
                        <entry name="java:/jms/queue/DLQ"/>
                    </jms-queue>
                    <jms-queue name="TestQueue">
                        <entry name="queue/TestQueue"/>
                        <entry name="java:jboss/exported/jms/queue/TestQueue"/>
                    </jms-queue>
                    <jms-topic name="TestTopic">
                        <entry name="topic/TestTopic"/>
                        <entry name="java:jboss/exported/jms/topic/TestTopic"/>
                    </jms-topic>
                </jms-destinations>
            </hornetq-server>
        </subsystem>

那么为什么不能将 JMS 队列注入到无状态 EJB 中,为什么它在 CDI bean 中工作?我做错了什么?

提前谢谢你!

克里斯

【问题讨论】:

您在 Wildfly 上使用 full 个人资料吗? Wildfly 是否启用了 JMS? 是的,我正在使用完整的个人资料。 【参考方案1】:

我认为这应该可行:

@Resource(mappedName="java:/queue/TestQueue")
private Queue queue;

另见 https://docs.jboss.org/author/display/WFLY9/JNDI+Reference

【讨论】:

谢谢!这是有效的。但是为什么我必须在 EJB 中使用前缀“java:/”而不是在 CDI bean 中?

以上是关于无法在无状态 EJB 中注入 JMS 队列的主要内容,如果未能解决你的问题,请参考以下文章

如何将超时设置为 EJB 方法(在无状态 bean 中)

将资源注入无状态本地EJB

在条件下在无状态会话 Bean 中注入特定的 EntityManager

在其他模块的 JSF ManagedBean 中通过 @EJB(lookup) 注入远程无状态 EJB

在无状态组件中的现有状态转换期间无法更新

迁移到 JBOSS 7.4 和 Java11 后未正确注入 EJB 无状态 bean