2626import java .util .List ;
2727import java .util .Map ;
2828import java .util .Properties ;
29+ import java .util .concurrent .CompletableFuture ;
2930import java .util .concurrent .ConcurrentHashMap ;
3031import java .util .concurrent .CopyOnWriteArrayList ;
3132import java .util .concurrent .CountDownLatch ;
@@ -164,6 +165,7 @@ public class TransportConnection implements Connection, Task, CommandVisitor {
164165 private final ReentrantReadWriteLock serviceLock = new ReentrantReadWriteLock ();
165166 private String duplexNetworkConnectorId ;
166167 private final long connectedTimestamp ;
168+ private final CompletableFuture <ConnectionId > initialConnectionId = new CompletableFuture <>();
167169
168170 /**
169171 * @param taskRunnerFactory - can be null if you want direct dispatch to the transport
@@ -852,11 +854,16 @@ public Response processAddConnection(ConnectionInfo info) throws Exception {
852854
853855 try {
854856 broker .addConnection (context , info );
857+ // Complete the future with the connectionId if we completed
858+ // the broker.addConnection() chain successfully
859+ initialConnectionId .complete (info .getConnectionId ());
855860 } catch (Exception e ) {
856861 synchronized (brokerConnectionStates ) {
857862 brokerConnectionStates .remove (info .getConnectionId ());
858863 }
859864 unregisterConnectionState (info .getConnectionId ());
865+ // complete with the exception
866+ initialConnectionId .completeExceptionally (e );
860867 LOG .warn ("Failed to add Connection id={}, clientId={}, clientIP={} due to {}" ,
861868 info .getConnectionId (), clientId , info .getClientIp (), e .getLocalizedMessage ());
862869 //AMQ-6561 - stop for all exceptions on addConnection
@@ -1390,13 +1397,10 @@ public Response processBrokerInfo(BrokerInfo info) {
13901397 LOG .error (" Slave Brokers are no longer supported - slave trying to attach is: {}" , info .getBrokerName ());
13911398 } else if (info .isNetworkConnection () && !info .isDuplexConnection ()) {
13921399 try {
1393- NetworkBridgeConfiguration config = getNetworkConfiguration (info );
1394- if (config .isSyncDurableSubs () && protocolVersion .get () >= CommandTypes .PROTOCOL_VERSION_DURABLE_SYNC ) {
1395- LOG .debug ("SyncDurableSubs is enabled, Sending BrokerSubscriptionInfo" );
1396- dispatchSync (NetworkBridgeUtils .getBrokerSubscriptionInfo (this .broker .getBrokerService (), config ));
1397- }
1400+ // register durable sync to be sent after ConnectionInfo has been handled
1401+ registerDurableSync (getNetworkConfiguration (info ), info );
13981402 } catch (Exception e ) {
1399- LOG .error ("Failed to respond to network bridge creation from broker {}" , info .getBrokerId (), e );
1403+ LOG .error ("Failed to register durable sync for network bridge creation from broker {}" , info .getBrokerId (), e );
14001404 return null ;
14011405 }
14021406 } else if (info .isNetworkConnection () && info .isDuplexConnection ()) {
@@ -1406,10 +1410,8 @@ public Response processBrokerInfo(BrokerInfo info) {
14061410 NetworkBridgeConfiguration config = getNetworkConfiguration (info );
14071411 config .setBrokerName (broker .getBrokerName ());
14081412
1409- if (config .isSyncDurableSubs () && protocolVersion .get () >= CommandTypes .PROTOCOL_VERSION_DURABLE_SYNC ) {
1410- LOG .debug ("SyncDurableSubs is enabled, Sending BrokerSubscriptionInfo" );
1411- dispatchSync (NetworkBridgeUtils .getBrokerSubscriptionInfo (this .broker .getBrokerService (), config ));
1412- }
1413+ // register durable sync to be sent after ConnectionInfo has been handled
1414+ registerDurableSync (config , info );
14131415
14141416 // check for existing duplex connection hanging about
14151417
@@ -1475,6 +1477,30 @@ public Response processBrokerInfo(BrokerInfo info) {
14751477 return null ;
14761478 }
14771479
1480+ private void registerDurableSync (final NetworkBridgeConfiguration config , final BrokerInfo info ) {
1481+ if (config .isSyncDurableSubs () && protocolVersion .get () >= CommandTypes .PROTOCOL_VERSION_DURABLE_SYNC ) {
1482+ // this will complete when the connection id has been set, or immediately if already set
1483+ initialConnectionId .whenComplete ((connectionId , t ) -> {
1484+ try {
1485+ if (t != null ) {
1486+ LOG .warn ("SyncDurableSubs will be skipped due to error {}" ,
1487+ t .getMessage ());
1488+ return ;
1489+ }
1490+ // check connection still registered
1491+ if (lookupConnectionState (connectionId ) != null ) {
1492+ LOG .debug ("SyncDurableSubs is enabled, Sending BrokerSubscriptionInfo" );
1493+ dispatchSync (NetworkBridgeUtils .getBrokerSubscriptionInfo (
1494+ this .broker .getBrokerService (), config ));
1495+ }
1496+ } catch (Exception e ) {
1497+ LOG .error ("Failed to respond to network bridge creation from broker {}" ,
1498+ info .getBrokerId (), e );
1499+ }
1500+ });
1501+ }
1502+ }
1503+
14781504 @ SuppressWarnings ({"unchecked" , "rawtypes" })
14791505 private HashMap <String , String > createMap (Properties properties ) {
14801506 return new HashMap (properties );
0 commit comments