首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jboss.naming.context.java.jboss.exported.jboss WildFly 18 NameNotFoundException: jboss -服务NameNotFoundException

jboss.naming.context.java.jboss.exported.jboss WildFly 18 NameNotFoundException: jboss -服务NameNotFoundException
EN

Stack Overflow用户
提问于 2020-08-19 08:35:25
回答 1查看 3.1K关注 0票数 2

我跟随Helloworld JMS示例从WildFly快速开始编写我自己的Java。当我运行Java程序时,我得到了下面的异常。有人能指导我如何访问将消息发送到JMS队列吗?我对JMS和WildFly还不熟悉。

代码语言:javascript
复制
javax.naming.NameNotFoundException: jboss -- service jboss.naming.context.java.jboss.exported.jboss
    at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:193)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:189)
    at org.wildfly.naming.client.remote.RemoteServerTransport.handleLookup(RemoteServerTransport.java:200)
    at org.wildfly.naming.client.remote.RemoteServerTransport$1.handleMessage(RemoteServerTransport.java:120)
    at org.jboss.remoting3.remote.RemoteConnectionChannel.lambda$receiveMessage$2(RemoteConnectionChannel.java:361)
    at org.jboss.remoting3.EndpointImpl$TrackingExecutor.lambda$execute$0(EndpointImpl.java:991)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1348)
    at java.lang.Thread.run(Thread.java:745)

代码如下:

代码语言:javascript
复制
package com.test;
    
import java.util.Properties;

import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;


public class JmsConsumerWithUser {

    // Set up all the default values
    private static final Logger log = Logger.getLogger(JmsConsumerWithUser.class.getName());
    private static final String DEFAULT_MESSAGE = "Hello, World!";
    private static final String DEFAULT_CONNECTION_FACTORY = "jboss/exported/jms/RemoteConnectionFactory";
   // private static final String DEFAULT_CONNECTION_FACTORY = " java:jboss/exported/jms/RemoteConnectionFactory";
   // private static final String DEFAULT_CONNECTION_FACTORY ="java:/JmsXA";
   
    private static final String DEFAULT_DESTINATION = "java:/jms/queue/test";
    private static final String DEFAULT_MESSAGE_COUNT = "1";
    private static final String DEFAULT_USERNAME = "adminuser";
    private static final String DEFAULT_PASSWORD = "Thbs123!";
    private static final String INITIAL_CONTEXT_FACTORY = "org.wildfly.naming.client.WildFlyInitialContextFactory";
    private static final String PROVIDER_URL = "http-remoting://127.0.0.1:8080";

    public static void main(String[] args) {

        Context namingContext = null;

        try {
            String userName = System.getProperty("username", DEFAULT_USERNAME);
            String password = System.getProperty("password", DEFAULT_PASSWORD);

            // Set up the namingContext for the JNDI lookup
            final Properties env = new Properties();
            env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
            env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
            env.put(Context.SECURITY_PRINCIPAL, userName);
            env.put(Context.SECURITY_CREDENTIALS, password);
            namingContext = new InitialContext(env);

            // Perform the JNDI lookups
            String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
            log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
            ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.lookup(connectionFactoryString);
            log.info("Found connection factory \"" + connectionFactoryString + "\" in JNDI");

            String destinationString = System.getProperty("destination", DEFAULT_DESTINATION);
            log.info("Attempting to acquire destination \"" + destinationString + "\"");
            Destination destination = (Destination) namingContext.lookup(destinationString);
            log.info("Found destination \"" + destinationString + "\" in JNDI");

            int count = Integer.parseInt(System.getProperty("message.count", DEFAULT_MESSAGE_COUNT));
            String content = System.getProperty("message.content", DEFAULT_MESSAGE);

            try (JMSContext context = connectionFactory.createContext(userName, password)) {
                log.info("Sending " + count + " messages with content: " + content);
                // Send the specified number of messages
                for (int i = 0; i < count; i++) {
                    context.createProducer().send(destination, content);
                }

                // Create the JMS consumer
                JMSConsumer consumer = context.createConsumer(destination);
                // Then receive the same number of messages that were sent
                for (int i = 0; i < count; i++) {
                    String text = consumer.receiveBody(String.class, 5000);
                    log.info("Received message with content " + text);
                }
            }
        } catch (NamingException e) {
            e.printStackTrace();
           // log.severe(e.getMessage());
        } finally {
            if (namingContext != null) {
                try {
                    namingContext.close();
                } catch (NamingException e) {
                    e.printStackTrace();
                  //  log.severe(e.getMessage());
                }
            }
        }
    }    
}

我的独立-Ful.xml如下所示:

代码语言:javascript
复制
<subsystem xmlns="urn:jboss:domain:messaging-activemq:8.0">
    <server name="default">
        <statistics enabled="${wildfly.messaging-activemq.statistics-enabled:${wildfly.statistics-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="ExpiryQueue" entries="java:/jms/queue/ExpiryQueue"/>
            <jms-queue name="DLQ" entries="java:/jms/queue/DLQ"/>
        <jms-queue name="Test" entries="java:/jms/queue/test"/>
        <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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-19 14:14:19

默认情况下,在WildFly中创建的JNDI绑定对远程客户端不可用。为了将JNDI绑定公开给远程客户端,它们需要在一个特殊的名称空间- java:jboss/exported/中。但是,从远程客户端的角度来看,java:jboss/exported/并不存在,因此它只是在查找时使用了java:jboss/exported/之后的内容。

例如,connection-factory "RemoteConnectionFactory“被配置为绑定到java:jboss/exported/jms/RemoteConnectionFactory

代码语言:javascript
复制
<connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector"/>

但是,远程客户端会查找jms/RemoteConnectionFactory,例如:

代码语言:javascript
复制
ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.lookup("jms/RemoteConnectionFactory");            

在您的代码和配置中会出现这种情况。您正在使用java:jboss/exported/查找连接工厂,这是不正确的,而且您的队列配置没有使用java:jboss/exported/。应该是这样的:

代码语言:javascript
复制
<jms-queue name="Test" entries="java:jboss/exported/jms/queue/test"/>

那么您的代码实际上将如下所示:

代码语言:javascript
复制
Destination destination = (Destination) namingContext.lookup("jms/queue/test");

顺便说一句,如果您只是简单地将代码和配置留在最初的快速启动程序中,那么一切都会正常工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63483080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档