{"action":"created","issue":{"url":"https://api.github.com/repos/spring-projects/spring-framework/issues/1993","repository_url":"https://api.github.com/repos/spring-projects/spring-framework","labels_url":"https://api.github.com/repos/spring-projects/spring-framework/issues/1993/labels{/name}","comments_url":"https://api.github.com/repos/spring-projects/spring-framework/issues/1993/comments","events_url":"https://api.github.com/repos/spring-projects/spring-framework/issues/1993/events","html_url":"https://github.com/spring-projects/spring-framework/pull/1993","id":370516118,"node_id":"MDExOlB1bGxSZXF1ZXN0MjIzMTczNzk3","number":1993,"title":"Increase the preprocessing when executing asynchronous calls.","user":{"login":"hqch0708","id":17590800,"node_id":"MDQ6VXNlcjE3NTkwODAw","avatar_url":"https://avatars0.githubusercontent.com/u/17590800?v=4","gravatar_id":"","url":"https://api.github.com/users/hqch0708","html_url":"https://github.com/hqch0708","followers_url":"https://api.github.com/users/hqch0708/followers","following_url":"https://api.github.com/users/hqch0708/following{/other_user}","gists_url":"https://api.github.com/users/hqch0708/gists{/gist_id}","starred_url":"https://api.github.com/users/hqch0708/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hqch0708/subscriptions","organizations_url":"https://api.github.com/users/hqch0708/orgs","repos_url":"https://api.github.com/users/hqch0708/repos","events_url":"https://api.github.com/users/hqch0708/events{/privacy}","received_events_url":"https://api.github.com/users/hqch0708/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-10-16T09:19:23Z","updated_at":"2018-10-16T09:19:24Z","closed_at":null,"author_association":"NONE","pull_request":{"url":"https://api.github.com/repos/spring-projects/spring-framework/pulls/1993","html_url":"https://github.com/spring-projects/spring-framework/pull/1993","diff_url":"https://github.com/spring-projects/spring-framework/pull/1993.diff","patch_url":"https://github.com/spring-projects/spring-framework/pull/1993.patch"},"body":"When the method is marked @Async, it allows developers to customize the pre-processing to meet functional requirements when performing asynchronous calls through a thread pool.\r\n\r\nFor example, you can customize ThreadFactory to pass information from ThreadLocal to asynchronous threads as ThreadName when you create a thread, and then perform pre-processing at the asynchronous call.\r\n\r\n\r\n```\r\npublic class ThreadTrace {\r\n\r\n    private static ThreadLocal<String> traceId = new ThreadLocal<>();\r\n\r\n    public static void addTrace(String uuid){\r\n        traceId.remove();\r\n        traceId.set(uuid);\r\n    }\r\n\r\n    public static String getTrace(){\r\n        return traceId.get();\r\n    }\r\n}\r\n```\r\n\r\n```\r\nprivate AtomicInteger count = new AtomicInteger(0);\r\n\r\n    @Bean\r\n    public AsyncTaskExecutor myTaskExecutor() {\r\n        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();\r\n        executor.setMaxPoolSize(100);\r\n        executor.setQueueCapacity(50);\r\n        executor.setThreadFactory(new ThreadFactory() {\r\n            @Override\r\n            public Thread newThread(Runnable r) {\r\n                Thread thread = new Thread(r);\r\n                thread.setName(\"AsyncThread-\" + count.incrementAndGet() + \"|\" + ThreadTrace.getTrace());\r\n                System.out.println(Thread.currentThread().getName() + \"-----create---\" + ThreadTrace.getTrace());\r\n                return thread;\r\n            }\r\n        });\r\n        return executor;\r\n    }\r\n```\r\n\r\n\r\n```\r\n@Configuration\r\npublic class TradeIdAsyncExecutionPreProcessor implements AsyncExecutionPreProcessor {\r\n\r\n    @Override\r\n    public void preProcessBeforeAsyncExecution(MethodInvocation invocation) {\r\n        String threadName = Thread.currentThread().getName();\r\n        System.out.println(threadName + \"----submit---\" + ThreadTrace.getTrace());\r\n        if (threadName.contains(\"|\")){\r\n            String[] str = Thread.currentThread().getName().split(\"\\\\|\");\r\n            ThreadTrace.addTrace(str[1]);\r\n            Thread.currentThread().setName(str[0]);\r\n        }\r\n    }\r\n}\r\n\r\n```\r\n\r\nThanks very much. You have reviewed about this pull request.\r\n"},"comment":{"url":"https://api.github.com/repos/spring-projects/spring-framework/issues/comments/430164740","html_url":"https://github.com/spring-projects/spring-framework/pull/1993#issuecomment-430164740","issue_url":"https://api.github.com/repos/spring-projects/spring-framework/issues/1993","id":430164740,"node_id":"MDEyOklzc3VlQ29tbWVudDQzMDE2NDc0MA==","user":{"login":"pivotal-issuemaster","id":17652765,"node_id":"MDQ6VXNlcjE3NjUyNzY1","avatar_url":"https://avatars2.githubusercontent.com/u/17652765?v=4","gravatar_id":"","url":"https://api.github.com/users/pivotal-issuemaster","html_url":"https://github.com/pivotal-issuemaster","followers_url":"https://api.github.com/users/pivotal-issuemaster/followers","following_url":"https://api.github.com/users/pivotal-issuemaster/following{/other_user}","gists_url":"https://api.github.com/users/pivotal-issuemaster/gists{/gist_id}","starred_url":"https://api.github.com/users/pivotal-issuemaster/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pivotal-issuemaster/subscriptions","organizations_url":"https://api.github.com/users/pivotal-issuemaster/orgs","repos_url":"https://api.github.com/users/pivotal-issuemaster/repos","events_url":"https://api.github.com/users/pivotal-issuemaster/events{/privacy}","received_events_url":"https://api.github.com/users/pivotal-issuemaster/received_events","type":"User","site_admin":false},"created_at":"2018-10-16T09:19:24Z","updated_at":"2018-10-16T09:19:24Z","author_association":"NONE","body":"@hqch0708 Please sign the [Contributor License Agreement](https://cla.pivotal.io/sign/spring?repositoryId=spring-projects/spring-framework&pullRequestId=1993)!\n\n[Click here](https://cla.pivotal.io/sync/spring?repositoryId=spring-projects/spring-framework&pullRequestId=1993) to manually synchronize the status of this Pull Request.\n\nSee the [FAQ](https://cla.pivotal.io/about) for frequently asked questions."}}
{"actor":{"display_login":"pivotal-issuemaster"}}