Search code examples
javaservletsquartz-schedulerinit-parameterscontext-param

How to acces other servlet's init-parameters or context's contex-parameters inside quartz jobs?


I have defined my quartz job with the XML jobs configuration as in here example 2

http://www.mkyong.com/java/example-to-run-multiple-jobs-in-quartz/

I have other servlets wich have some init-params and my web app also has some context-params.

How do I access these parameters inside my job which implements the Job class?


Solution

  • 1) One can basically access the servlet's context like this

    in web.xml

    <context-param>
        <param-name>quartz:scheduler-context-servlet-context-key</param-name>
        <param-value>ServletContext</param-value>
    </context-param>
    

    in code

        ServletContext MyServletContext = null;
        MyServletContext = (ServletContext) context.getScheduler().getContext().get("ServletContext");
    

    2) And then another servlet's parameters like this

    ServletContext.getServletRegistration("MyServlet").getInitParameter("MyInitParam");