Search code examples
javaliferayliferay-7

Liferay 7.1 - Kill a thread execution from scheduler jobs


I have my following job in Liferay 7.1 :

@Component(
        immediate = true, property = {"cron.expression=0 5 10 * * ? *"},
        service = CustomJob.class
)
public class CustomJob extends BaseMessageListener {

....

    @Override
    protected void doReceive(Message message) throws Exception {
           // HERE I CALL A SERVICE FUNCTION TO INACTIVATE USER, SEND MAILS, READ FILES TO IMPORT DATA
           RunnableService rs = new RunnableService();
           rs.run();
    }

....
}

And my RunnableService :

 public class RunnableService implements Runnable  {
    
        @Override
        public synchronized void run() {
            // DO MY STUFF
        }
    }

The job is working great, but another instance of the job can be started even when the service execution from the first call hasn't finished.

Is there any solutions to kill the first process ?

Thanks,


Solution

  • Sounds like there are several options, depending on what you want to achieve with this:

    • You shouldn't interrupt threads with technical measures. Rather have your long-running task check frequently if it should still be running, otherwise terminate gracefully - with the potential of cleaning up after itself
    • You can implement your functionality with Liferay's MessageBus - without the need to start a thread (which isn't good behavior in a webapp anyway). The beauty of this is that even in a cluster you end up with only one concurrent execution.
    • You can implement your functionality outside of the Liferay process and just interact with Liferay's API in order to do anything that needs to have an impact on Liferay. The beauty of this approach is that both can be separated to different machines - e.g. scale.