Skip to content

Commit 4214017

Browse files
committed
Get stream connection list with table formatter
JSON formatter is broken in latest broker release.
1 parent 933f34a commit 4214017

File tree

1 file changed

+63
-15
lines changed

1 file changed

+63
-15
lines changed

src/test/java/com/rabbitmq/stream/Host.java

+63-15
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424
import java.io.InputStreamReader;
25-
import java.util.List;
25+
import java.util.*;
2626
import java.util.concurrent.Callable;
2727
import java.util.stream.Collectors;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
2830

2931
public class Host {
3032

33+
private static final Logger LOGGER = LoggerFactory.getLogger(Host.class);
34+
3135
private static final String DOCKER_PREFIX = "DOCKER:";
3236

3337
private static final Gson GSON = new Gson();
@@ -130,13 +134,56 @@ public static Process killConnection(String connectionName) {
130134
public static List<ConnectionInfo> listConnections() {
131135
try {
132136
Process process =
133-
rabbitmqctl("list_stream_connections --formatter json conn_name,client_properties");
134-
return toConnectionInfoList(capture(process.getInputStream()));
137+
rabbitmqctl("list_stream_connections -q --formatter table conn_name,client_properties");
138+
List<ConnectionInfo> connectionInfoList = Collections.emptyList();
139+
if (process.exitValue() != 0) {
140+
LOGGER.warn(
141+
"Error while trying to list stream connections. Standard output: {}, error output: {}",
142+
capture(process.getInputStream()),
143+
capture(process.getErrorStream()));
144+
return connectionInfoList;
145+
}
146+
String content = capture(process.getInputStream());
147+
String[] lines = content.split(System.getProperty("line.separator"));
148+
if (lines.length > 1) {
149+
connectionInfoList = new ArrayList<>(lines.length - 1);
150+
for (int i = 1; i < lines.length; i++) {
151+
String line = lines[i];
152+
String[] fields = line.split("\t");
153+
String connectionName = fields[0];
154+
Map<String, String> clientProperties = Collections.emptyMap();
155+
if (fields.length > 1 && fields[1].length() > 1) {
156+
clientProperties = buildClientProperties(fields);
157+
}
158+
connectionInfoList.add(new ConnectionInfo(connectionName, clientProperties));
159+
}
160+
}
161+
return connectionInfoList;
135162
} catch (IOException e) {
136163
throw new RuntimeException(e);
137164
}
138165
}
139166

167+
private static Map<String, String> buildClientProperties(String[] fields) {
168+
String clientPropertiesString = fields[1];
169+
clientPropertiesString =
170+
clientPropertiesString
171+
.replace("[", "")
172+
.replace("]", "")
173+
.replace("},{", "|")
174+
.replace("{", "")
175+
.replace("}", "");
176+
Map<String, String> clientProperties = new LinkedHashMap<>();
177+
String[] clientPropertyEntries = clientPropertiesString.split("\\|");
178+
for (String clientPropertyEntry : clientPropertyEntries) {
179+
String[] clientProperty = clientPropertyEntry.split("\",\"");
180+
clientProperties.put(
181+
clientProperty[0].substring(1),
182+
clientProperty[1].substring(0, clientProperty[1].length() - 1));
183+
}
184+
return clientProperties;
185+
}
186+
140187
static List<ConnectionInfo> toConnectionInfoList(String json) {
141188
return GSON.fromJson(json, new TypeToken<List<ConnectionInfo>>() {}.getType());
142189
}
@@ -278,29 +325,30 @@ public void close() throws Exception {
278325

279326
public static class ConnectionInfo {
280327

281-
private String conn_name;
282-
private List<List<String>> client_properties;
328+
private final String name;
329+
private final Map<String, String> clientProperties;
330+
331+
public ConnectionInfo(String name, Map<String, String> clientProperties) {
332+
this.name = name;
333+
this.clientProperties = clientProperties;
334+
}
283335

284336
public String name() {
285-
return this.conn_name;
337+
return this.name;
286338
}
287339

288340
public String clientProvidedName() {
289-
return client_properties.stream()
290-
.filter(p -> "connection_name".equals(p.get(0)))
291-
.findFirst()
292-
.get()
293-
.get(2);
341+
return this.clientProperties.get("connection_name");
294342
}
295343

296344
@Override
297345
public String toString() {
298346
return "ConnectionInfo{"
299-
+ "conn_name='"
300-
+ conn_name
347+
+ "name='"
348+
+ name
301349
+ '\''
302-
+ ", client_properties="
303-
+ client_properties
350+
+ ", clientProperties="
351+
+ clientProperties
304352
+ '}';
305353
}
306354
}

0 commit comments

Comments
 (0)