5

I've been searching all over google and stackoverflow about whether can the server send auto email , for example every monday, using servlet without any user interactions. AT the moment, user has to log in and codes gets excuted. What I want is the server to excute the code at specific time and user don't have to log in and don't even see that. I don't want to relay on users, I want to relay on server instead.

Cœur
  • 32,421
  • 21
  • 173
  • 232
darkerror
  • 57
  • 8

5 Answers5

3

You need to have a non interactive thread in your server. Ideally, this thread should be started by a ServletContextListener, to be sure that it will be active even before first client connection.

You can either build something from scratch or use a dedicated tool such as the excellent Quartz scheduler, that will do the boiler plate code for you.

Once you have the scheduler, you just need to be able to send mail to a mail server via SMTP. Here again, you can implement the protocol by hand (SMTP is not that hard), but if you do not want to re-invent an oval wheel where round ones are already around, use the javamail API from the Java EE SDK, or another third party library such as the one provided by Apache

Serge Ballesta
  • 121,548
  • 10
  • 94
  • 199
  • thank you very much, Quartz seems to be relying on maven, my application do Not, However, as servlet has numver of method, wht shall i invode to create non-interactive thread as you suggusted above? – darkerror Oct 05 '15 at 10:08
  • 1
    @darkerror: do not try to use `init` method of `Servlet` because the container can delay initialization *until the container determines the servlet is needed to service a request*. Declare instead a `ServletContextListener` either in `web.xml` file or in you use full java initialization *annotated with WebListener, or registered via one of the addListener methods defined on ServletContext* – Serge Ballesta Oct 05 '15 at 10:32
2

As long as your server application is running, you can use Quartz to schedule your email-sending process.

Check here: https://quartz-scheduler.org/documentation

javatutorial
  • 1,756
  • 14
  • 18
2

You should be able to do what you are after by setting up another application which executes as a service. Underneath, this application could use something such as Quartz to manage the scheduled events.

You could keep it in sync with the web application through the use of some database. This approach should allow for the automation of email transmission.

npinti
  • 50,175
  • 5
  • 67
  • 92
1

You can use Quartz to scheduler a job to run at a specified date and time. there are two types of triggers in Quartz 2

  • SimpleTrigger - this one Allows to set start time, end time, repeat interval.

  • CronTrigger - it Allows Unix cron expression to specify the dates and times to run your job.

You can get the Quartz library from official website or from maven repository.

In House Solution

If you dont wanna use any third party jar for this purpose,there is one more way.As of J2SE 1.3, Java contains the java.util.Timer and java.util.TimerTask classes that can be used for this purpose

See Also

Prince
  • 2,577
  • 2
  • 12
  • 31
1

Background threads

You need to have a background thread running and waiting for the next time an email is to be sent. Java 5 adds some classes to make such background threads and tasks quite easy.

Some other answers mentioned the Quartz library. I've not used that, and it may have nice features. But it is certainly not necessary for your purpose. The classes built into Java suffice.

ServletContextListener

Create a subclass of ServletContextListener. This class is guaranteed to be called before any servlet request is processed. So this is the place to initialize objects used by your web app. This class is also called when your web app is shutting down.

ScheduledExecutorService

In your servlet context listener, when your web app launches, instantiate a ScheduledExecutorService. This class uses a thread pool to run tasks in the background. You specify an initial delay before first run (optional), and specify how often to repeat the task.

In your servlet context listener, be sure to shutdown your ScheduledExecutorService when your web app is shutting down. If you do not, the background threads in the pool continue to live. The java process launched on your host operating system to run your servlet container will continue rather than exit.

Be sure to add a try-catch around your task. Any Exception reaching your ScheduledExecutorService causes the service to silently stop. See this funny post (caution, naughty language).

Search StackOverflow for more details and examples. This Question is basically a duplicate, so I kept this Answer brief.

Basil Bourque
  • 218,480
  • 72
  • 657
  • 915