WildFly 18 名称未找到异常: jboss — service jboss.naming.context.java.jboss.exported.jboss

huangapple go评论84阅读模式
英文:

WildFly 18 NameNotFoundException: jboss -- service jboss.naming.context.java.jboss.exported.jboss

问题

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 {

    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_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);

            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);

            String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
            ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.lookup(connectionFactoryString);

            String destinationString = System.getProperty("destination", DEFAULT_DESTINATION);
            Destination destination = (Destination) namingContext.lookup(destinationString);

            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)) {
                for (int i = 0; i < count; i++) {
                    context.createProducer().send(destination, content);
                }

                JMSConsumer consumer = context.createConsumer(destination);
                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();
        } finally {
            if (namingContext != null) {
                try {
                    namingContext.close();
                } catch (NamingException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
英文:

I have followed the Helloworld JMS example from the WildFly quickstarts to write my own Java client. When I am running the Java program I am getting the exception below. Can anyone guide me on how to access to send message to the JMS queue? I am new to JMS and WildFly.

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)

Code is below:

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 = &quot;Hello, World!&quot;;
    private static final String DEFAULT_CONNECTION_FACTORY = &quot;jboss/exported/jms/RemoteConnectionFactory&quot;;
   // private static final String DEFAULT_CONNECTION_FACTORY = &quot; java:jboss/exported/jms/RemoteConnectionFactory&quot;;
   // private static final String DEFAULT_CONNECTION_FACTORY =&quot;java:/JmsXA&quot;;
   
    private static final String DEFAULT_DESTINATION = &quot;java:/jms/queue/test&quot;;
    private static final String DEFAULT_MESSAGE_COUNT = &quot;1&quot;;
    private static final String DEFAULT_USERNAME = &quot;adminuser&quot;;
    private static final String DEFAULT_PASSWORD = &quot;Thbs123!&quot;;
    private static final String INITIAL_CONTEXT_FACTORY = &quot;org.wildfly.naming.client.WildFlyInitialContextFactory&quot;;
    private static final String PROVIDER_URL = &quot;http-remoting://127.0.0.1:8080&quot;;

    public static void main(String[] args) {

        Context namingContext = null;

        try {
            String userName = System.getProperty(&quot;username&quot;, DEFAULT_USERNAME);
            String password = System.getProperty(&quot;password&quot;, 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(&quot;connection.factory&quot;, DEFAULT_CONNECTION_FACTORY);
            log.info(&quot;Attempting to acquire connection factory \&quot;&quot; + connectionFactoryString + &quot;\&quot;&quot;);
            ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.lookup(connectionFactoryString);
            log.info(&quot;Found connection factory \&quot;&quot; + connectionFactoryString + &quot;\&quot; in JNDI&quot;);

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

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

            try (JMSContext context = connectionFactory.createContext(userName, password)) {
                log.info(&quot;Sending &quot; + count + &quot; messages with content: &quot; + content);
                // Send the specified number of messages
                for (int i = 0; i &lt; 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 &lt; count; i++) {
                    String text = consumer.receiveBody(String.class, 5000);
                    log.info(&quot;Received message with content &quot; + 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());
                }
            }
        }
    }    
}

My standalone-full.xml is below:

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

答案1

得分: 2

在WildFly中创建的JNDI绑定默认情况下对远程客户端不可用。为了将JNDI绑定暴露给远程客户端,它们需要位于特殊命名空间中 - java:jboss/exported/。然而,从远程客户端的角度来看,java:jboss/exported/并不存在,因此它只是使用在其查找中跟在java:jboss/exported/之后的部分。

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

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

然而,远程客户端会在jms/RemoteConnectionFactory进行查找,例如:

ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.lookup("jms/RemoteConnectionFactory");            

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

<jms-queue name="Test" entries="java:jboss/exported/jms/queue/test"/>

然后您的代码看起来应该是这样的:

Destination destination = (Destination) namingContext.lookup("jms/queue/test");

顺便说一下,如果您只是保持代码和配置与原始快速入门一样,一切都会正常工作。

英文:

JNDI bindings created in WildFly are not available to remote clients by default. In order to expose JNDI bindings to remote clients they need to be in a special namespace - java:jboss/exported/. However, from a remote client's perspective the java:jboss/exported/ doesn't exist so it simply uses what comes after java:jboss/exported/ in its lookup.

For example, the connection-factory "RemoteConnectionFactory" is configured to bound to java:jboss/exported/jms/RemoteConnectionFactory:

&lt;connection-factory name=&quot;RemoteConnectionFactory&quot; entries=&quot;java:jboss/exported/jms/RemoteConnectionFactory&quot; connectors=&quot;http-connector&quot;/&gt;

However, a remote client would look it up at jms/RemoteConnectionFactory, e.g.:

ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.lookup(&quot;jms/RemoteConnectionFactory&quot;);            

You've got this mixed up in your code and configuration. You're looking up the connection factory using java:jboss/exported/ which is incorrect and your queue configuration isn't using java:jboss/exported/. It should be like this:

&lt;jms-queue name=&quot;Test&quot; entries=&quot;java:jboss/exported/jms/queue/test&quot;/&gt;

Then your code would effectively look like this:

Destination destination = (Destination) namingContext.lookup(&quot;jms/queue/test&quot;);

Incidentally if you simply left the code and configuration alone from the original quickstart everything would work.

huangapple
  • 本文由 发表于 2020年8月19日 16:35:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63483080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定