Search code examples
javaspringspring-mvccron-task

How to get Session/Request Object in child class of QuartzJobBean


I am implementing cron job schedular to send periodic email with attachment in a web application.

Problem Description : For creating a file (that will be attached with mail) in java code, I need session object to get absolute path of file.

For example :

request.getSession().getServletContext().getRealPath("/");

we use to get path.

So how can I implement/ get session object? Or is there any other way to get relative path of a file?

Note: I need to create attachmnet file in WebContent/MyFolder.


Solution

  • You cannot get HTTP session because there is no HTTP sessions in cron job. This job is asynchronous, it is running in its own thread and does not have any relationships with session. It will run even if no user connect to your server.

    So, your question is actually is how to get the path to file.

    And the answer is into your question. Pay attention on getServletContext(). You can extract servlet context from your servlet: servlet.getServletConfig().getServletContext(). So, if you have at least one servlet in your application you can put your servlet context (or better file path extracted from it) to static variable or to system property and then use it in your cron job.