Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Extracting values from Job Parameters in Spring Batch

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone.

The situation is, I have a Batch Job that is set up to read messages and stats from all the queues across multiple brokers in an environment. But I want to have a separate deployment of the job for each broker environment and have things configured that between broker environments, all I have to do is change the properties in my .properties file. What I would like to do is set up my job so that a value is passed to through the command line that tells it which properties file it uses. So it has to do this in the Application Context in my configuration of the PropertyPlaceholderConfigurer.

But I'm running into an issue where it's not resolving they way I thought it would.



Its resolving the location to "classpath:#{jobParameters[env]}.properties" and not what I'm passing in as the parameter. So basically, can I even use values passed to the command line in the Application context this way? How do I go about doing this because I'm definitely missing something. Is there some additional wiring I have to do? Can I even extract Job Parameters in this manner in the Application Context?

Thank you in advance for any and all advice/help!
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you have the right idea. You can use the Spring Expression Language to get values from the job parameters, just like you are doing. But what is env? in your SPel you have env in the brackets. Is this supposed to be the parameter name you pass in? If so, the name needs to be in quotes. Typically single quotes like

#{jobParameters['parameterNameHere']}

You have a PropertyPlaceholderConfigurer, and that gets evaluated when the ApplicationContext is created and not anytime after that, so when you run your job, this is well after the ApplicationContext has already been created. You can use the <util:properties> to load the properties file and then use its id in your <step> <job> definitions.

Hope that helps enough.

Mark

 
Steve Zukowski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the quick answer mark! I'm definitely getting onto the right track but I think I'm still a bit off.

Yes, env was supposed to be the parameter name I pass in. Thank you for the catch on the quotes, I looked right over that heh.

So it's not possible to us SpEL to extract Job Parameters in the Application context? I'm a little fuzzy on when things such as that vs the ApplicationContext are evaluated and resolved. Could I get a little more detail on this? I profess this is a weak point in my understanding of Spring etc and I'd very much like to fill it in. Or if anything, point me to a resource I can read through for this?

Also I am curious about this <until:properties> usage you mention. I've not used that before and I'm wondering if you could go into more detail on that? An example would be highly appreciated! Essentially I have my job set up in such a way as all I need to do is get it to use a different Properties file for each setup.
 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is actually taken from SpringSource's Enterprise Integration with Spring labs. But only a small portion.

For defining a Read they have the reader reading in a file, where the file location is passed in through the job parameters



Since the scope of that bean is "step" means that each time the Batch job runs and runs that step, the Reader is instantiated at that point, which is after you called the job launcher and passed in a job parameter for input.resource.path. So that means when it needs the true value, it is actually available.

In your configuration, the only time it can be evaluated when the bean gets instantiated is when the ApplicationContext is being created, therefore no value for the job parameter.

Or if you want the value for that job parameter to come from a .properties file, create a bean for it using <util:properties location="locationOfPropertiesFile" then that bean will be available all throughout the existence of your ApplicationContext.

PropertyPlaceholderConfigurer is a BeanFactoryPostProcessor, which runs right after Spring reads in the configuration and before Spring will instantiate any of your beans. So once you are past the BeanFactoryPostProcessor step then the PropertyPlaceholderConfigurer is "not around".

Mark>
 
    Bookmark Topic Watch Topic
  • New Topic