我尝试使用分区在多线程上运行JAVAEE7批处理 我的批次很简单:读取一堆随机数,用3个线程写出它们的总和。
我的工作XML
<job id="partition" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
version="1.0">
<step id="process" next="cleanup">
<chunk item-count="3">
<reader ref="partitionProcessIR">
<properties>
<property name="start" value="#{partitionPlan['start']}" />
<property name="end" value="#{partitionPlan['end']}" />
</properties>
</reader>
<processor ref="partitionProcessIP" />
<writer ref="partitionProcessIW" />
</chunk>
<partition>
<mapper ref="partitionMapperImpl" />
</partition>
</step>
<step id="cleanup">
<batchlet ref="partitionCleanupBatchlet"></batchlet>
</step>
</job>
我的PartitionMapperImpl:
@Override
public PartitionPlan mapPartitions() throws Exception {
// TODO Auto-generated method stub
return new PartitionPlanImpl() {
@Override
public int getPartitions() {
return 3;
}
@Override
public int getThreads() {
return 3;
}
@Override
public Properties[] getPartitionProperties() {
int totalRecords = getTotalRecords();
int partItems = totalRecords / getPartitions();
int remainItems = totalRecords % getPartitions();
Properties[] props = new Properties[getPartitions()];
for (int i = 0; i < getPartitions(); i++) {
props[i] = new Properties();
props[i].setProperty("start", String.valueOf(i * partItems));
// if this is the last partition, add remaining items
if (i == getPartitions() - 1) {
props[i].setProperty("end", String.valueOf((i + 1) * partItems + remainItems));
} else {
props[i].setProperty("end", String.valueOf((i + 1) * partItems));
}
}
return props;
}
};
}
private int getTotalRecords() {
return 50;
}
我的读者:
@Override
public void open(Serializable checkpoint) throws Exception {
int start = new Integer(startProperty);
int end = new Integer(endProperty);
List<Integer> listNumber = new ArrayList<>();
for (int i = start; i < end; i++) {
int rand = (int) (Math.random() * 10);
listNumber.add(rand);
}
iterator = listNumber.iterator();
}
@Override
public Integer readItem() throws Exception {
if (iterator.hasNext()) {
return iterator.next();
}
// end read
return null;
}
我的处理器
@Override
public Integer processItem(Object arg0) throws Exception {
Integer rand = (Integer) arg0;
return rand;
}
我的作家
@Override
public void writeItems(List<Object> arg0) throws Exception {
int sum = 0;
for (Object object : arg0) {
Integer rand = (Integer) object;
sum += rand;
}
System.out.println(Thread.currentThread().getId() + " | SUM OF CHUNK: " + sum);
}
运行此批处理时,发生以下错误。 我猜这与在德比数据库中同时存储多个检查点有关。
2017-03-02T15:22:45.955 + 0700 |情报:275 | CHUNK:13 2017-03-02T15:22:45.958 + 0700 |情报:316 | CHUNK:17 2017-03-02T15:23:05.971 + 0700 |重大:读进程写入循环失败 com.ibm.jbatch.container.exception.BatchContainerServiceException: 无法保留[process]的检查点数据 com.ibm.jbatch.container.persistence.CheckpointManager.checkpoint(CheckpointManager.java:133) 在 com.ibm.jbatch.container.impl.ChunkStepControllerImpl.invokeChunk(ChunkStepControllerImpl.java:644) 在 com.ibm.jbatch.container.impl.ChunkStepControllerImpl.invokeCoreStep(ChunkStepControllerImpl.java:764) 在 com.ibm.jbatch.container.impl.BaseStepControllerImpl.execute(BaseStepControllerImpl.java:144) 在 com.ibm.jbatch.container.impl.ExecutionTransitioner.doExecutionLoop(ExecutionTransitioner.java:112) 在 com.ibm.jbatch.container.impl.JobThreadRootControllerImpl.originateExecutionOnThread(JobThreadRootControllerImpl.java:110) 在 com.ibm.jbatch.container.util.BatchWorkUnit.run(BatchWorkUnit.java:80) 在 java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511) 在java.util.concurrent.FutureTask.run(FutureTask.java:266)at org.glassfish.enterprise.concurrent.internal.ManagedFutureTask.run(ManagedFutureTask.java:141) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617) 在java.lang.Thread.run(Thread.java:745)at org.glassfish.enterprise.concurrent.ManagedThreadFactoryImpl $ ManagedThread.run(ManagedThreadFactoryImpl.java:250) 引起:com.ibm.jbatch.container.exception.PersistenceException: java.sql.SQLTransactionRollbackException: 锁:ROW,CHECKPOINTDATA,??????????????????????????????????????????? (110,27)等待XID:{77885156,S},APP,选择id,obj from CHECKPOINTDATA,其中id =?授予XID:{77885155,X} Lock:ROW, CHECKPOINTDATA,(110,28)等待XID:{77885155,S},APP,选择id, 来自CHECKPOINTDATA的obj,其中id =?授予XID:{77885156,X} ???????? XID:77885156?在 fish.payara.jbatch.persistence.rdbms.JBatchJDBCPersistenceManager.queryCheckpointData(JBatchJDBCPersistenceManager.java:503) 在 fish.payara.jbatch.persistence.rdbms.JBatchJDBCPersistenceManager.updateCheckpointData(JBatchJDBCPersistenceManager.java:388) 在 fish.payara.jbatch.persistence.rdbms.LazyBootPersistenceManager.updateCheckpointData(LazyBootPersistenceManager.java:230) 在 com.ibm.jbatch.container.persistence.CheckpointManager.checkpoint(CheckpointManager.java:128) ... 13更多引起:java.sql.SQLTransactionRollbackException: 锁:ROW,CHECKPOINTDATA,??????????????????????????????????????????? (110,27)等待XID:{77885156,S},APP,选择id,obj from CHECKPOINTDATA,其中id =?授予XID:{77885155,X} Lock:ROW, CHECKPOINTDATA,(110,28)等待XID:{77885155,S},APP,选择id, 来自CHECKPOINTDATA的obj,其中id =?授予XID:{77885156,X} ???????? XID:77885156?在 org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(未知 来源)at org.apache.derby.impl.jdbc.Util.generateCsSQLException(未知来源) 在 org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(未知 来源)at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(未知 来源)at org.apache.derby.impl.jdbc.EmbedConnection.handleException(未知 来源)at org.apache.derby.impl.jdbc.ConnectionChild.handleException(未知 来源)at org.apache.derby.impl.jdbc.EmbedResultSet.closeOnTransactionError(未知 来源)at org.apache.derby.impl.jdbc.EmbedResultSet.movePosition(未知来源) 在org.apache.derby.impl.jdbc.EmbedResultSet.next(未知来源)at at com.sun.gjc.spi.base.ResultSetWrapper.next(ResultSetWrapper.java:103) 在 fish.payara.jbatch.persistence.rdbms.JBatchJDBCPersistenceManager.queryCheckpointData(JBatchJDBCPersistenceManager.java:498) ... 16更多引起:java.sql.SQLException: 锁:ROW,CHECKPOINTDATA,??????????????????????????????????????????? (110,27)等待XID:{77885156,S},APP,选择id,obj from CHECKPOINTDATA,其中id =?授予XID:{77885155,X} Lock:ROW, CHECKPOINTDATA,(110,28)等待XID:{77885155,S},APP,选择id, 来自CHECKPOINTDATA的obj,其中id =?授予XID:{77885156,X} ???????? XID:77885156?在 org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(未知 来源)at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(未知 来源)... 27更多引起:ERROR 40001: 锁:ROW,CHECKPOINTDATA,??????????????????????????????????????????? (110,27)等待XID:{77885156,S},APP,选择id,obj from CHECKPOINTDATA,其中id =?授予XID:{77885155,X} Lock:ROW, CHECKPOINTDATA,(110,28)等待XID:{77885155,S},APP,选择id, 来自CHECKPOINTDATA的obj,其中id =?授予XID:{77885156,X} ???????? XID:77885156?在 org.apache.derby.iapi.error.StandardException.newException(未知 来源)at org.apache.derby.impl.services.locks.Deadlock.buildException(未知 来源)at org.apache.derby.impl.services.locks.ConcurrentLockSet.lockObject(未知 来源)at org.apache.derby.impl.services.locks.ConcurrentLockSet.zeroDurationLockObject(未知 来源)at org.apache.derby.impl.services.locks.AbstractPool.zeroDurationlockObject(未知 来源)at org.apache.derby.impl.services.locks.ConcurrentPool.zeroDurationlockObject(未知 来源)at org.apache.derby.impl.store.raw.xact.RowLocking2nohold.lockRecordForRead(未知 来源)at org.apache.derby.impl.store.access.conglomerate.OpenConglomerate.lockPositionForRead(未知 来源)at org.apache.derby.impl.store.access.conglomerate.GenericScanController.fetchRows(未知 来源)at org.apache.derby.impl.store.access.heap.HeapScan.fetchNextGroup(未知 来源)at org.apache.derby.impl.sql.execute.BulkTableScanResultSet.reloadArray(未知 来源)at org.apache.derby.impl.sql.execute.BulkTableScanResultSet.getNextRowCore(未知 来源)at org.apache.derby.impl.sql.execute.BasicNoPutResultSetImpl.getNextRow(未知 来源)... 20多个
您有任何想法如何解决这个问题吗? 或者任何可以在2个以上线程上运行的样本都非常有用 提前谢谢。
答案 0 :(得分:0)
在我看来,您可能遇到并发问题,例如死锁或锁定超时。 (这有点难以辨别,因为您的异常信息在问题中有点混乱,我认为,因为Derby消息是以母语字符串和英语字符串的形式打印的。)
您可以在此处找到一些策略来诊断和理解并发数据库访问遇到这些问题的原因:https://wiki.apache.org/db-derby/LockDebugging
答案 1 :(得分:0)
看起来像Payara问题,来自stacktrace中的这一行:
fish.payara.jbatch.persistence.rdbms.JBatchJDBCPersistenceManager.queryCheckpointData(JBatchJDBCPersistenceManager.java:503)
您可以尝试正确使用GlassFish运行您的应用,看看您是否遇到同样的问题。
或者您可以将应用程序部署到WildFly,其中包含JBeret作为批处理容器。如果您的应用程序已写入JSR 352规范,则应在任何Java EE 7可兼容的应用程序服务器中部署和运行。您可以将WildFly配置为使用带有Derby的jdbc作业存储库,或任何其他受支持的DBMS,包括捆绑的H2数据库。
如果你仍然被困,我建议跟进Payara项目。