java.net.UnknownHostException: localhost
问题描述:
今天在配置jms的时候,修改了jms的配置文件:<broker brokerName="localhost" useJmx="false" xmlns= "http://activemq.org/config/1.0" >
但是在启动jms的时候,报了一个错误:
Loading message broker from: xbean:activemq.xml INFO BrokerService - ActiveMQ 4.1.1 JMS Message Broker (localhost) is starting INFO BrokerService - For help or more information please see: http://incubator.apache.org/activemq/ INFO JDBCPersistenceAdapter - Database driver recognized: [apache_derby_embedded_jdbc_driver] INFO DefaultDatabaseLocker - Attempting to acquire the exclusive lock to become the Master broker INFO DefaultDatabaseLocker - Becoming the master on dataSource: org.apache.derby.jdbc.EmbeddedDataSource@31ca9394 INFO JournalPersistenceAdapter - Journal Recovery Started from: Active Journal: using 5 x 20.0 Megs at: /home/dhcp2/lcdmp2_web/apache-activemq-4.1.1/activemq-data/journal INFO JournalPersistenceAdapter - Journal Recovered: 0 message(s) in transactions recovered. ERROR BrokerService - Failed to start ActiveMQ JMS Message Broker. Reason: java.net.UnknownHostException: localhost java.net.UnknownHostException: localhost at java.net.ResolverCache.getHostByName(libgcj.so.7rh) at java.net.InetAddress.getAllByName(libgcj.so.7rh) at java.net.InetAddress.getByName(libgcj.so.7rh) at org.apache.activemq.transport.tcp.TcpTransportServer.bind(TcpTransportServer.java:76) at org.apache.activemq.transport.tcp.TcpTransportFactory.doBind(TcpTransportFactory.java:56) at org.apache.activemq.transport.TransportFactory.bind(TransportFactory.java:109) at org.apache.activemq.broker.TransportConnector.createTransportServer(TransportConnector.java:270) at org.apache.activemq.broker.TransportConnector.getServer(TransportConnector.java:122) at org.apache.activemq.broker.TransportConnector.start(TransportConnector.java:220) at org.apache.activemq.broker.BrokerService.startTransportConnector(BrokerService.java:1519) at org.apache.activemq.broker.BrokerService.startAllConnectors(BrokerService.java:1471) at org.apache.activemq.broker.BrokerService.start(BrokerService.java:402) at org.apache.activemq.xbean.XBeanBrokerService.afterPropertiesSet(XBeanBrokerService.java:47) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1062) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1029) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:420) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:290) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348) at org.apache.xbean.spring.context.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:161) at org.apache.xbean.spring.context.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:51) at org.apache.activemq.xbean.XBeanBrokerFactory.createBroker(XBeanBrokerFactory.java:41) at org.apache.activemq.broker.BrokerFactory.createBroker(BrokerFactory.java:57) at org.apache.activemq.console.command.StartCommand.startBroker(StartCommand.java:82) at org.apache.activemq.console.command.StartCommand.runTask(StartCommand.java:47) at org.apache.activemq.console.command.AbstractCommand.execute(AbstractCommand.java:50) at org.apache.activemq.console.command.ShellCommand.runTask(ShellCommand.java:82) at org.apache.activemq.console.command.AbstractCommand.execute(AbstractCommand.java:50) at org.apache.activemq.console.command.ShellCommand.main(ShellCommand.java:46) at java.lang.reflect.Method.invoke(libgcj.so.7rh) at org.apache.activemq.console.Main.runTaskClass(Main.java:202) at org.apache.activemq.console.Main.main(Main.java:91) ERROR: java.lang.RuntimeException: Failed to execute start task. Reason: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.activemq.xbean.XBeanBrokerService' defined in class path resource [activemq.xml]: Invocation of init method failed; nested exception is java.net.UnknownHostException: localhost java.lang.RuntimeException: Failed to execute start task. Reason: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.activemq.xbean.XBeanBrokerService' defined in class path resource [activemq.xml]: Invocation of init method failed; nested exception is java.net.UnknownHostException: localhost at org.apache.activemq.console.command.StartCommand.runTask(StartCommand.java:70) at org.apache.activemq.console.command.AbstractCommand.execute(AbstractCommand.java:50) at org.apache.activemq.console.command.ShellCommand.runTask(ShellCommand.java:82) at org.apache.activemq.console.command.AbstractCommand.execute(AbstractCommand.java:50) at org.apache.activemq.console.command.ShellCommand.main(ShellCommand.java:46) at java.lang.reflect.Method.invoke(libgcj.so.7rh) at org.apache.activemq.console.Main.runTaskClass(Main.java:202) at org.apache.activemq.console.Main.main(Main.java:91) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.activemq.xbean.XBeanBrokerService' defined in class path resource [activemq.xml]: Invocation of init method failed; nested exception is java.net.UnknownHostException: localhost at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1032) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:420) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:290) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348) at org.apache.xbean.spring.context.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:161) at org.apache.xbean.spring.context.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:51) at org.apache.activemq.xbean.XBeanBrokerFactory.createBroker(XBeanBrokerFactory.java:41) at org.apache.activemq.broker.BrokerFactory.createBroker(BrokerFactory.java:57) at org.apache.activemq.console.command.StartCommand.startBroker(StartCommand.java:82) at org.apache.activemq.console.command.StartCommand.runTask(StartCommand.java:47) ...7 more Caused by: java.net.UnknownHostException: localhost at java.net.ResolverCache.getHostByName(libgcj.so.7rh) at java.net.InetAddress.getAllByName(libgcj.so.7rh) at java.net.InetAddress.getByName(libgcj.so.7rh) at org.apache.activemq.transport.tcp.TcpTransportServer.bind(TcpTransportServer.java:76) at org.apache.activemq.transport.tcp.TcpTransportFactory.doBind(TcpTransportFactory.java:56) at org.apache.activemq.transport.TransportFactory.bind(TransportFactory.java:109) at org.apache.activemq.broker.TransportConnector.createTransportServer(TransportConnector.java:270) at org.apache.activemq.broker.TransportConnector.getServer(TransportConnector.java:122) at org.apache.activemq.broker.TransportConnector.start(TransportConnector.java:220) at org.apache.activemq.broker.BrokerService.startTransportConnector(BrokerService.java:1519) at org.apache.activemq.broker.BrokerService.startAllConnectors(BrokerService.java:1471) at org.apache.activemq.broker.BrokerService.start(BrokerService.java:402) at org.apache.activemq.xbean.XBeanBrokerService.afterPropertiesSet(XBeanBrokerService.java:47) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1062) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1029) ...20 more ERROR: java.lang.Exception: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.activemq.xbean.XBeanBrokerService' defined in class path resource [activemq.xml]: Invocation of init method failed; nested exception is java.net.UnknownHostException: localhost java.lang.Exception: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.activemq.xbean.XBeanBrokerService' defined in class path resource [activemq.xml]: Invocation of init method failed; nested exception is java.net.UnknownHostException: localhost at org.apache.activemq.console.command.StartCommand.runTask(StartCommand.java:71) at org.apache.activemq.console.command.AbstractCommand.execute(AbstractCommand.java:50) at org.apache.activemq.console.command.ShellCommand.runTask(ShellCommand.java:82) at org.apache.activemq.console.command.AbstractCommand.execute(AbstractCommand.java:50) at org.apache.activemq.console.command.ShellCommand.main(ShellCommand.java:46) at java.lang.reflect.Method.invoke(libgcj.so.7rh) at org.apache.activemq.console.Main.runTaskClass(Main.java:202) at org.apache.activemq.console.Main.main(Main.java:91) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.activemq.xbean.XBeanBrokerService' defined in class path resource [activemq.xml]: Invocation of init method failed; nested exception is java.net.UnknownHostException: localhost at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1032) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:420) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:290) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348) at org.apache.xbean.spring.context.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:161) at org.apache.xbean.spring.context.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:51) at org.apache.activemq.xbean.XBeanBrokerFactory.createBroker(XBeanBrokerFactory.java:41) at org.apache.activemq.broker.BrokerFactory.createBroker(BrokerFactory.java:57) at org.apache.activemq.console.command.StartCommand.startBroker(StartCommand.java:82) at org.apache.activemq.console.command.StartCommand.runTask(StartCommand.java:47) ...7 more Caused by: java.net.UnknownHostException: localhost at java.net.ResolverCache.getHostByName(libgcj.so.7rh) at java.net.InetAddress.getAllByName(libgcj.so.7rh) at java.net.InetAddress.getByName(libgcj.so.7rh) at org.apache.activemq.transport.tcp.TcpTransportServer.bind(TcpTransportServer.java:76) at org.apache.activemq.transport.tcp.TcpTransportFactory.doBind(TcpTransportFactory.java:56) at org.apache.activemq.transport.TransportFactory.bind(TransportFactory.java:109) at org.apache.activemq.broker.TransportConnector.createTransportServer(TransportConnector.java:270) at org.apache.activemq.broker.TransportConnector.getServer(TransportConnector.java:122) at org.apache.activemq.broker.TransportConnector.start(TransportConnector.java:220) at org.apache.activemq.broker.BrokerService.startTransportConnector(BrokerService.java:1519) at org.apache.activemq.broker.BrokerService.startAllConnectors(BrokerService.java:1471) at org.apache.activemq.broker.BrokerService.start(BrokerService.java:402) at org.apache.activemq.xbean.XBeanBrokerService.afterPropertiesSet(XBeanBrokerService.java:47) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1062) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1029) ...20 more INFO BrokerService - ActiveMQ Message Broker (localhost, null) is shutting down INFO NetworkConnector - Network Connector default-nc Stopped INFO TransportConnector - Connector openwire Stopped INFO BrokerService - ActiveMQ JMS Message Broker (localhost, null) stopped
问题分析:
根据日志java.net.UnknownHostException: localhost判断,应该是由于服务器无法正确解析localhost造成的
[123@test conf]$ ping localhost ping: unknown host localhost [123@test conf]$ ping 127.0.0.1 PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.020 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.026 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.023 ms
根据日志Error creating bean with name 'org.apache.activemq.xbean.XBeanBrokerService' defined in class path resource [activemq.xml]判断,应该是activemq.xml文件中的BrokerService配置出错
解决方式:
一:不使用localhost而使用127.0.0.1代替
二:修改主机映射
[root@test test]# vi /etc/hosts
在打开的文件中增加下面一行(保证新增的这条为以127.0.0.1开头的第一个):
127.0.0.1 localhost.localdomain localhost
出现如下测试结果说明配置成功:
[root@test test]# ping localhost PING localhost.localdomain (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=64 time=0.019 ms 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=2 ttl=64 time=0.020 ms 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=3 ttl=64 time=0.024 ms 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=4 ttl=64 time=0.020 ms 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=5 ttl=64 time=0.018 ms --- localhost.localdomain ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4009ms rtt min/avg/max/mdev = 0.018/0.020/0.024/0.003 ms
此时可以在配置文件中使用localhost,服务器会将localhost解析为127.0.0.1