2012-02-03 102 views
8

我想安排一個班級每15分鐘運行一次。我知道我們可以每小時設置一次salesforce,但是有沒有辦法將粒度降低到10-15分鐘?我們如何安排班級在salesforce中每15分鐘運行一次?

global class scheduledMerge implements Schedulable { 
    global void execute(SchedulableContext SC) { 
     ProcessTransactionLog p= new ProcessTransactionLog(); 
     p.ProcessTransaction(); 
    } 
} 

回答

14

您可以使用此apex代碼段來安排您的作業每15分鐘運行一次。

System.schedule('Job1', '0 0 * * * ?', new scheduledMerge()); 
System.schedule('Job2', '0 15 * * * ?', new scheduledMerge()); 
System.schedule('Job3', '0 30 * * * ?', new scheduledMerge()); 
System.schedule('Job4', '0 45 * * * ?', new scheduledMerge()); 
+0

我敢肯定,你也可以用逗號分隔多個值: 'System.schedule( '作業1', '0 0,15,30,45 * * *?',新的scheduledMerge());' – 2012-02-04 04:48:43

+0

這個答案的第一行將導致作業(理論上)每分鐘執行一次。表達「每15分鐘運行15次」的「cron-preferred」方式將是'0 0/15 * * *?'。 – jkraybill 2012-02-05 22:54:50

+3

Matt,實際上在Apex CRON中,您必須指定秒和分鐘作爲整數 - 逗號,星號和破折號僅適用於其他CRON組件。所以你的'0,15,30,45'方法將不起作用。此外,關於遵循Rajesh的方法,一個謹慎的詞語:你只能有25個總調度頂點類,Rajesh的方法使用了其中4個。但是,如果你有這麼多的工作要做,那麼這將會很好。 – zachelrath 2012-06-04 21:20:58

0
global class scheduledTest implements Schedulable { 
    global void execute(SchedulableContext SC) { 
     RecurringScheduleJob.startJob(); 
     String day = string.valueOf(system.now().day()); 
     String month = string.valueOf(system.now().month()); 
     String hour = string.valueOf(system.now().hour()); 
     String minute = string.valueOf(system.now().minute() + 15); 
     String second = string.valueOf(system.now().second()); 
     String year = string.valueOf(system.now().year()); 

     String strJobName = 'Job-' + second + '_' + minute + '_' + hour + '_' + day + '_' + month + '_' + year; 
     String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year; 
     System.schedule(strJobName, strSchedule, new scheduledTest()); 
    } 
} 
+1

定義一個名爲'now'的變量,並使用addMinutes(Integer)方法將是準確的。 Datetime now = System.now();日期時間nextE = now.addMinutes(10); – 2014-04-05 01:45:30

+0

即使當我將頂點時間表設置爲1小時時,這將每15分鐘運行一次? – sfdclearner 2016-08-12 02:49:10

相關問題