0

For an account management system, i need to send an email to the users in database which have XX days remaining for their subscription.

The idea is to create a function in my Java EE code (the application in written in Java/Java EE with VAADIN 7.2 framework) which checks remaining time of each users, and sends an email if the remaining time is XX days with my MailEngine() class;

The problem is that i don't know how to create a daemon like that in Java/Java EE, or call a function everyday at 12:00 PM for example.

The solution can be to create a Java App witch is called by crontab everyday but the best solution is to do it in my Java EE application, then it'll be fully customizable (change mail body/ title in the web interfaces, for admins who are not developers).

EDIT:

I tested this code sent by joseripla

import javax.ejb.Schedule;
import javax.ejb.Singleton;

@Singleton
public class MarketingMailEngine {
    int count = 0;
    //Print to log every 5 seconds
    @Schedule(second="*/5", minute="*", hour="*", persistent=true)
    public void print() {

        System.out.println(count);
        count++;
    }
}

But it only prints 0, even if i wait for some minutes (it should do it every 5 secs).

5
  • Hi!, that's not exactly my code ;). By the way, i test the MarketingMailEngine class in a Weblogic 12.1.2 and it's running ok. What is your application server¿?. Commented Oct 13, 2014 at 16:13
  • i use tomcat7 as server Commented Oct 14, 2014 at 7:10
  • Try with TomEE which is an "Java EE 6 Web Profile certified stack" or with anohter JavaEE aplications server (if you want to use JavaEE) Commented Oct 14, 2014 at 8:53
  • If you want to use Java SE try with ScheduledExecutorService docs.oracle.com/javase/6/docs/api/java/util/concurrent/… or Quartz. Hope this help you. Commented Oct 14, 2014 at 9:06
  • @joseripla I can't use another application server, only tomcat7 because all of the application runs on it. Commented Oct 14, 2014 at 9:07

2 Answers 2

1

The solution for your problem is use the Java EE Scheduled Time Service link

Some basic code :

import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.inject.Inject;

@Singleton
public class SampleTask {

    @Inject
    private org.slf4j.Logger logger;

    //Print to log every 5 seconds
    @Schedule(second="*/5", minute="*", hour="*", persistent=false)
    public void print() {

        String m = "********** --------- CRON-TASK ---------- *********";
        logger.info(m);
    }
}

And it's done.

Sign up to request clarification or add additional context in comments.

1 Comment

Will try this tomorrow and tell you if it's the answer i'm looking for =)
0

It's rather off topic to recommend a tool, but what you are asking is exactly what quartz scheduler is made for.

You can start it and stop it in a servlet context listener. It is a well maintained application, with extensive documentation and tutorials, and it is the scheduler used by Spring framework.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.