0

我是 Java 的新手,目前我们每天都在运行一个执行类(struts2 Web 应用程序项目)的 cronjob,该类在特定时间向两个不同的团队发送电子邮件。该类包括两种方法,一种用于向销售团队发送电子邮件,另一种用于向当天在网站上创建的关键字列表的业务团队发送电子邮件。要求是在不同的时间向销售团队发送电子邮件,并将业务团队发送给其他团队。那么,我可以通过指定方法名称来编写 cron 作业,以便在那个时候只执行那个特定的方法。

谢谢。

4

1 回答 1

1

您可以将参数传递给 Main 类,并使用此参数调用不同的方法:

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();
        }
    }
}

对于销售团队,使用java -cp . SelectMethod sales; 对于另一个团队,使用java -cp . SelectMethod other.

于 2011-11-28T06:18:03.583 回答