Skip to content

Commit 14eb9a5

Browse files
committed
CAMEL-22952: camel-jbang - Fix receive command to work better and auto select if only 1 camel app running.
1 parent 053a6e8 commit 14eb9a5

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

  • dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action

dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelReceiveAction.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,14 @@ public Integer doCall() throws Exception {
227227
if (name != null) {
228228
return doCall(name, autoDump);
229229
} else {
230-
return doCallLocal(autoDump);
230+
// are there only 1 pid running then use that
231+
List<Long> pids = findPids("*");
232+
if (pids.size() == 1) {
233+
return doCall(pids.get(0), autoDump);
234+
}
231235
}
236+
// okay fallback and run camel locally to connect to external system
237+
return doCallLocal(autoDump);
232238
}
233239

234240
private Integer doCall(String name, boolean autoDump) throws Exception {
@@ -240,7 +246,8 @@ private Integer doCall(String name, boolean autoDump) throws Exception {
240246
Files.writeString(f, "{}");
241247
}
242248
} else {
243-
Path outputFile = writeReceiveData();
249+
Path outputFile = writeReceiveData(pid);
250+
printer().println("Starting to receive messages from existing Camel: " + name + " (pid: " + pid + ")");
244251
showStatus(outputFile);
245252
}
246253
}
@@ -252,6 +259,26 @@ private Integer doCall(String name, boolean autoDump) throws Exception {
252259
return 0;
253260
}
254261

262+
private Integer doCall(long pid, boolean autoDump) throws Exception {
263+
this.pid = pid;
264+
if ("clear".equals(action)) {
265+
Path f = getReceiveFile("" + pid);
266+
if (Files.exists(f)) {
267+
Files.writeString(f, "{}");
268+
}
269+
} else {
270+
Path outputFile = writeReceiveData(pid);
271+
printer().println("Starting to receive messages from existing Camel (pid: " + pid + ")");
272+
showStatus(outputFile);
273+
}
274+
275+
if (autoDump) {
276+
return doDumpCall();
277+
}
278+
279+
return 0;
280+
}
281+
255282
private Integer doCallLocal(boolean autoDump) throws Exception {
256283
AtomicReference<KameletMain> ref = new AtomicReference<>();
257284
Run run = new Run(this.getMain()) {
@@ -268,7 +295,7 @@ protected int runKameletMain(KameletMain main) throws Exception {
268295
// spawn thread that waits for response file
269296
final CountDownLatch latch = new CountDownLatch(1);
270297
this.pid = ProcessHandle.current().pid();
271-
Path outputFile = writeReceiveData();
298+
Path outputFile = writeReceiveData(this.pid);
272299
Thread t = new Thread("CamelJBangSendStatus") {
273300
@Override
274301
public void run() {
@@ -294,14 +321,16 @@ public void run() {
294321
};
295322
// keep thread running as we need it to show the status before terminating
296323
t.start();
324+
printer().println(
325+
"Starting to receive messages by connecting to external system using local process (pid: " + pid + ")");
297326

298327
Integer exit = run.call();
299328
latch.await();
300329

301330
return exit;
302331
}
303332

304-
protected Path writeReceiveData() {
333+
protected Path writeReceiveData(long pid) {
305334
// ensure output file is deleted before executing action
306335
Path outputFile = getOutputFile(Long.toString(pid));
307336
PathUtils.deleteFile(outputFile);

0 commit comments

Comments
 (0)