custom Karaf commands and OSGi container termination

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

custom Karaf commands and OSGi container termination

Max Spring
Is it possible to write a Karaf command which
(a) initiates the termination of the OSGi container and
(b) defines the exit code of the JVM process (like System.exit(exitCode) would do)?

Context: For my own development I quite often run Karaf in a shell loop so I don't have to manually restart:

$ while true; do karaf clean; sleep 2; done

I like to write my own Karaf console command so I can exit this loop based on Karaf's exit code.

Thanks!
-Max
Reply | Threaded
Open this post in threaded view
|

Re: custom Karaf commands and OSGi container termination

jbonofre
Hi,

You can use the Karaf System service to perform a halt/shutdown:

https://github.com/apache/karaf/blob/master/system/src/main/java/org/apache/karaf/system/SystemService.java

However, you can't define the exit code (it's controlled internally).

I think it makes more sense to exit the loop based on the concrete exit
condition: you can have a custom "control" service that your command is
tracking and then exit the loop (if I understood your use case).

Regards
JB

On 12/01/2019 02:13, Max Spring wrote:

> Is it possible to write a Karaf command which
> (a) initiates the termination of the OSGi container and
> (b) defines the exit code of the JVM process (like System.exit(exitCode)
> would do)?
>
> Context: For my own development I quite often run Karaf in a shell loop
> so I don't have to manually restart:
>
> $ while true; do karaf clean; sleep 2; done
>
> I like to write my own Karaf console command so I can exit this loop
> based on Karaf's exit code.
>
> Thanks!
> -Max

--
Jean-Baptiste Onofré
[hidden email]
http://blog.nanthrax.net
Talend - http://www.talend.com
Reply | Threaded
Open this post in threaded view
|

Re: custom Karaf commands and OSGi container termination

Max Spring
Hi JB,

thanks for the pointer.
I just found the Shutdown command source [1] as a good example for how to use the SystemService.

Instead of using an exit code, I can use a temporary file as a marker created by my own "shutdown" command to have the outer loop continue or exit.

Regards,
-Max

[1] https://github.com/apache/karaf/blob/master/system/src/main/java/org/apache/karaf/system/commands/Shutdown.java


On 1/11/19 10:23 PM, Jean-Baptiste Onofré wrote:

> Hi,
>
> You can use the Karaf System service to perform a halt/shutdown:
>
> https://github.com/apache/karaf/blob/master/system/src/main/java/org/apache/karaf/system/SystemService.java
>
> However, you can't define the exit code (it's controlled internally).
>
> I think it makes more sense to exit the loop based on the concrete exit
> condition: you can have a custom "control" service that your command is
> tracking and then exit the loop (if I understood your use case).
>
> Regards
> JB
>
> On 12/01/2019 02:13, Max Spring wrote:
>> Is it possible to write a Karaf command which
>> (a) initiates the termination of the OSGi container and
>> (b) defines the exit code of the JVM process (like System.exit(exitCode)
>> would do)?
>>
>> Context: For my own development I quite often run Karaf in a shell loop
>> so I don't have to manually restart:
>>
>> $ while true; do karaf clean; sleep 2; done
>>
>> I like to write my own Karaf console command so I can exit this loop
>> based on Karaf's exit code.
>>
>> Thanks!
>> -Max
>