Results 1 to 5 of 5

Thread: Spring batch admin sample - add jobs

  1. #1
    Join Date
    Oct 2009
    Location
    San Francisco CA
    Posts
    42

    Default Spring batch admin sample - add jobs

    I downloaded the spring batch admin sample through STS and would like to add launch-able jobs.

    The domain services and context is present in a jar, I added it to the sample project as a maven dependency. Then created a job-context.xml in the META-INF/batch directory with a job using the beans in the domain service context.

    The domain context is present under META-INF/spring/applicationContext.xml, it does not have a dataSource definition, only the transactionManager.

    There are two (possible related) issues

    1. The tests fail since the dataSource referred by the applicationContext is not found during startup because it couldn't resolve the properties

    Code:
    Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in file [C:\app\target\classes\META-INF\spring\applicationContext.xml]: Could not resolve placeholder 'database.driverClassName'
    	at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:271)
    	at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75)
    	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:624)
    	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:599)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:398)
    	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
    	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
    	at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
    	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
    	... 28 more
    This is the applicationContext.xml

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    	<!--
    		This will automatically locate any and all property files you have
    		within your classpath, provided they fall under the META-INF/spring
    		directory. The located property files are parsed and their values can
    		then be used within application context files in the form of
    		${propertyKey}.
    	-->
    	<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
    
    	<!--
    		Turn on AspectJ @Configurable support. As a result, any time you
    		instantiate an object, Spring will attempt to perform dependency
    		injection on that object. This occurs for instantiation via the "new"
    		keyword, as well as via reflection. This is possible because AspectJ
    		is used to "weave" Roo-based applications at compile time. In effect
    		this feature allows dependency injection of any object at all in your
    		system, which is a very useful feature (without @Configurable you'd
    		only be able to dependency inject objects acquired from Spring or
    		subsequently presented to a specific Spring dependency injection
    		method). Roo applications use this useful feature in a number of
    		areas, such as @PersistenceContext injection into entities.
    	-->
    	<context:spring-configured/>
    
    	<!--
    		This declaration will cause Spring to locate every @Component,
    		@Repository and @Service in your application. In practical terms this
    		allows you to write a POJO and then simply annotate the new POJO as an
    		@Service and Spring will automatically detect, instantiate and
    		dependency inject your service at startup time. Importantly, you can
    		then also have your new service injected into any other class that
    		requires it simply by declaring a field for your service inside the
    		relying class and Spring will inject it. Note that two exclude filters
    		are declared. The first ensures that Spring doesn't spend time
    		introspecting Roo-specific ITD aspects. The second ensures Roo doesn't
    		instantiate your @Controller classes, as these should be instantiated
    		by a web tier application context. Refer to web.xml for more details
    		about the web tier application context setup services.
    		
    		Furthermore, this turns on @Autowired, @PostConstruct etc support. These 
    		annotations allow you to use common Spring and Java Enterprise Edition 
    		annotations in your classes without needing to do any special configuration. 
    		The most commonly used annotation is @Autowired, which instructs Spring to
    		dependency inject an object into your class.
    	-->
    	<context:component-scan base-package="com.app">
    		<context:exclude-filter expression=".*_Roo_.*" type="regex"/>
    		<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    	</context:component-scan>
    
    	<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
        </bean>
        
        <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
        <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    </beans>
    2. The item reader bean in the job-context.xml fails to load since it refers to a property which I added to batch-hsql.properties. I tried placing this property in a another file under META-INF, META-INF/spring and nothing worked.

    Code:
    Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'assetItemReader' defined in file [C:\app\target\classes\META-INF\batch\job-context.xml]: Could not resolve placeholder 'readSql'
    	at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:271)
    	at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75)
    	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:624)
    	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:599)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:398)
    	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
    	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
    	at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
    	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
    	... 28 more
    Please help me with this issue.

  2. #2
    Join Date
    Oct 2009
    Location
    San Francisco CA
    Posts
    42

    Default

    I also tried all of the following that didn't work

    a) removing the property-placeholder definition from the applicationContext.xml
    b) changing the location of the properties to classpath, classpath/META-INF, classpath/META-INF/spring (i know this syntax is not correct, but you get the picture)
    c) removing the META-INF/spring folder from the webapp and hoping that the execution-context.xml in the bootstrap folder will pick up the applicationContext.xml in the jar file

  3. #3
    Join Date
    Jun 2005
    Posts
    4,232

    Default

    I think your additional PropertyPlaceholderConfigurer is getting loaded before the one from the framework, and it has strict settings, so it causes a failure on a bean that you already know it will not resolve. You can set it up to ignore unresolvable properties, or make it load the batch-*.properties from the framework as well as your custom ones.

  4. #4
    Join Date
    Oct 2009
    Location
    San Francisco CA
    Posts
    42

    Default

    Changing the applicationContext.xml to

    Code:
    <context:property-placeholder location="classpath*:META-INF/spring/*.properties" ignore-unresolvable="true"/>
    did not fix the first issue.

    The META-INF/spring does have a properties file, why is the appContext in the jar not able to pick it up (or the property file in the same path in the jar for that matter)?

    The second one on the item reader bean went away.

  5. #5
    Join Date
    Oct 2009
    Location
    San Francisco CA
    Posts
    42

    Default

    Thanks a lot, Dave. It works after I added the "database.driverClassName" required by the context in the jar file to batch-*.properties. It did not work since the property in the batch-*.property was called "batch.jdbc.driver".

    So now I think the dataSource for the jar file would be the one configured with "database.*" properties and the batch datasource would be "batch.jdbc.*".

    It is clear though that the PropertyPlaceholderConfigurer in the jar is ignored.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •