覆盖spring批处理admin以使用mysql数据库

时间:2014-03-20 13:10:06

标签: java spring spring-mvc spring-batch spring-batch-admin

我试图在spring batch admin中使用mysql数据库而不是默认的HSQL。根据文件

http://docs.spring.io/spring-batch-admin/reference/reference.xhtmlUsing jndi datasource with spring batch admin

我将env-context.xml复制到src/main/resources/META-INF/batch/override/manager/env-context.xml并从

更改了配置值
<value>classpath:batch-${ENVIRONMENT:hsql}.properties</value>

 <value>classpath:batch-mysql.properties</value>

以下是我的完整配置。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--  Use this to set additional properties on beans at run time -->
    <bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/org/springframework/batch/admin/bootstrap/batch.properties</value>
                <value>classpath:batch-default.properties</value>
                <value>classpath:batch-mysql.properties</value>
            </list>
        </property>
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="ignoreUnresolvablePlaceholders" value="false" />
        <property name="order" value="1" />
    </bean>

</beans>

我还尝试将data-source-context.xml复制到同一文件夹并将其配置更改为mysql

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/batch" />
        <property name="username" value="root" />
        <property name="password" value="root" />
        <property name="testWhileIdle" value="true"/>
        <property name="validationQuery" value="SELECT 1"/>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!--  Initialise the database if enabled: -->
    <jdbc:initialize-database data-source="dataSource" enabled="false" ignore-failures="DROPS">
        <jdbc:script location="classpath*:/org/springframework/batch/core/schema-drop-mysql.sql"/>
        <jdbc:script location="classpath:/org/springframework/batch/core/schema-mysql.sql"/>
        <jdbc:script location="classpath:/business-schema-mysql.sql"/>
    </jdbc:initialize-database>

</beans>

但它还在使用hsql数据库吗?如何覆盖默认配置以使用mysql数据库?

3 个答案:

答案 0 :(得分:2)

您不应该替换<value>classpath:batch-${ENVIRONMENT:hsql}.properties</value>。相反,传入一个设置为mysql的环境变量ENVIRONMENT。这应该导致所有适当的组件获取正确的数据库。您可以在此处详细了解该功能:http://docs.spring.io/spring-batch-admin/reference/infrastructure.html#Environment_Settings

答案 1 :(得分:0)

如果您想尝试仅使用没有任何xml配置的注释 - 请尝试使用

HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setDatabase(Database.MYSQL); 

它正在发挥作用。我的代码可在此处找到 - http://github.com/sidnan/spring-batch-example

答案 2 :(得分:0)

我能够通过以下步骤使用上述方法获得连接

首先,我将env-context.xml复制到src/main/resources/META-INF/batch/override/manager/env-context.xml

<?xml version="1.0" encoding="UTF-8"?>`
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">`

    <!--  Use this to set additional properties on beans at run time -->
    <bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/org/springframework/batch/admin/bootstrap/batch.properties</value>
                <value>classpath:batch-default.properties</value>
                <value>classpath:batch-${ENVIRONMENT:sqlserver}.properties</value>
            </list>
        </property>
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="ignoreUnresolvablePlaceholders" value="false" />
        <property name="order" value="1" />
    </bean>

</beans>

之后,将以下条目放在资源下的batch-sqlserver.properties sql server中作为

# Default placeholders for database platform independent features 
batch.remote.base.url=http://localhost:8080/spring-batch-admin-sample
# Non-platform dependent settings that you might like to change

batch.jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
batch.jdbc.url=jdbc:sqlserver://localhost;databaseName=batchrepo
batch.jdbc.user=la
batch.jdbc.password=la
atch.jdbc.testWhileIdle=true
batch.data.source.init=false
batch.jdbc.validationQuery=

batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.SqlServerMaxValueIncrementer
batch.lob.handler.class=org.springframework.jdbc.support.lob.DefaultLobHandler
batch.database.incrementer.parent=columnIncrementerParent
batch.grid.size=2
batch.jdbc.pool.size=6
batch.verify.cursor.position=true
batch.isolationlevel=ISOLATION_SERIALIZABLE
batch.initializer.enabled=false

由于我的表已在数据库中创建,因此我跳过了以下条目:     #batch.drop.script = /组织/ springframework的/批次/型芯/架构的下拉式sqlserver.sql     #batch.schema.script = /组织/ springframework的/批次/型芯/架构sqlserver.sql     #batch.business.schema.script =业务架构sqlserver.sql

最后,通过batch.initializer.enabled=false,我终于能够建立连接。

我可以在我的管理应用程序中监视工作以及午餐新工作。这些已启动的作业也会出现在数据库中。