You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by DUTERTRY Nicolas <Ni...@soprahr.com> on 2019/01/30 10:26:19 UTC
Interrupting Karaf command
Hi,
I would like to write a command allowing the user to press Ctrl+C to interrupt the command.
For instance :
@Command(name = "loop", scope = "test", description = "Test loop")
@Service
public class LoopCommand implements Action {
private static final Logger LOGGER = LoggerFactory.getLogger(LoopCommand.class);
@Override
public Object execute() throws Exception {
int i = 0;
while(true) {
System.out.println(i);
LOGGER.info("LoopCommand {}", i);
Thread.sleep(1000L);
i++;
}
}
}
When I run this command inside karaf shell and press Ctrl+C, the command thread is interrupted and receives an InterruptedException. This is fine.
But when I run the command outside karaf shell with "client test:loop" it is not possible to interrupt the command. Ctrl+C has no effect and if I kill the client process, the command is still running inside karaf.
What is the right way to write a command that can be interrupted by the user ?
Regards,
--
Nicolas Dutertry
Re: Interrupting Karaf command
Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
CTRL-C is interceptor by the local console, but not the SSH one, which
is normal. I can change this behavior, but it could impact other users
(for instance, users might launch a long command with the client,
disconnect the client, with the command still running).
You can kill a running command thread with dedicated commands though.
Regards
JB
On 30/01/2019 11:26, DUTERTRY Nicolas wrote:
> Hi,
>
>
>
> I would like to write a command allowing the user to press Ctrl+C to
> interrupt the command.
>
>
>
> For instance :
>
> @Command(name = "loop", scope = "test", description = "Test loop")
>
> @Service
>
> public class LoopCommand implements Action {
>
> private static final Logger LOGGER =
> LoggerFactory.getLogger(LoopCommand.class);
>
>
>
> @Override
>
> public Object execute() throws Exception {
>
> int i = 0;
>
> while(true) {
>
> System.out.println(i);
>
> LOGGER.info("LoopCommand {}", i);
>
> Thread.sleep(1000L);
>
> i++;
>
> }
>
> }
>
> }
>
>
>
> When I run this command inside karaf shell and press Ctrl+C, the command
> thread is interrupted and receives an InterruptedException. This is fine.
>
>
>
> But when I run the command outside karaf shell with “client test:loop”
> it is not possible to interrupt the command. Ctrl+C has no effect and if
> I kill the client process, the command is still running inside karaf.
>
>
>
> What is the right way to write a command that can be interrupted by the
> user ?
>
>
>
> Regards,
>
> --
>
> Nicolas Dutertry
>
>
>
--
Jean-Baptiste Onofré
jbonofre@apache.org
http://blog.nanthrax.net
Talend - http://www.talend.com