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

时间:2015-11-03 09:32:20

标签: java jndi wildfly-8

我在Wildfly 8.2.0中有一个队列,并使用监听器部署了耳朵。然后我试图从客户端程序发送文本消息到远程队列。在这种情况下,无法查找远程连接工厂。附上程序和错误。

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

ERROR

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,请告诉我。感谢。

更新

方法2: 如果我使用下面的配置,能够调用任何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);

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

1 个答案:

答案 0 :(得分:0)

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

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