1

我有一个示例 JMS 消息传递应用程序,如下面的代码片段所示。当我执行程序时(特别是在启动连接时),我得到一个 UnknowHostException。异常的原因是clientid属性获取为空。

应用程序:公共类 MessagingTestApp {

private static final String MY_JMS_CONNECTION_FACTORY_NAME = "myJmsFactory";
private static final String EXCHANGE_NAME = "topicExchange";
private static final String JNDI_PROPERTIES_FILE_NAME = "jndi.properties";
private static final String COULD_NOT_LOAD_JNDI_PROPERTIES_MESSAGE = "Could not load JNDI properties....";
private static final boolean NON_TRANSACTED = false;
private static final Logger LOG = Logger.getLogger(MessagingTestApp.class);

public  MessagingTestApp() {}

public static void main(String[] args) {
    MessagingTestApp messagingTestApp = new MessagingTestApp();
    messagingTestApp.runTest();
}

private void runTest() {
    try {
        Properties properties = new Properties();
        properties.load(loadPropertiesFile());
        Context context = new InitialContext(properties);
        ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup(MY_JMS_CONNECTION_FACTORY_NAME);
        Connection connection = connectionFactory.createConnection();

        connection.start();
        Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        Destination destination = (Destination) context.lookup(EXCHANGE_NAME);
        MessageProducer messageProducer = session.createProducer(destination);
        MessageConsumer messageConsumer = session.createConsumer(destination);

        TextMessage message = session.createTextMessage("Hello JMS!");
        messageProducer.send(message);

        message = (TextMessage) messageConsumer.receive();
        System.out.println(message.getText());

        connection.close();
        context.close();
    } catch (Exception e) {
        LOG.error(e);
        e.printStackTrace();
    }
}

private InputStream loadPropertiesFile() {
    Thread currentThread = Thread.currentThread();
    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
    InputStream propertiesStream = contextClassLoader.getResourceAsStream(JNDI_PROPERTIES_FILE_NAME);
    if (propertiesStream != null) {
        return propertiesStream;
    } else {
        System.out.println(COULD_NOT_LOAD_JNDI_PROPERTIES_MESSAGE);
        return null;
    }
}
}

JNDI 属性文件:

java.naming.factory.initial = org.apache.qpid.amqp_1_0.jms.jndi.PropertiesFileInitialContextFactory
java.naming.provider.url = src/main/resources/jndi.properties


connectionfactory.myJmsFactory = amqp://admin:admin@clientid/test?

brokerlist = 'tcp://localhost:5672'

destination.topicExchange = amq.topic

堆栈跟踪:

javax.jms.JMSException: java.net.UnknownHostException: clientid
    at org.apache.qpid.amqp_1_0.jms.impl.ConnectionImpl.connect(ConnectionImpl.java:112)
    at org.apache.qpid.amqp_1_0.jms.impl.ConnectionImpl.start(ConnectionImpl.java:266)
    at com.adc.efg.MessagingTestApp.runTest(MessagingTestApp.java:39)
    at com.adc.efg.MessagingTestApp.main(MessagingTestApp.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.apache.qpid.amqp_1_0.client.ConnectionException: java.net.UnknownHostException: clientid
    at org.apache.qpid.amqp_1_0.client.Connection.<init>(Connection.java:271)
    at org.apache.qpid.amqp_1_0.client.Connection.<init>(Connection.java:135)
    at org.apache.qpid.amqp_1_0.jms.impl.ConnectionImpl.connect(ConnectionImpl.java:105)
    ... 8 more
Caused by: java.net.UnknownHostException: clientid
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at org.apache.qpid.amqp_1_0.client.Connection.<init>(Connection.java:159)
    ... 10 more

我是 JBoss-AMQ 和 JMS 的新手。如果有人能指出我哪里出错了,我将不胜感激。

4

1 回答 1

0

问题出在 JNDI 属性文件中。它应该像下面,

java.naming.factory.initial = org.apache.qpid.amqp_1_0.jms.jndi.PropertiesFileInitialContextFactory
java.naming.provider.url = src/main/resources/jndi.properties


connectionfactory.myJmsFactory = amqp://admin:admin@localhost/test?

brokerlist = 'tcp://localhost:5672'

destination.topicExchange = amq.topic
于 2015-03-17T08:27:54.700 回答