0

I have a Java program that reads text files and processes them sequentially and on completion of each file a mail is triggered in a separate thread (using runnable). I have configured this program as a cron job in crontab to run every 15 mins.

The cron does not get triggered if the child thread of the last job run takes more than 20 minutes to send the email. How can I have the job triggered every 15 minutes, even if the last child thread is still executing?

Below is the code used::

EpdFeedLoader.java

public class EpdFeedLoader {
    public static void main(String[] args) throws Exception {
        startProcess(feedFile);
    }

    public static void startProcess(BatchFeedFile feedFile) {
        EpdBatchLoader epdBatchLoader = new EpdBatchLoader(feedFile);
        epdBatchLoader.execute();
    }
}

EpdBatchLoader.java

public class EpdBatchLoader {
    private static void sendDelayedFeedEmail(){
        SendDelayedFeedJobStatusMail delayedFeedEmail = new SendDelayedFeedJobStatusMail();
        delayedFeedEmail.setParameters(//parameters for my email method);
        Thread delayedFeedThread =  new Thread(delayedFeedEmail);
        delayedFeedThread.start();
    }
}

SendDelayedFeedJobStatusMail.java

public class SendDelayedFeedJobStatusMail implements Runnable {

    private MailRequestDTO mailRequestDTO;

    public void setParameters(MailRequestDTO mailRequestDTO){
        this.mailRequestDTO = mailRequestDTO;
    }

    public void sendMail() {
        boolean isSend = false;
        message.setSubject(subject);
        message.setSentDate(Utils.getDateInTimeZone(mailRequestDTO.getCountryCode(),new Date()));
        message.setContent(mailRequestDTO.getMessageContent(), "text/html");
        message.addHeader("Content-Transfer-Encoding", "7bit");
        attachInlineImages(message, mailRequestDTO.getInlineImageMap()); 
        ConfigUtils.logger.info("Ready to Send Delayed Feed Receipt Email for Process Code : " +  mailRequestDTO.getProcessCode().toString());
        Transport.send(message);
        isSend = true;
        ConfigUtils.logger.info("Delayed Feed Receipt Email Sent Successfully for Process Code : " +  mailRequestDTO.getProcessCode().toString());
    }   

    @Override
    public void run(){
        this.sendMail();
    }
}
Ryan Wright
  • 91
  • 1
  • 4
  • What framework you are using? Can you please post some code? – xingbin May 02 '18 at 11:12
  • user6690200 - i have added the code that i have used – Ryan Wright May 03 '18 at 02:31
  • What exactly is the problem? That cron does not start a new job? Then all the Java code is irrelevant. But cron [should start a new job anyway](https://stackoverflow.com/questions/10552016/how-to-prevent-the-cron-job-execution-if-it-is-already-running). Is your Java code doing something to prevent multiple instances from running? – Robert May 03 '18 at 03:05
  • The cron is scheduled to run every 15 mins, but the mail that is triggered in the first job is running for about 30 minutes, and the second run does not kick off. I have not done anything code wise to prevent multiple instances from running. Any suggestions on how to go about this ? – Ryan Wright May 03 '18 at 03:09

0 Answers0