1

I have a Talend process with an infinite loop. I'd love to replace this infinite loop with a more controllable "while" loop. My thought is to have the while loop monitor an internal variable (maybe a context variable?) and set this variable once a Unix SIGINT signal is caught.

Is this possible? If yes how?

Are there viable alternatives? I could work with a sentinel file and stop the process if that file is found on disk but the SIGINT handling would be cleaner.

Technically the Talend process is a Java application, and I could potentially use a custon tJava component to write Java code to handle this properly.

update

I have tried to add a shutdown hook using a tJava component:

Runtime.getRuntime().addShutdownHook(new Thread() {
    @Override
    public void run() {
        System.out.println("Signal caught... finishing up current work and exiting.");
        context.keepRunning = false;
    }
});

In this component I am setting context.keepRunning, which is my loop condition to false.

Unfortunately, this not only runs the content in the shutdown hook, but also immediately exits the process. Control is not given back to the Talend process. I have two tWarn component after the loop component. One onSubjobOk and one on onSubjobError. Neither one is executed after sending a SIGINT or SIGTERM signal. Yet the println statement from the shutdown hook is executed.

exhuma
  • 20,071
  • 12
  • 90
  • 123
  • As far as I understood those signals, they will work directly with the thread you are referring to. So, if you send this signal to Java, Java will handle it immediately and react as always. An *additional* way to do something is `addShutdownHook`. Still, the job will be interrupted at any state. I'd go with the file or anything else which would be usable from a *tJava* or *tJavaRow*. – tobi6 Jul 07 '16 at 09:20

1 Answers1

1

From addShutdownHook docs :

When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks [..]. When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled. Finally, the virtual machine will halt.

So JVM starts the hook then it halts immediately, any code after the hook and all the Talend components after the tJava will not be executed !

What you need is a signal handler and not a hook. See this SO answer.

Community
  • 1
  • 1
54l3d
  • 3,913
  • 4
  • 32
  • 58