如何在春季批处理中的工作中检索taskexecutionID?
(出于记录目的,)我需要检索(任务)执行ID 在Spring Cloud数据流中的作业执行中。特别是,我可以访问多个“作业”对象( Joblauncher,JobExplorer,Jobrepository,JobExecution
...),它们似乎与任务没有关联。此外,类似 @value(“ {$ spring.cloud.task.task.execution Id:}”)
似乎不起作用。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用。
您可以让任何BEAN实施
taskexecutionListener
(或使用注释@beforetask
,@aftertask
,@failedtask
)和从作为参数传递给其方法的taskexecution
中获取ID。You can use a Task Execution Listener.
You can let any bean implement
TaskExecutionListener
(or use the annotations@BeforeTask
,@AfterTask
,@FailedTask
) and take the id from theTaskExecution
that is passed as argument to its methods.我不知道您愿意做什么,但是您可以使用Spring AOP和Log4J2来完成此类任务。
首先,将log4j2添加到项目中,然后编辑log4j config file(log4j2.xml),您可以在其中通过在模式标签中指定该数字(| thread ID:%tid |)来记录运行线程的ID:
并创建一个用于类的类。方面登录物,我已经为我的情况创建了一个,它可以使用通用方法记录所有内容,您可以使用它:
您还需要为上面的类创建连接点,以指定应用程序中的软件包或层检查:
仅此而已。
不要忘记在log4j2和spring aop的pom.xml中添加所需的依赖项。
这是您可能需要在pom.xml中包含的依赖项的小概述:
最后,控制台中的日志将看起来像:
提示:如果您想每个请求都有UID,则必须有兴趣阅读此blog
I don't know what you are willing to do exactly, but you could use spring AOP and log4j2 to achieve such task.
Start by adding log4j2 to your project and edit the log4j config file (log4j2.xml) where you could log the id of the running threads by specifying that in the pattern tag (|thread id:%tid|) :
And create a class for aspect login matter, I've already created one for my case that logs everything using a generic method, you could use it:
You will need also to create join points for the class above where you specify the packages or layers in your application you want to inspect:
And that's all.
Don't forget to add required dependencies in your pom.xml for log4j2 and spring aop.
Here is a small overview of the dependencies you might need to include in your pom.xml:
Finally the logs in the console will look like that:

Hint: if you want to have UID per request, you must be interested in reading this blog