views:

390

answers:

3

Hiya all,

I have a method that returns a list of items called findItemByPIdEndDate() and its found inside MngtImpl.java class. Now I'd like to execute this method every once in a while, hourly lets say so here is what I did so far :

This is CronSchedule.java

import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.JobDetail;

public class CronSchedule {
  public CronSchedule ()throws Exception {
    SchedulerFactory sf=new StdSchedulerFactory();
    Scheduler sched=sf.getScheduler();
    JobDetail jd=new JobDetail("job1","group1",CronJob.class);
    CronTrigger ct=new CronTrigger("cronTrigger","group2","0 * * * * ?");
    sched.scheduleJob(jd,ct);
    sched.start();
  }
  public static void main(String args[]){
    try{    
        new CronSchedule();          //Class where cron is created
    }catch(Exception e){}
  }                                 
}

Now with this one I have problems. This class is called from the class above based on time constraints (* in parenthesis) and it executes method of MngtImpl class.

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.rosa.MngtImpl;

public class CronJob implements Job  {
  public void execute(JobExecutionContext arg0) throws JobExecutionException {
          try {
                  MngtImpl pers = new MngtImpl();
                  pers.findItemByPIdEndDate();
        } catch (Exception e) {

                e.printStackTrace();
        }
  }
}

Here is the method I try to invoke from the above class. This method is found inside class named MngtImpl.java

 public List<Long> findItemByPIdEndDate() throws Exception {

                List<Long> list = null;

                try{
                        Session session = sessionFactory.getCurrentSession();   
                        Query query = session.getNamedQuery("endDateChecker");
                        list = query.list();

                }catch (HibernateException e){
                        throw new DataAccessException(e.getMessage());
                }

                return list;
        }

Now here is what I'd like to do. I need to run this this above method based on some amount of time(which I could specify anytime). I need to make a bean inside bean factory xml file that actually instances this scheduler and calls this method, and I tried this by calling CronJob.java from CronScheldule.java which calls method from another class and I made a bean which maybe working or not

<bean id="cronSchedule" class="com.mypackage.CronSchedule">
        </bean>

But its not working as I expected and its pretty complicated, may I get some help with code please? Thank you

I encounter the following exception:

java.lang.NullPointerException 11:10:46,410 ERROR [STDERR]
   at com.packftc.RulesManager.exception(RulesManager.java:25) 11:10:46,410 ERROR [STDERR]
   at org.drools.agent.RuleAgent$2.run(RuleAgent.java:442) 11:10:46,410 ERROR [STDERR]
   at java.util.TimerThread.mainLoop(Unknown Source) 11:10:46,410 ERROR [STDERR]
   at java.util.TimerThread.run(Unknown Source)

This is obviously wrong way of doing this I've tried like zilion things so far : Here is what I'm trying to do in simple words, I want to execute findItemByPIdEndDate() method from MngtImpl class every lets say minute. Method is functioning properly I wrote a test and it passes it. So what do I do now ? Thank you

A: 

Can you please elaborate ur question?

Adnan
put it in comment please !
Nrj
Agreed - this is not an answer to the question and should not be presented as such...
Andrzej Doyle
A: 

If executing a task periodically is the problem here (I'm not quite sure) I suggest using ScheduledExecutorService, which is part of JDK 1.5 and later:

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class SchedulerExample {

private ScheduledExecutorService scheduler = 
       Executors.newScheduledThreadPool(1);

public void activateHourlyDataRetrieval() {

        Runnable dataRetriever = new Runnable() {

           MngtImpl pers = new MngtImpl();

           public void run() {

               pers.findItemByPIdEndDate();

            }
         };

        scheduler.scheduleAtFixedRate(dataRetriever, 0, 1, TimeUnit.HOURS);
}
}
MarkoU
I'm getting a compile error "TimeUnit.HOURS cannot be resolved " Do I need to create any beans inside appcontext ?
c0mrade
Did you remember to import <code>java.util.concurrent.TimeUnit</code>? Are you using JDK 1.5 or later?
MarkoU
I changed the time to seconds.. still nothing
c0mrade
Marco - the problem is an NPE, as the OP alluded to in comments to his question, but still has not shown the stacktrace of (hint, hint).
Andrzej Doyle
A: 

Just a pointer since I could not make much by the stack trace, but it appears the problem is in pers.findItemByPIdEndDate(); since the code works fine when I replace the Hibernate stuff with a plain java class.

Also I supplied the CronTrigger pattern as 0/5 * * * * ? so to run it per 5 second. Your current pattern does not fire the job at all.

See if this is of any help.

Nrj