【问题标题】:javax.jms.JMSException: Failed to create session factory while sending message to embedded ActiveMQ Artemis within JBoss EAP 7.2javax.jms.JMSException: 在 JBoss EAP 7.2 中向嵌入式 ActiveMQ Artemis 发送消息时创建会话工厂失败
【发布时间】:2020-05-12 01:18:15
【问题描述】:

我正在尝试开发一个将消息发送到嵌入在 JBoss EAP 7.2 中的 ActiveMQ Artemis 的程序。我已按照this question:

中给出的说明进行操作

以下是我发送消息的方法:

public void sendMessage() {
    Context context = null;
    ConnectionFactory factory = null;
    Destination destination = null;
    Connection connection = null;
    Session session = null;
    MessageProducer producer = null;

    Properties initialProperties = new Properties();
    initialProperties.put(InitialContext.INITIAL_CONTEXT_FACTORY,
            "org.wildfly.naming.client.WildFlyInitialContextFactory");
    initialProperties.put(InitialContext.PROVIDER_URL, "http-remoting://<<EXTERNAL-IP>>:8080");
    initialProperties.put(Context.SECURITY_PRINCIPAL, "mquser");
    initialProperties.put(Context.SECURITY_CREDENTIALS, "Mquser@123");
    try {
        context = new InitialContext(initialProperties);
        factory = (QueueConnectionFactory) context.lookup("jms/RemoteConnectionFactory");
        System.out.println("Lookup Success");
        destination = (Destination) context.lookup("jms/queue/TestQueue");
        System.out.println("Queue lookup success");
        connection = factory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(destination);
        String text = "Test Message";
        TextMessage textMessage = session.createTextMessage();
        textMessage.setText(text);
        connection.start();
        System.out.println("Going to send");
        producer.send(textMessage);
        System.out.println(this.getClass().getName() + "has sent a message : " + text);
    } catch (NamingException e) {
        e.printStackTrace();
    } catch (JMSException e) {
        e.printStackTrace();
    } finally {
        if (context != null) {
            try {
                context.close();
            } catch (NamingException ex) {
                ex.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException ex) {
                ex.printStackTrace();
            }
        }
    }
}

我创建了应用程序用户mquser,密码为Mquser@123。我在standalone-full.xml 中禁用了安全性。下面是standalone-full.xml 中消息传递子系统的样子:

<subsystem xmlns="urn:jboss:domain:messaging-activemq:4.0">
    <server name="default">
        <journal pool-files="10"/>
        <security enabled="false"/>
        <security-setting name="#">
            <role name="guest" send="true" consume="true" create-non-durable-queue="true" delete-non-durable-queue="true"/>
        </security-setting>
        <address-setting name="#" dead-letter-address="jms.queue.DLQ" expiry-address="jms.queue.ExpiryQueue" max-size-bytes="10485760" page-size-bytes="2097152" message-counter-history-day-limit="10"/>
        <http-connector name="http-connector" socket-binding="http" endpoint="http-acceptor"/>
        <http-connector name="http-connector-throughput" socket-binding="http" endpoint="http-acceptor-throughput">
            <param name="batch-delay" value="50"/>
        </http-connector>
        <in-vm-connector name="in-vm" server-id="0">
            <param name="buffer-pooling" value="false"/>
        </in-vm-connector>
        <http-acceptor name="http-acceptor" http-listener="default"/>
        <http-acceptor name="http-acceptor-throughput" http-listener="default">
            <param name="batch-delay" value="50"/>
            <param name="direct-deliver" value="false"/>
        </http-acceptor>
        <in-vm-acceptor name="in-vm" server-id="0">
            <param name="buffer-pooling" value="false"/>
        </in-vm-acceptor>
        <jms-queue name="TestQueue" entries="java:jboss/exported/jms/queue/TestQueue"/>
        <jms-queue name="ExpiryQueue" entries="java:/jms/queue/ExpiryQueue"/>
        <jms-queue name="DLQ" entries="java:/jms/queue/DLQ"/>
        <connection-factory name="InVmConnectionFactory" entries="java:/ConnectionFactory" connectors="in-vm"/>
        <connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector"/>
        <pooled-connection-factory name="activemq-ra" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory" connectors="in-vm" transaction="xa"/>   
    </server>
</subsystem>

下面是standalone-full.xml的and部分

<interfaces>
    <interface name="management">
        <inet-address value="${jboss.bind.address.management:127.0.0.1}"/>
    </interface>
    <interface name="public">
        <inet-address value="${jboss.bind.address:127.0.0.1}"/>
    </interface>
    <interface name="unsecure">
        <inet-address value="${jboss.bind.address.unsecure:127.0.0.1}"/>
    </interface>
</interfaces>
<socket-binding-group name="standard-sockets" default-interface="public" port-offset="${jboss.socket.binding.port-offset:0}">
    <socket-binding name="management-http" interface="management" port="${jboss.management.http.port:9990}"/>
    <socket-binding name="management-https" interface="management" port="${jboss.management.https.port:9993}"/>
    <socket-binding name="ajp" port="${jboss.ajp.port:8009}"/>
    <socket-binding name="http" port="${jboss.http.port:8080}"/>
    <socket-binding name="https" port="${jboss.https.port:8443}"/>
    <socket-binding name="txn-recovery-environment" port="4712"/>
    <socket-binding name="txn-status-manager" port="4713"/>
    <socket-binding name="iiop" interface="unsecure" port="3528"/>
    <socket-binding name="iiop-ssl" interface="unsecure" port="3529"/>
    <outbound-socket-binding name="mail-smtp">
        <remote-destination host="localhost" port="25"/>
    </outbound-socket-binding>
</socket-binding-group>

我正在使用以下命令启动 jboss

sh standalone.sh --server-config=standalone-full.xml -b <<INTERNAL-IP>> -bmanagement <<INTERNAL-IP>>

有了这些,我能够在我的一个环境中成功地向 MQ 发送消息,但在另一个环境中它失败了,出现以下异常:

javax.jms.JMSException: Failed to create session factory
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:846)
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:282)
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:277)
    at com.mycompany.springdb.service.MessagingService.sendMessage(MessagingService.java:45)
    at com.mycompany.springdb.controller.DateController.sendMessage(DateController.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1591)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: ActiveMQNotConnectedException[errorType=NOT_CONNECTED message=AMQ219007: Cannot connect to server(s). Tried with all available servers.]
    at org.apache.activemq.artemis.core.client.impl.ServerLocatorImpl.createSessionFactory(ServerLocatorImpl.java:797)
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:844)
    ... 58 more

connection = factory.createConnection();这一行中,谁能告诉我程序有什么问题,或者环境有什么问题,或者可能是什么问题?

【问题讨论】:

  • @JustinBertram 我已经用standalone-full.xml 的完整消息子系统部分更新了我的问题。还需要什么吗?
  • @JustinBertram 我添加了standalone-full.xml 的 部分以及我用来启动jboss 的命令。请注意,在启动 jboss 时,我使用的是服务器的内部 IP 地址,而在 JNDI 中查找管理对象时,我使用的是服务器的外部 IP 地址。
  • 如果您将服务器绑定到内部 IP 地址,那么 JNDI 查找在使用 外部 IP 地址的情况下如何工作?您是否正在进行某种 NAT?
  • 我不确定网络细节,但在 JNDI 中成功查找了 ConnectionFactory 和 Queue。只有在创建 Connection 对象时才会引发异常。

标签: java jms activemq-artemis jboss-eap-7


【解决方案1】:

当您在启动服务器时使用命令行上的-b 开关时,这意味着您的socket-binding-group 中的所有套接字绑定将默认绑定到该IP 地址。

您的 JMS 客户端正在查找 jms/RemoteConnectionFactory。这是connection-factory的配置:

<connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector"/>

这个connection-factory 正在使用http-connector。这是http-connector的配置:

<http-connector name="http-connector" socket-binding="http" endpoint="http-acceptor"/>

这是使用 http socket-binding 使用内部 IP 地址,因为您在启动服务器时使用命令行上的 -b 开关。因此,当您的远程 JMS 客户端查找 jms/RemoteConnectionFactory 时,它将获得一个指向 internal IP 地址的连接工厂,这将失败。

要解决此问题,您需要将 RemoteConnectionFactory 配置为使用外部 IP 地址。

首先,定义一个新的interface,例如:

<interface name="external">
    <inet-address value="${jboss.bind.address.external:<<EXTERNAL-IP>>}"/>
</interface>

您可以在此处配置实际的外部 IP 地址,也可以在命令行中使用 -Djboss.bind.address.external=&lt;&lt;EXTERNAL-IP&gt;&gt; 传递它。

其次,定义一个新的socket-binding来使用这个接口:

<socket-binding name="external" interface="external" port="${jboss.http.port:8080}"/>

第三,定义一个新的http-connector来使用这个socket-binding

<http-connector name="http-connector-external" socket-binding="external" endpoint="http-acceptor"/>

第四,将RemoteConnectionFactory改成使用这个http-connector

<connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector-external"/>

【讨论】:

  • 我按照上面提到的步骤操作,但是 jboss 在启动过程中抛出异常。异常说“无法解析外部接口”
  • 如果我在接口部分提供内部 IP 地址(私有 IP)而不是外部 IP(公共 IP),它不会引发任何异常,但我收到问题中最初发布的错误.我必须在网络级别进行修改吗?
  • 问题已解决,NAT 配置错误,我们需要更改。谢谢。我将提出一个关于启用安全性的新问题,因为我无法解决该区域并在standalone-full.xml 中禁用安全性
  • 仅供参考,我必须添加一个新的出站套接字绑定条目,并按照此处的建议在 http 连接器中使用新创建的套接字绑定:stackoverflow.com/questions/42757922/…
猜你喜欢
  • 1970-01-01
  • 2020-04-20
  • 1970-01-01
  • 2019-03-08
  • 2022-08-10
  • 2019-01-24
  • 2015-03-28
  • 1970-01-01
  • 2018-11-24
相关资源
最近更新 更多