You need to sign in to do that
Don't have an account?
venkyyy
using batch class i want to schedule a batch class to print hello wold for every minute.. even if user get logged out.
Hi all,
I have a scenario that i want to schedule a batch class for every minute even if user get logged out.
for that i just created one basic batch class which i want to get in debug (please have a look bellow code).
if i run this throug execute ananomus window, it throws an error is: Invalid token "helloworld".
give me the lines what r the modifications i need to do.
batch class is1:
--------------------------
global class SampleBatchClass implements Database.Batchable<sObject>{
// Start Method
global Database.QueryLocator start(Database.BatchableContext BC){
string query = 'hellow world';
return Database.getQueryLocator(query);
}
// Execute Logic
global void execute(Database.BatchableContext BC, List<sObject>scope){
system.debug('>>>>>inside batcg<<<<<<<<<<'+scope);
}
global void finish(Database.BatchableContext BC){
// Logic to be Executed at finish
}
}
scheduled class is :
--------------------------------
global class sampleschdeulerclass implements schedulable
{
global void execute(SchedulableContext sc)
{
SampleBatchClass sbc = new SampleBatchClass();
database.executebatch(sbc);
system.debug('>>>>>>>>inside schedule class<<<<<<<<<');
}
}
I have a scenario that i want to schedule a batch class for every minute even if user get logged out.
for that i just created one basic batch class which i want to get in debug (please have a look bellow code).
if i run this throug execute ananomus window, it throws an error is: Invalid token "helloworld".
give me the lines what r the modifications i need to do.
batch class is1:
--------------------------
global class SampleBatchClass implements Database.Batchable<sObject>{
// Start Method
global Database.QueryLocator start(Database.BatchableContext BC){
string query = 'hellow world';
return Database.getQueryLocator(query);
}
// Execute Logic
global void execute(Database.BatchableContext BC, List<sObject>scope){
system.debug('>>>>>inside batcg<<<<<<<<<<'+scope);
}
global void finish(Database.BatchableContext BC){
// Logic to be Executed at finish
}
}
scheduled class is :
--------------------------------
global class sampleschdeulerclass implements schedulable
{
global void execute(SchedulableContext sc)
{
SampleBatchClass sbc = new SampleBatchClass();
database.executebatch(sbc);
system.debug('>>>>>>>>inside schedule class<<<<<<<<<');
}
}
Please see my comments below and do share the problem you are trying to solve.
Following are the issues:
- You are using query variable to save "Hello World" and its not correct query.
- If you need to run your batch every minute you will potentially run out of concurrent batch very quickly, I would recommend you to use finish method of batch to invoke it self again, this way you can chain your batches and will not run out of limit if one of the batch takes longer in processing.
Thanks,
Fahad Akhtar
Batch job is not required here. You can directly created on scheduler class. Please try below class. I hope that will help you
Please let us know if this will help you
Thanks,
Amit Chaudhary
I tried the code what you gave, it is running successfully but i get only once the '>>>>>Hellow world<<<<<<<<<<' in debug log and i am confusing to see how it'll works with every minute,? how can i see it. Can you guide me with the steps plz.
Its needfull to me.
Thanks in advance again. :)
Below code will schedule your class again and again:-
DateTime currentDateTime=System.now().addMinutes(1);
String nextScheduleTime=String.valueof(currentDateTime.second()) +' '+String.valueof(currentDateTime.minute())+' '
+String.valueof(currentDateTime.hour())+' '+String.valueof(currentDateTime.day())+' '
+String.valueof(currentDateTime.month())+' ? '+String.valueof(currentDateTime.Year());
//schedule next execution after a minute
SampleSchedulableClass obj = new SampleSchedulableClass ();
system.schedule('Scheduled at '+System.now().format(), nextScheduleTime, obj);
//delete completed apex scheduled jobs for which state is DELETED to keep the Scheduled APex log cleaner
for( CronTrigger cronTriggerItem:[Select Id From CronTrigger where
NextFireTime= null AND State='DELETED' Limit 100]){
System.abortJob(cronTriggerItem.id);
}
Please let us know if this will help u
I did not find any difference with previous code.