无法从独立 java 客户端应用程序查找 Wildfly 8.2.0 的远程连接工厂

Unable to lookup Remote Connection Factory of Wildfly 8.2.0 from Standalone java client Application

我在 Wildfly 8.2.0 中有一个 Queue 并部署了 ear with listener。然后我正在尝试 post 从客户端程序向远程队列发送文本消息。在这种情况下,无法查找远程连接工厂。附上程序和错误。

Wildfly 上的 MDB 侦听器

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "jms/queue/mdbtest")})
public class ExampleQueueMDB implements MessageListener {

    @Override
    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            try {
                System.out.println("----------------");
                System.out.println("Received: "
                        + ((TextMessage) message).getText());
                System.out.println("----------------");
            } catch (JMSException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("----------------");
            System.out.println("Received: " + message);
            System.out.println("----------------");
        }
    }

}

Java 客户端程序

import java.util.logging.Logger;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

public class HelloWorldJMSClientSample {
    private static final Logger log = Logger.getLogger(HelloWorldJMSClient.class.getName());

    // Set up all the default values
    private static final String DEFAULT_MESSAGE = "Hello, World!";
    private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
    private static final String DEFAULT_DESTINATION = "jms/queue/mdbtest";
    private static final String DEFAULT_MESSAGE_COUNT = "1";
    private static final String DEFAULT_USERNAME = "user";
    private static final String DEFAULT_PASSWORD = "pass";
    private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
    //private static final String PROVIDER_URL = "http-remoting://localhost:8080";
    private static final String PROVIDER_URL = "http-remoting://localhost:8080";

    public static void main(String[] args) throws Exception {

        ConnectionFactory connectionFactory = null;
        Connection connection = null;
        Session session = null;
        MessageProducer producer = null;
        MessageConsumer consumer = null;
        Destination destination = null;
        TextMessage message = null;
        Context context = null;

        try {
            // Set up the context for the JNDI lookup
            final Properties env = new Properties();
            env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
            env.put(Context.PROVIDER_URL, PROVIDER_URL);
            env.put(Context.SECURITY_PRINCIPAL, DEFAULT_USERNAME);
            env.put(Context.SECURITY_CREDENTIALS, DEFAULT_PASSWORD);

        env.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
            context = 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) context.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) context.lookup(destinationString);
            log.info("Found destination \"" + destinationString + "\" in JNDI");

            // Create the JMS connection, session, producer, and consumer
            connection = connectionFactory.createConnection(DEFAULT_USERNAME,DEFAULT_PASSWORD);
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            producer = session.createProducer(destination);
           // consumer = session.createConsumer(destination);
            connection.start();

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

            log.info("Sending " + count + " messages with content: " + content);

            // Send the specified number of messages
            for (int i = 0; i < count; i++) {
                message = session.createTextMessage(content);
                producer.send(message);
            }

        } catch (Exception e) {
            log.severe(e.getMessage());
            throw e;
        } finally {
            if (context != null) {
                context.close();
            }

            // closing the connection takes care of the session, producer, and consumer
            if (connection != null) {
                connection.close();
            }
        }
    }
}

错误

SEVERE: Failed to create session factory
Exception in thread "main" javax.jms.JMSException: Failed to create session factory
    at org.hornetq.jms.client.HornetQConnectionFactory.createConnectionInternal(HornetQConnectionFactory.java:615)
    at org.hornetq.jms.client.HornetQConnectionFactory.createConnection(HornetQConnectionFactory.java:121)
    at HelloWorldJMSClientSample.main(HelloWorldJMSClientSample.java:62)
Caused by: java.lang.IllegalStateException: The following keys are invalid for configuring a connector: http-upgrade-endpoint, http-upgrade-enabled
    at org.hornetq.core.client.impl.ClientSessionFactoryImpl.checkTransportKeys(ClientSessionFactoryImpl.java:1227)
    at org.hornetq.core.client.impl.ClientSessionFactoryImpl.<init>(ClientSessionFactoryImpl.java:181)
    at org.hornetq.core.client.impl.ServerLocatorImpl.createSessionFactory(ServerLocatorImpl.java:589)
    at org.hornetq.jms.client.HornetQConnectionFactory.createConnectionInternal(HornetQConnectionFactory.java:611)
    ... 2 more

在第 62 行创建连接时出现上述错误,

connection = connectionFactory.createConnection(DEFAULT_USERNAME,DEFAULT_PASSWORD);

我不确定为什么这不起作用。希望这是一个简单的案例。如果有不同的方法来查找 RemoteConnectionFactory,请告诉我。谢谢。

更新:

方法二: 如果我使用下面的配置,能够调用任何 ejb 方法,但仍然会抛出 RemoteConnectionFactory 查找错误。

// Set up the context for the JNDI lookup
final Properties env = new Properties();
env.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
env.put("endpoint.name", "client-endpoint");
env.put("remote.connections","default");
env.put("remote.connection.default.port","8080");
env.put("remote.connection.default.host","localhost");
env.put("remote.connection.default.username", DEFAULT_USERNAME);
env.put("remote.connection.default.password", DEFAULT_PASSWORD);
env.put("org.jboss.ejb.client.scoped.context",true);
context = new InitialContext(env);

上述更改后出错,

INFO: Attempting to acquire connection factory "jms/RemoteConnectionFactory"
SEVERE: Receive timed out
Exception in thread "main" javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out]
    at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1317)
    at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1446)
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:594)
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at HelloWorldJMSClientSample.main(HelloWorldJMSClientSample.java:56)
Caused by: java.net.SocketTimeoutException: Receive timed out
    at java.net.PlainDatagramSocketImpl.receive0(Native Method)
    at java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:146)
    at java.net.DatagramSocket.receive(DatagramSocket.java:816)
    at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1287)
    ... 5 more

查找时出错,第 56 行,

connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryString);

请提供此问题的任何解决方案。谢谢。

https://developer.jboss.org/thread/265919 找到答案。

由于我在使用infinispan,所以我使用了Wildfly 8.2.0 的infinispan 版本(6.0.2) 作为客户端程序。但是使用 HornetQ 2.4.5 的 Wildfly 和具有 HornetQ 2.2.7 的客户端。我已经在客户端更改为 HornetQ 2.4.5,一切正常。