JBoss-A-MQ在JMS消息传递应用程序中遇到了UnknownHostException

时间:2015-03-16 13:07:56

标签: jboss jms jndi unknown-host jboss-amq

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

应用: 公共类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的新手。非常感谢,如果有人能指出我哪里出错了。

1 个答案:

答案 0 :(得分: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