te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Micrometer tracing: Propagate context to spring batch (spring boot 3) SimpleAsyncTaskExecutor tasks - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Micrometer tracing: Propagate context to spring batch (spring boot 3) SimpleAsyncTaskExecutor tasks - Stack Overflow

programmeradmin3浏览0评论

I am using spring boot 3 / spring batch and I would like to propagate the context across the spring batch SimpleAsyncTaskExecutor steps for distributed tracing (micrometer). In my case, the main flow splits into parallel flow and back to main flow. I would like the trace id to be the same across all the parent and child flows. I noticed the steps in the main flow have the same trace id, but the parallel flows have a different trace id.

Please advise. Thanks

ps Took similar approach to answer below:

    @Bean
    public TaskDecorator taskFlowDecorator() {
        return new ContextPropagatingTaskDecorator();
    }

    @Bean("taskFlowExecutor")
    public TaskExecutor taskFlowExecutor(TaskDecorator taskFlowDecorator) {
        return new SimpleAsyncTaskExecutorBuilder()
                .threadNamePrefix("work-flow-")
                .taskDecorator(taskFlowDecorator)
                .build();
    }

I am using spring boot 3 / spring batch and I would like to propagate the context across the spring batch SimpleAsyncTaskExecutor steps for distributed tracing (micrometer). In my case, the main flow splits into parallel flow and back to main flow. I would like the trace id to be the same across all the parent and child flows. I noticed the steps in the main flow have the same trace id, but the parallel flows have a different trace id.

Please advise. Thanks

ps Took similar approach to answer below:

    @Bean
    public TaskDecorator taskFlowDecorator() {
        return new ContextPropagatingTaskDecorator();
    }

    @Bean("taskFlowExecutor")
    public TaskExecutor taskFlowExecutor(TaskDecorator taskFlowDecorator) {
        return new SimpleAsyncTaskExecutorBuilder()
                .threadNamePrefix("work-flow-")
                .taskDecorator(taskFlowDecorator)
                .build();
    }
Share edited 2 days ago user518066 asked Feb 17 at 23:07 user518066user518066 1,4295 gold badges27 silver badges41 bronze badges 6
  • Not completely about your case, but maybe it'll help stackoverflow/a/79435244/7613649 . – Geba Commented Feb 18 at 3:10
  • I have tried to create a simple example of what you described and used TaskDecorator as suggested in the answer above It has helped - all steps/flows of one job share the same trace id. So, I believe the answer I provided above should help you. If not, please share a minimal reproducible example. – Geba Commented 2 days ago
  • Thank you for accepting my answer! I would suggest to not add answer to the question box, there is an answer box specifically for this point There is a post about it meta.stackoverflow/questions/387723/… – Geba Commented 2 days ago
  • I find your question helpful for other devs. I hope you'll not mind if I edit your question by removing the answer from it and mention SimpleAsyncTaskExecutorBuilder in my answer. In order to keep answer and question in correct places. Also I will clear comment section later. Thanks! – Geba Commented 2 days ago
  • The first link gave me the clue. Thanks. But found the solution independently before I saw the answer. – user518066 Commented 2 days ago
 |  Show 1 more comment

1 Answer 1

Reset to default 2

The approach described in this answer could be adapted to propagate context (e.g. thread-local variables) to Spring Batch child flows or steps.

If you want to have one traceId in all steps or flows of the Spring Batch job, then

  • set ContextPropagatingTaskDecorator to SimpleAsyncTaskExecutor:
@Bean
public TaskDecorator taskDecorator() {
    return new ContextPropagatingTaskDecorator();
}

@Bean
public TaskExecutor taskExecutor(TaskDecorator taskDecorator) {
    var taskExecutor = new SimpleAsyncTaskExecutor();
    executor.setTaskDecorator(taskDecorator);
    return taskExecutor;
}
  • use the decorated executor in your flows/steps:
// example of flow
FlowBuilder<SimpleFlow>("parallelFlow")
    .split(taskExecutor)
// example of step
StepBuilder("mainStep", jobRepository)
    ...
    .taskExecutor(taskExecutor)

Another approach how to create a decorated executor suggested by the question's author:

@Bean
public TaskExecutor taskExecutor(TaskDecorator taskDecorator) {
    return new SimpleAsyncTaskExecutorBuilder()
        .threadNamePrefix("work-flow-")
        .taskDecorator(taskDecorator)
        .build();
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论