我开发了 Spring Batch示例,在这个项目中,它从Mysql
中读取数据并将其写入Redis DB
。
我创建了一个自定义编写器,该编写器将以下数据写入redis,而在编写时,该错误低于错误
2018-07-17 00:58:59 DEBUG o.s.b.core.step.tasklet.TaskletStep - Applying contribution: [StepContribution: read=100, written=0, filtered=0, readSkips=0, writeSkips=0, processSkips=0, exitStatus=EXECUTING]
2018-07-17 00:58:59 DEBUG o.s.b.core.step.tasklet.TaskletStep - Rollback for RuntimeException: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [com.prateek.model.Product] to type [java.lang.String]
2018-07-17 00:58:59 DEBUG o.s.t.support.TransactionTemplate - Initiating transaction rollback on application exception
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [com.prateek.model.Product] to type [java.lang.String]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174)
at org.springframework.data.redis.serializer.GenericToStringSerializer$Converter.convert(GenericToStringSerializer.java:120)
at org.springframework.data.redis.serializer.GenericToStringSerializer.serialize(GenericToStringSerializer.java:88)
at org.springframework.data.redis.core.AbstractOperations.rawHashValue(AbstractOperations.java:184)
at org.springframework.data.redis.core.DefaultHashOperations.put(DefaultHashOperations.java:175)
at com.prateek.writer.ProductRedisWriter.write(ProductRedisWriter.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:338)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:136)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:124)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy8.write(Unknown Source)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.writeItems(SimpleChunkProcessor.java:185)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.doWrite(SimpleChunkProcessor.java:151)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.write(SimpleChunkProcessor.java:284)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:209)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:272)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:66)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:136)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:308)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:141)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:134)
at com.prateek.App.main(App.java:29)
2018-07-17 00:58:59 DEBUG o.s.b.s.t.ResourcelessTransactionManager - Initiating transaction rollback
尝试写入的数据:
Product(productCode = S10_1678,productName = 1969 Harley Davidson Ultimate Chopper,productLine = Motorcycles,productScale = 1:10,productVendor = Min Lin Diecast,productDescription =此复制品具有工作支架,前悬架,变速杆和脚刹杆,传动链,车轮和转向装置。由于零件的精确比例,所有零件都特别精致,需要特别的保养和注意。,quantageInStock = 7933,buyPrice = 48.81,MSRP = 95.7)
ProductRedisWriter.java
@Repository
public class ProductRedisWriter implements ItemWriter<Product>{
@Resource(name="redisTemplate")
private HashOperations<String, String, Product> hashOps;
@Override
public void write(List<? extends Product> products) throws Exception {
for (Product product : products) {
System.out.println("Prodcus saved to Redis "+product.toString());
hashOps.put("Product", product.getProductCode(), product);
}
}
}
Product.java
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Product {
private String productCode;
private String productName;
private String productLine;
private String productScale;
private String productVendor;
private String productDescription;
private Integer quantityInStock;
private Double buyPrice;
private Double MSRP;
}
database.xml
<bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="hashKeySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="hashValueSerializer" class="org.springframework.data.redis.serializer.GenericToStringSerializer" >
<constructor-arg name="type" value="java.lang.String" />
</bean>
<bean id="valueSerializer" class="org.springframework.data.redis.serializer.GenericToStringSerializer" >
<constructor-arg name="type" value="java.lang.String" />
</bean>
<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:enableTransactionSupport="true"
p:connection-factory-ref="jedisConnectionFactory"
p:keySerializer-ref="keySerializer"
p:hashKeySerializer-ref="hashKeySerializer"
p:valueSerializer-ref="valueSerializer"
p:hashValueSerializer-ref="hashValueSerializer" />