mirror of https://github.com/apache/activemq.git
Fix for AMQ-5652: IdGenerator not optimal in port restricted environments.
We now support configuring via system props activemq.idgenerator.hostname and activemq.idgenerator.localport which are used as the base part of GUIDs.
This commit is contained in:
parent
1c72579d7a
commit
e25a6aa8a1
|
@ -35,6 +35,8 @@ public class IdGenerator {
|
||||||
private String seed;
|
private String seed;
|
||||||
private final AtomicLong sequence = new AtomicLong(1);
|
private final AtomicLong sequence = new AtomicLong(1);
|
||||||
private int length;
|
private int length;
|
||||||
|
public static final String PROPERTY_IDGENERATOR_HOSTNAME ="activemq.idgenerator.hostname";
|
||||||
|
public static final String PROPERTY_IDGENERATOR_LOCALPORT ="activemq.idgenerator.localport";
|
||||||
public static final String PROPERTY_IDGENERATOR_PORT ="activemq.idgenerator.port";
|
public static final String PROPERTY_IDGENERATOR_PORT ="activemq.idgenerator.port";
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
@ -50,15 +52,26 @@ public class IdGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (canAccessSystemProps) {
|
if (canAccessSystemProps) {
|
||||||
|
|
||||||
|
hostName = System.getProperty(PROPERTY_IDGENERATOR_HOSTNAME);
|
||||||
|
int localPort = Integer.parseInt(System.getProperty(PROPERTY_IDGENERATOR_LOCALPORT, "0"));
|
||||||
|
|
||||||
int idGeneratorPort = 0;
|
int idGeneratorPort = 0;
|
||||||
ServerSocket ss = null;
|
ServerSocket ss = null;
|
||||||
try {
|
try {
|
||||||
idGeneratorPort = Integer.parseInt(System.getProperty(PROPERTY_IDGENERATOR_PORT, "0"));
|
if( hostName!=null ) {
|
||||||
LOG.trace("Using port {}", idGeneratorPort);
|
hostName = InetAddressUtil.getLocalHostName();
|
||||||
hostName = InetAddressUtil.getLocalHostName();
|
}
|
||||||
ss = new ServerSocket(idGeneratorPort);
|
if( localPort==0 ) {
|
||||||
stub = "-" + ss.getLocalPort() + "-" + System.currentTimeMillis() + "-";
|
idGeneratorPort = Integer.parseInt(System.getProperty(PROPERTY_IDGENERATOR_PORT, "0"));
|
||||||
Thread.sleep(100);
|
LOG.trace("Using port {}", idGeneratorPort);
|
||||||
|
ss = new ServerSocket(idGeneratorPort);
|
||||||
|
localPort = ss.getLocalPort();
|
||||||
|
stub = "-" + localPort + "-" + System.currentTimeMillis() + "-";
|
||||||
|
Thread.sleep(100);
|
||||||
|
} else {
|
||||||
|
stub = "-" + localPort + "-" + System.currentTimeMillis() + "-";
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (LOG.isTraceEnabled()) {
|
if (LOG.isTraceEnabled()) {
|
||||||
LOG.trace("could not generate unique stub by using DNS and binding to local port", e);
|
LOG.trace("could not generate unique stub by using DNS and binding to local port", e);
|
||||||
|
|
Loading…
Reference in New Issue