views:

792

answers:

1

Hello good fellas!

I've created a small console application to see how quartz work and it was easy to create an applicationcontext object inside the main method to get the cron run. OK now I'm in a real project managed by maven and which is using cron jobs defined in some of the modules. Each of the module has his own spring config file. I had 3 of the modules using quartz so it was setup in each of the spring config file. The web app module is the one who has the dependency of each of the modules.

Now i had few concerns:

  1. should I created the applicationcontext as in the console project or it's supposed to be loaded. If yes, where am I supposed to load it.

  2. based on research on the Internet I did on line I use MethodInvokingJobDetailFactoryBean for easy unit testing. And now that I have to use the CronExpression class to test the getNextValidTimeAfter, I still don't know how to organize it properly

Can anyone give me a hand. I'd really appreciate it. Thanks for reading

+4  A: 

As per comment, the question is closer to "How to load Spring application context file(s) for a Web application".

According to Section 3.8.5, "Convenient ApplicationContext instantiation for web applications", you can register an ApplicationContext using the ContextLoaderListener as follows (add this to your web.xml file):

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- or use the ContextLoaderServlet instead of the above listener
<servlet>
  <servlet-name>context</servlet-name>
  <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
-->
Pascal Thivent
pascal i really appreciate your effort but i don't think you 've understood me.English not being my language.right now i don't have any problem wiring the bean and the trigger and the schedulerfactorybean since i've done it already for my console project.what i meant was that now that i'm in a web application and that i don't have the java main method to call the applicationcontext object in, i'm lost about how to go about it in a wep app environment.i edited the post i was writing getNextValidTimeAfter not getNext. thanks
black sensei
Ooooohh, I think I get it now. I'll update my answer.
Pascal Thivent
pascal i start to see the light now.One concern.if i go by the listener , i'll just have to write all the applicaitoncontexts inside the param-value right?but the application contexts are in different modules so in my case the using the servlet is suitable.correct me if i'm wrong.and thank you very very much
black sensei
Filter or Servlet, you have to declare the `<context-param>` in both cases, this part doesn't change. Then, in order to allow Spring to find the application context files, you just need to put them on the classpath of the webapp (i.e. the modules need to be packaged in the WEB-INF/lib directory). But this should already be the case.
Pascal Thivent
thanks you so much for your help.i found that all the other maven module applicationcontext should be available in the web-app /META-INF so i'll just add to the existing webappcontext those like `<context-param>/WEB-INF/webappcontext.xml module1contextxml module2context.xml</context-param>`. thanks do you have any idea about the unit testing
black sensei
To be honest, I'm not sure what you want to "unit" test. `CronExpression#getNextValidTimeAfter(java.util.Date)` is a method from the Quartz API and is a supposed to be working.
Pascal Thivent