如果上下文存储在数据库中,则在springbatch中使用Charset编码问题

时间:2016-11-18 10:47:28

标签: spring-batch

我正在使用带有JDBC的SpringBatch来存储作业上下文并面临序列化非ascii字符的问题。看起来像'JdbcExecutionContextDao'类的字符编码设置为'ISO-8859-1'。无论如何将编码指定为'UTF-8'

1 个答案:

答案 0 :(得分:0)

我遇到了与Spring Batch 4.1.1相同的问题。我懒得从头开始编写自己的ExecutionContextSerializer。我只是将字节流转换为ISO-8859-1,并在我的BatchConfig中使用了它:

import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CustomBatchConfig extends DefaultBatchConfigurer {

    @Autowired
    private PlatformTransactionManager transactionManager;

    @Override
    public JobExplorer getJobExplorer() {

        try {
            return jobExplorerFactoryBean();
        } catch (Exception e) {

            e.printStackTrace();
            return null;
        }
    }

    @Override
    public JobRepository getJobRepository() {

        try {
            return jobRepositoryFactoryBean();
        } catch (Exception e) {

            e.printStackTrace();
            return null;
        }
    }

    private JobRepository jobRepositoryFactoryBean() throws Exception {
        JobRepositoryFactoryBean fb = new JobRepositoryFactoryBean();
        fb.setSerializer(getContextSerializer());
        fb.setDataSource(getYourDataSource());
        fb.setTransactionManager(transactionManager);
        return fb.getObject();
    }


    private JobExplorer jobExplorerFactoryBean() throws Exception {
        JobExplorerFactoryBean fb = new JobExplorerFactoryBean();
        fb.setSerializer(getContextSerializer());
        fb.setJdbcOperations(new JdbcTemplate(getYourDataSource()));
        return fb.getObject();
    }

    private ExecutionContextSerializer getContextSerializer(){
        return new LatinJacksonExecutionContextSerializer();        
    }
}


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer;
import org.springframework.stereotype.Component;

@Component
public class LatinJacksonExecutionContextSerializer extends Jackson2ExecutionContextStringSerializer {

    @Override
    public void serialize(Map<String, Object> context, OutputStream out) throws IOException {

        super.serialize(context, out);
        if (out instanceof ByteArrayOutputStream){
            try {

                //Convert to ISO-8859-1 byteStream, because JDBC ExecutionContextDao expect that
                ByteArrayOutputStream baos = (ByteArrayOutputStream) out;
                byte[] bytesUtf8 = baos.toByteArray();
                byte[] latin1 = new String(bytesUtf8, "UTF-8").getBytes("ISO-8859-1");
                baos.reset();
                //Convert to ByteArrayOutputStream
                ByteArrayInputStream in = new ByteArrayInputStream(latin1);
                IOUtils.copy(in, out);
                IOUtils.closeQuietly(in);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}