AMQ-2965 - Patched to allow ActiveMQ to start up when DNS resolution is unavailable

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1028143 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Bruce Snyder 2010-10-27 23:56:40 +00:00
parent c290a17460
commit 8a1f994bf5
7 changed files with 51 additions and 7 deletions

View File

@ -197,7 +197,7 @@ public class BrokerService implements Service {
static { static {
String localHostName = "localhost"; String localHostName = "localhost";
try { try {
localHostName = java.net.InetAddress.getLocalHost().getHostName(); localHostName = InetAddressUtil.getLocalHostName();
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
LOG.error("Failed to resolve localhost"); LOG.error("Failed to resolve localhost");
} }

View File

@ -64,6 +64,7 @@ import org.apache.activemq.thread.TaskRunnerFactory;
import org.apache.activemq.usage.SystemUsage; import org.apache.activemq.usage.SystemUsage;
import org.apache.activemq.util.BrokerSupport; import org.apache.activemq.util.BrokerSupport;
import org.apache.activemq.util.IdGenerator; import org.apache.activemq.util.IdGenerator;
import org.apache.activemq.util.InetAddressUtil;
import org.apache.activemq.util.LongSequenceGenerator; import org.apache.activemq.util.LongSequenceGenerator;
import org.apache.activemq.util.ServiceStopper; import org.apache.activemq.util.ServiceStopper;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -615,7 +616,7 @@ public class RegionBroker extends EmptyBroker {
public String getBrokerName() { public String getBrokerName() {
if (brokerName == null) { if (brokerName == null) {
try { try {
brokerName = java.net.InetAddress.getLocalHost().getHostName().toLowerCase(); brokerName = InetAddressUtil.getLocalHostName().toLowerCase();
} catch (Exception e) { } catch (Exception e) {
brokerName = "localhost"; brokerName = "localhost";
} }

View File

@ -40,6 +40,7 @@ import org.apache.activemq.Service;
import org.apache.activemq.transport.Transport; import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.TransportLoggerFactory; import org.apache.activemq.transport.TransportLoggerFactory;
import org.apache.activemq.transport.TransportThreadSupport; import org.apache.activemq.transport.TransportThreadSupport;
import org.apache.activemq.util.InetAddressUtil;
import org.apache.activemq.util.IntrospectionSupport; import org.apache.activemq.util.IntrospectionSupport;
import org.apache.activemq.util.ServiceStopper; import org.apache.activemq.util.ServiceStopper;
import org.apache.activemq.wireformat.WireFormat; import org.apache.activemq.wireformat.WireFormat;
@ -400,7 +401,7 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
protected String resolveHostName(String host) throws UnknownHostException { protected String resolveHostName(String host) throws UnknownHostException {
if (isUseLocalHost()) { if (isUseLocalHost()) {
String localName = InetAddress.getLocalHost().getHostName(); String localName = InetAddressUtil.getLocalHostName();
if (localName != null && localName.equals(host)) { if (localName != null && localName.equals(host)) {
return "localhost"; return "localhost";
} }

View File

@ -43,6 +43,7 @@ import org.apache.activemq.transport.TransportLoggerFactory;
import org.apache.activemq.transport.TransportServer; import org.apache.activemq.transport.TransportServer;
import org.apache.activemq.transport.TransportServerThreadSupport; import org.apache.activemq.transport.TransportServerThreadSupport;
import org.apache.activemq.util.IOExceptionSupport; import org.apache.activemq.util.IOExceptionSupport;
import org.apache.activemq.util.InetAddressUtil;
import org.apache.activemq.util.IntrospectionSupport; import org.apache.activemq.util.IntrospectionSupport;
import org.apache.activemq.util.ServiceListener; import org.apache.activemq.util.ServiceListener;
import org.apache.activemq.util.ServiceStopper; import org.apache.activemq.util.ServiceStopper;
@ -332,7 +333,7 @@ public class TcpTransportServer extends TransportServerThreadSupport implements
if (socket.isBound()) { if (socket.isBound()) {
if (socket.getInetAddress().isAnyLocalAddress()) { if (socket.getInetAddress().isAnyLocalAddress()) {
// make it more human readable and useful, an alternative to 0.0.0.0 // make it more human readable and useful, an alternative to 0.0.0.0
result = InetAddress.getLocalHost().getHostName(); result = InetAddressUtil.getLocalHostName();
} else { } else {
result = socket.getInetAddress().getCanonicalHostName(); result = socket.getInetAddress().getCanonicalHostName();
} }

View File

@ -39,6 +39,7 @@ import org.apache.activemq.transport.reliable.ExceptionIfDroppedReplayStrategy;
import org.apache.activemq.transport.reliable.ReplayBuffer; import org.apache.activemq.transport.reliable.ReplayBuffer;
import org.apache.activemq.transport.reliable.ReplayStrategy; import org.apache.activemq.transport.reliable.ReplayStrategy;
import org.apache.activemq.transport.reliable.Replayer; import org.apache.activemq.transport.reliable.Replayer;
import org.apache.activemq.util.InetAddressUtil;
import org.apache.activemq.util.IntSequenceGenerator; import org.apache.activemq.util.IntSequenceGenerator;
import org.apache.activemq.util.ServiceStopper; import org.apache.activemq.util.ServiceStopper;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -340,7 +341,7 @@ public class UdpTransport extends TransportThreadSupport implements Transport, S
} }
protected String resolveHostName(String host) throws UnknownHostException { protected String resolveHostName(String host) throws UnknownHostException {
String localName = InetAddress.getLocalHost().getHostName(); String localName = InetAddressUtil.getLocalHostName();
if (localName != null && isUseLocalHost()) { if (localName != null && isUseLocalHost()) {
if (localName.equals(host)) { if (localName.equals(host)) {
return "localhost"; return "localhost";

View File

@ -16,7 +16,6 @@
*/ */
package org.apache.activemq.util; package org.apache.activemq.util;
import java.net.InetAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
@ -51,7 +50,7 @@ public class IdGenerator {
if (canAccessSystemProps) { if (canAccessSystemProps) {
try { try {
hostName = InetAddress.getLocalHost().getHostName(); hostName = InetAddressUtil.getLocalHostName();
ServerSocket ss = new ServerSocket(0); ServerSocket ss = new ServerSocket(0);
stub = "-" + ss.getLocalPort() + "-" + System.currentTimeMillis() + "-"; stub = "-" + ss.getLocalPort() + "-" + System.currentTimeMillis() + "-";
Thread.sleep(100); Thread.sleep(100);

View File

@ -0,0 +1,41 @@
package org.apache.activemq.util;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressUtil {
/**
* When using the {@link java.net.InetAddress#getHostName()} method in an
* environment where neither a proper DNS lookup nor an <tt>/etc/hosts</tt>
* entry exists for a given host, the following exception will be thrown:
* <code>
* java.net.UnknownHostException: &lt;hostname&gt;: &lt;hostname&gt;
* at java.net.InetAddress.getLocalHost(InetAddress.java:1425)
* ...
* </code>
* Instead of just throwing an UnknownHostException and giving up, this
* method grabs a suitable hostname from the exception and prevents the
* exception from being thrown. If a suitable hostname cannot be acquired
* from the exception, only then is the <tt>UnknownHostException</tt> thrown.
*
* @return The hostname
* @throws UnknownHostException
* @see {@link java.net.InetAddress#getLocalHost()}
* @see {@link java.net.InetAddress#getHostName()}
*/
public static String getLocalHostName() throws UnknownHostException {
try {
return (InetAddress.getLocalHost()).getHostName();
} catch (UnknownHostException uhe) {
String host = uhe.getMessage(); // host = "hostname: hostname"
if (host != null) {
int colon = host.indexOf(':');
if (colon > 0) {
return host.substring(0, colon);
}
}
throw uhe;
}
}
}