Question

Am a new bie to Java, Currently we are running a daily cronjob of executing a class (a struts2 web application project) which sends an email to two different team @ a specific time. The class includes two methods, one for sending email to sales team, and other for sending email to business team of the list of created keywords on the site that day. The requirement is to send an email to sales team @ a different time, and to business team to other team. So, can i write cron jobs by specifying the method name, so that only that specific method will be executed @ that time.

Thanks.

Was it helpful?

Solution

You can pass a parameter to your Main class, and use this parameter to call different method:

public class SelectMethod {
    public static void sendToSales() {
        System.out.println("Sending mail to sales team...");
    }

    public static void sendToOther() {
        System.out.println("Sending mail to other team...");
    }

    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("No required parameter passed\n"
                    + "Valid options: sales, other");
            System.exit(1);
        }

        if ("sales".equals(args[0])) {
            sendToSales();
        } else if ("other".equals(args[0])) {
            sendToOther();
        }
    }
}

For sales team, run it with java -cp . SelectMethod sales; and for the other team, use java -cp . SelectMethod other.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top