1

I am working on a Spring-MVC application in which I am using scheduling to delete extra stuff which is not necessary. Unfortunately, my scheduled method did not fire. Can anyone tell me what I did wrong.

Here is the code :

@Repository
@Transactional
@EnableScheduling
public class NotificationDAOImpl implements NotificationDAO{

    @Override
    @Scheduled(cron = "0 3 3 * * ?")
    public void deleteNotificationsAutoMagically(){
        session=this.sessionFactory.getCurrentSession();
        long now = System.currentTimeMillis();
        long nowMinus1Week = now - (1000 * 60 * 60 * 24 * 3);
        Timestamp nowMinus1WeekAsTimeStamp = new Timestamp(nowMinus1Week);
        Query query = session.createQuery("delete from NoteLock as nl where nl.timestamp < :limit and nl.read=:true");
        query.setParameter("limit", nowMinus1WeekAsTimeStamp);
        query.executeUpdate();
        session.flush();
    }
}

I know parameter name is for 1 week, but I am deleting it in 3 days. I just copied the code .. :D Any help would be nice. Thanks.

We are Borg
  • 5,117
  • 17
  • 102
  • 225

1 Answers1

1

That cron expression looks like it will run at 3 O' clock on 3rd of every month.

If you want to run every 3 minutes, you can use the below expression.

0 0/3 * 1/1 * ? *

You can use cronmaker for generating expressions

To verify whether the cron expressions you create, visit this page

aksappy
  • 3,400
  • 3
  • 23
  • 49
  • No, I want to run it every night at 3.03am, everyday, once. – We are Borg May 12 '15 at 11:00
  • Hello, there is a problem with the cronmaker I think, he is giving me me 7 variables instead of 6, I am getting an error. Can you check our the cronmaker site you just gave. – We are Borg May 12 '15 at 11:40
  • What is your condition? – aksappy May 12 '15 at 11:48
  • If I want to run it every night at 3:03am, cronmaker gives me "0 3 3 1/1 * ? *", which has 7 variables. – We are Borg May 12 '15 at 11:49
  • Have you done EnableScheduling in your configuration? http://docs.spring.io/autorepo/docs/spring-framework/current/spring-framework-reference/html/scheduling.html#scheduling-annotation-support – aksappy May 12 '15 at 12:06
  • The one which I gave is purely Quartz related cron expressions. There is already an SO here http://stackoverflow.com/questions/26147044/spring-cron-expression-for-every-day-101am – aksappy May 12 '15 at 12:08
  • Yes, I used the above link only after researching the problem. I have enabledscheduling. It works now, I thought you had noticed that there are only 6 variables and had your answer that way. It is fine. – We are Borg May 12 '15 at 12:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77611/discussion-between-aksappy-and-we-are-borg). – aksappy May 12 '15 at 12:16