首页 \ 问答 \ 如何使用不同的作业参数多次运行弹簧批处理作业?(How to run spring batch job multiple times with different job parameters?)

如何使用不同的作业参数多次运行弹簧批处理作业?(How to run spring batch job multiple times with different job parameters?)

我们用单步创建了一个简单的弹簧批处理作业。 有自定义实现的ItemReader和ItemWriter。 ItemReader从job参数获取初始数据。 当作为独立的java进程运行时,批处理运行完美。 但我们想要的是在一些服务器上托管批处理。 因此,我们已经创建了REST服务来初始化批处理。 该服务调用作业URL并传递一些参数。 此参数作为作业参数传递给批处理。 调用一个参数时,服务和作业运行正常。

但是当我们不止一次调用该服务(两次用于测试目的)时,批处理行为奇怪。 我们正在传递不同的工作参数。 但是当第二个作业初始化执行开始时,ItemReader接收的作业参数值与第一次执行的作业参数值相同。 并且两个执行都相互干扰,共享数据库连接,干扰检索的数据等。

我们已经尝试将restartable参数设置为false但它不起作用。 我们还尝试了以下解决方案:

我们可以创建同一个java(spring)批处理作业的多个实例吗?

上面的解决方案开始在JBoss中给出“中断尝试锁定”错误。

在进一步调查中,我们发现ItemReader只被初始化一次。 这就是为什么它获得相同的作业参数值并干扰先前的执行。

编辑
以下是作业配置:

<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />

<job id="jobid" restartable="false"> <step id="step1"> <tasklet> <chunk reader="reader" writer="writer" commit-interval="2"> </chunk> </tasklet> </step> </job>

以下是启动作业的代码段:

JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("jobid");

try {
    JobParameters param = new JobParametersBuilder().addString("key","value").toJobParameters();
    JobExecution execution = jobLauncher.run(job, param);
} catch (Exception e) {
    e.printStackTrace();
}

有人可以建议一些解决方案吗? 我错过了一步的配置吗? 提前致谢。


We have created a simple spring batch job with single step. There are custom implemented ItemReader and ItemWriter. The ItemReader gets the initial data from job parameter. The batch runs perfectly when run as a standalone java process. But what we want is to host the batch on some server. Therefore, we have created REST service to initialize the batch. The service calls the job URL and passes some parameter. This parameter is passed as job parameter to the batch. The service and job run fine when it is called for one parameter.

But when we call the service more than once (twice for testing purpose), the batch behaves strangely. We are passing different job parameters. But when the execution starts for second job initialization, the job parameter value which is received by the ItemReader is the same as the one for the first execution. And both execution interfere with each other, sharing database connection, interfering with data retrieved etc.

We have tried setting the restartable parameter to false but it didn't work. We have also tried the following solution:

Can we create multiple instances of a same java(spring) batch job?

The above solution started giving "Interrupted attempting lock" error in JBoss.

On further investigation we found that ItemReader is getting initialized only once. That is why it is getting same job parameter value and is interfering with the previous execution.

EDIT
Following is the job configuration:

<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />

<job id="jobid" restartable="false"> <step id="step1"> <tasklet> <chunk reader="reader" writer="writer" commit-interval="2"> </chunk> </tasklet> </step> </job>

Following is the code snippet to launch the job:

JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("jobid");

try {
    JobParameters param = new JobParametersBuilder().addString("key","value").toJobParameters();
    JobExecution execution = jobLauncher.run(job, param);
} catch (Exception e) {
    e.printStackTrace();
}

Can anyone please suggest some solution? Am I missing some configuration for the step? Thanks in advance.


原文:https://stackoverflow.com/questions/31313826
更新时间:2024-01-21 08:01

最满意答案

你非常接近; 你只需要在最后一行使用列表理解而不是生成器表达式。

Date_List = (datetime.datetime.strptime(i, "%Y-%m-%d %H:%M:%S") for i in Date_List)
Date_List = [datetime.datetime.strftime(i, "%m-%d-%Y") for i in Date_List]

我会像这样清理它:

from datetime import datetime
from pprint import pprint

timestamps = [
    '2015-08-24 00:00:00',
    '2015-08-30 00:00:00',
    '2015-08-22 00:00:00',
    '2015-08-21 00:00:00',
    '2015-08-25 00:00:00',
    '2015-08-29 00:00:00',
    ]

dates = (datetime.strptime(ts, '%Y-%m-%d %H:%M:%S') for ts in timestamps)
date_strings = [datetime.strftime(d, '%m-%d-%Y') for d in dates]

pprint(date_strings)

输出:

['08-24-2015',
 '08-30-2015',
 '08-22-2015',
 '08-21-2015',
 '08-25-2015',
 '08-29-2015']

这是一个稍微更一般化的方法:

from datetime import datetime
from pprint import pprint


def convert_timestamp(ts, from_pattern, to_pattern):
    dt = datetime.strptime(ts, from_pattern)
    return datetime.strftime(dt, to_pattern)


timestamps = [
    '2015-08-24 00:00:00',
    '2015-08-30 00:00:00',
    '2015-08-22 00:00:00',
    '2015-08-21 00:00:00',
    '2015-08-25 00:00:00',
    '2015-08-29 00:00:00',
    ]

date_strings = [convert_timestamp(ts, '%Y-%m-%d %H:%M:%S', '%m-%d-%Y')
                for ts in timestamps]

pprint(date_strings)

输出:

['08-24-2015',
 '08-30-2015',
 '08-22-2015',
 '08-21-2015',
 '08-25-2015',
 '08-29-2015']

You're very close; you just need to use a list comprehension on the last line instead of a generator expression.

Date_List = (datetime.datetime.strptime(i, "%Y-%m-%d %H:%M:%S") for i in Date_List)
Date_List = [datetime.datetime.strftime(i, "%m-%d-%Y") for i in Date_List]

I would clean it up like so:

from datetime import datetime
from pprint import pprint

timestamps = [
    '2015-08-24 00:00:00',
    '2015-08-30 00:00:00',
    '2015-08-22 00:00:00',
    '2015-08-21 00:00:00',
    '2015-08-25 00:00:00',
    '2015-08-29 00:00:00',
    ]

dates = (datetime.strptime(ts, '%Y-%m-%d %H:%M:%S') for ts in timestamps)
date_strings = [datetime.strftime(d, '%m-%d-%Y') for d in dates]

pprint(date_strings)

Output:

['08-24-2015',
 '08-30-2015',
 '08-22-2015',
 '08-21-2015',
 '08-25-2015',
 '08-29-2015']

Here's a slightly more generalized way to do it:

from datetime import datetime
from pprint import pprint


def convert_timestamp(ts, from_pattern, to_pattern):
    dt = datetime.strptime(ts, from_pattern)
    return datetime.strftime(dt, to_pattern)


timestamps = [
    '2015-08-24 00:00:00',
    '2015-08-30 00:00:00',
    '2015-08-22 00:00:00',
    '2015-08-21 00:00:00',
    '2015-08-25 00:00:00',
    '2015-08-29 00:00:00',
    ]

date_strings = [convert_timestamp(ts, '%Y-%m-%d %H:%M:%S', '%m-%d-%Y')
                for ts in timestamps]

pprint(date_strings)

Output:

['08-24-2015',
 '08-30-2015',
 '08-22-2015',
 '08-21-2015',
 '08-25-2015',
 '08-29-2015']

相关问答

更多

相关文章

更多

最新问答

更多
  • 以编程方式创建视频?(Create videos programmatically?)
  • 为什么开机慢上面还显示;Inetrnet,Explorer
  • javascript数组,如何改变这个数组结构(javascript arrays, how to change this array structure)
  • 在ASP.NET Web API中使用多个Get方法进行路由(Routing with multiple Get methods in ASP.NET Web API)
  • 用于backbone.js验证的自定义验证器(Custom validator for backbone.js validation)
  • const char *与其他指针有什么不同?(Is const char * different from other pointers? [duplicate])
  • 无效的列索引,使用PreparedStatement更新(Invalid column index , update using PreparedStatement)
  • watchOS WCSession'已配对'和'watchAppAvailable'不可用(watchOS WCSession 'paired' and 'watchAppAvailable' are unavailable)
  • CalledFromWrongThreadException在Android上执行JUnit测试(CalledFromWrongThreadException exercising JUnit tests on Android)
  • 如何把文件保存到你的应用程序目录中?(How to put\save files into your application directory? (adobe air))
  • 美元符号在Java方法描述符中的含义?(Meanings of dollar sign in Java method descriptor?)
  • font-size的含义是什么:1em / 2em?(What doe the meaning of font-size:1em/2em?)
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • Android - 检测与特定wifi ssid断开连接的正确方法?(Android - Correct way to detect disconnecting from a particular wifi ssid?)
  • 通过Shell脚本将文件转换为另一个文件(Convert File To Another File By Shell Script)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • 如何过滤magento废弃的购物车报告集合(How to Filter the magento abandoned cart report collection)
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • web api http post传递对象504接收失败(web api http post passing object 504 Receive Failure)
  • Rails从视图编辑模型上的多个属性的方法(Rails way to edit multiple attributes on a model from a view)
  • 总是用{}初始化对象是否是好习惯?(Is it good habit to always initialize objects with {}?)
  • 在方案中编写特殊字符到输出端口(编译器设计)(writing special characters to output port in scheme (compiler design))
  • 电脑等级考试得证有多大用处?
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • 第一次调用函数将无法按预期工作,但下一次工作正常(calling a function on the first time won't work as expected, but next time is working)
  • 如何优化使用BigInteger操作执行时间的代码(How to optimize the code that uses BigInteger operations for execution time)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何提供个人资料信息,以便Passport.js可以使用它?(how does Profile information should be provided so Passport.js can use it?)
  • 有没有办法初始化jquery数据表中的细节?(is there any way to initialize details in jquery datatable?)