1616
1717package reactor .rabbitmq ;
1818
19- import com .rabbitmq .client .impl . NetworkConnection ;
19+ import com .rabbitmq .client .Connection ;
2020
2121import java .io .BufferedReader ;
2222import java .io .IOException ;
@@ -87,23 +87,34 @@ public static Process rabbitmqctl(String command) throws IOException {
8787 }
8888
8989 public static String rabbitmqctlCommand () {
90- return System .getProperty ("rabbitmqctl.bin" );
90+ String rabbitmqCtl = System .getProperty ("rabbitmqctl.bin" , System .getenv ("RABBITMQCTL_BIN" ));
91+ if (rabbitmqCtl == null ) {
92+ throw new IllegalStateException ("Please define the rabbitmqctl.bin system property or "
93+ + "the RABBITMQCTL_BIN environment variable" );
94+ }
95+ if (rabbitmqCtl .startsWith ("DOCKER:" )) {
96+ String containerId = rabbitmqCtl .split (":" )[1 ];
97+ return "docker exec " + containerId + " rabbitmqctl" ;
98+ } else {
99+ return rabbitmqCtl ;
100+ }
91101 }
92102
93- public static void closeConnection (String pid ) throws IOException {
103+ private static void closeConnection (String pid ) throws IOException {
94104 rabbitmqctl ("close_connection '" + pid + "' 'Closed via rabbitmqctl'" );
95105 }
96106
97- public static void closeConnection (NetworkConnection c ) throws IOException {
107+ public static void closeConnection (Connection c ) throws IOException {
98108 Host .ConnectionInfo ci = findConnectionInfoFor (Host .listConnections (), c );
99109 closeConnection (ci .getPid ());
100110 }
101111
102112 public static List <ConnectionInfo > listConnections () throws IOException {
103- String output = capture (rabbitmqctl ("list_connections -q pid peer_port" ).getInputStream ());
113+ String output = capture (rabbitmqctl ("list_connections -q pid peer_port client_properties" )
114+ .getInputStream ());
104115 // output (header line presence depends on broker version):
105116 // pid peer_port
106- 117+ // <[email protected] > 58713 [{"product","RabbitMQ"},{"... 107118 String [] allLines = output .split ("\n " );
108119
109120 ArrayList <ConnectionInfo > result = new ArrayList <ConnectionInfo >();
@@ -112,18 +123,31 @@ public static List<ConnectionInfo> listConnections() throws IOException {
112123 String [] columns = line .split ("\t " );
113124 // can be also header line, so ignoring NumberFormatException
114125 try {
115- result .add (new ConnectionInfo (columns [0 ], Integer .valueOf (columns [1 ])));
126+ Integer .valueOf (columns [1 ]); // just to ignore header line
127+ result .add (new ConnectionInfo (columns [0 ], connectionName (columns [2 ])));
116128 } catch (NumberFormatException e ) {
117129 // OK
118130 }
119131 }
120132 return result ;
121133 }
122134
123- private static Host .ConnectionInfo findConnectionInfoFor (List <ConnectionInfo > xs , NetworkConnection c ) {
135+ private static String connectionName (String clientProperties ) {
136+ String beginning = "\" connection_name\" ,\" " ;
137+ int begin = clientProperties .indexOf (beginning );
138+ if (begin > 0 ) {
139+ int start = clientProperties .indexOf (beginning ) + beginning .length ();
140+ int end = clientProperties .indexOf ("\" " , start );
141+ return clientProperties .substring (start , end );
142+ } else {
143+ return null ;
144+ }
145+ }
146+
147+ private static Host .ConnectionInfo findConnectionInfoFor (List <ConnectionInfo > xs , Connection c ) {
124148 Host .ConnectionInfo result = null ;
125149 for (Host .ConnectionInfo ci : xs ) {
126- if (c .getLocalPort () == ci .getPeerPort ( )) {
150+ if (c .getClientProvidedName (). equals ( ci .getName () )) {
127151 result = ci ;
128152 break ;
129153 }
@@ -134,19 +158,19 @@ private static Host.ConnectionInfo findConnectionInfoFor(List<ConnectionInfo> xs
134158 public static class ConnectionInfo {
135159
136160 private final String pid ;
137- private final int peerPort ;
161+ private final String name ;
138162
139- public ConnectionInfo (String pid , int peerPort ) {
163+ public ConnectionInfo (String pid , String name ) {
140164 this .pid = pid ;
141- this .peerPort = peerPort ;
165+ this .name = name ;
142166 }
143167
144168 public String getPid () {
145169 return pid ;
146170 }
147171
148- public int getPeerPort () {
149- return peerPort ;
172+ public String getName () {
173+ return name ;
150174 }
151175 }
152176}
0 commit comments