在Spring Boot应用程序的同一类中如何启动新线程

huangapple go评论88阅读模式
英文:

How to start a new Thread in spring boot app from the same class

问题

@Service
public class CreationRequestServiceImpl implements CreationRequestService {

    @Autowired
    private MessageSender messageSender;

    @Autowired
    private AsyncMessageSender asyncMessageSender;

    @Override
    public void sendMessage() {

        sendMessageToManager(createParamMap(/*some param*/));

        //send message to customer async
        asyncMessageSender.run(
            () -> sendMessageToCustomer(createParamMap(/*some param*/))
        );
    }

    private void sendMessageToManager(Map<String, String> param) {
        MessageTemplate messageTemplate;
        //some code
        messageSender.send(messageTemplate);
    }

    private void sendMessageToCustomer(Map<String, String> param) {
        MessageTemplate messageTemplate;
        //some code
        messageSender.send(messageTemplate);
    }

    private Map<String, String> createParamMap(/*some param*/) {
        Map<String, String> param;
        //some code
        return param;
    }
}

QUESTION:

  1. 如何将Thread(...).start()替换为异步启动方法?或者
  2. 如何使第二个示例起作用?

(请注意,我只回答了你提供的内容,并没有添加任何额外的信息。)

英文:

everyone. I have a service class in my spring boot application. Where I should call one method sync and one async.

@Service
public class CreationRequestServiceImpl implements CreationRequestService {

    @Autowired
    private MessageSender messageSender;

    @Override
    public void sendMessage() {

        sendMessageToManager(createParamMap(/*some param*/));

        //send message to customr async
        new Thread(
            () -&gt; sendMessageToCustomer(createParamMap(/*some param*/))
        ).start();
    }

    private void sendMessageToManager(Map&lt;String, String&gt; param) {
        MessageTemplate messageTemplate;
        //some code
        messageSender.send(messageTemplate)
    }

    private void sendMessageToCustomer(Map&lt;String, String&gt; param) {
        MessageTemplate messageTemplate;
        //some code
        messageSender.send(messageTemplate)
    }

    private Map&lt;String, String&gt; createParamMap(/*some param*/) {
        Map&lt;String, String&gt; param;
        //some code
        return param;
    }
}

I use new Thread(...).start() to invoke method async.
But I think it is not a good idea.

I did the following changes:

I define Executor bean in Configuration, add @EnableAsync

    @Bean(name = &quot;taskExecutor&quot;)
    public Executor getAsyncExecutor() {
        //some code
    }

create a new service for async invocation

@Service
public class AsyncMessageSenderImpl implements AsyncMessageSender {

    @Async(&quot;taskExecutor&quot;)
    @Override
    public void run(Runnable runnable) {
        runnable.run();
    }
}

Refactor my CreationRequestServiceImpl class

@Service
public class CreationRequestServiceImpl implements CreationRequestService {

    @Autowired
    private MessageSender messageSender;

    @Autowired
    private AsyncMessageSender asyncMessageSender;

    @Override
    public void sendMessage() {

        sendMessageToManager(createParamMap(/*some param*/));

        //send message to customr async
        asyncMessageSender.run(
            () -&gt; sendMessageToCustomer(createParamMap(/*some param*/))
        );
    }

    private void sendMessageToManager(Map&lt;String, String&gt; param) {
        MessageTemplate messageTemplate;
        //some code
        messageSender.send(messageTemplate)
    }

    private void sendMessageToCustomer(Map&lt;String, String&gt; param) {
        MessageTemplate messageTemplate;
        //some code
        messageSender.send(messageTemplate)
    }

    private Map&lt;String, String&gt; createParamMap(/*some param*/) {
        Map&lt;String, String&gt; param;
        //some code
        return param;
    }
}

But this is not working async.

QUESTION:

  1. How can I replace Thread(...).start() to start my method async. OR
  2. How can I make the second example to work?

答案1

得分: 4

为什么不使用CompletableFuture,它更优雅,并且在异常处理和函数式编码方面为您提供了许多强大的选项。
以下是我会这样做的示例代码 -

@Override
public void sendMessage() {

    sendMessageToManager(createParamMap(/*一些参数*/ ));
    CompletableFuture.runAsync(() -> sendMessageToCustomer(createParamMap(/*一些参数*/ )));
}
英文:

Why not use CompletableFuture, which is more elegant and gives you lots of powerful option in terms of exception handling and functional way of writing code.
Here is how I would do -

@Override
public void sendMessage() {

    sendMessageToManager(createParamMap(/*some param*/ ));
    CompletableFuture.runAsync(() -&gt; sendMessageToCustomer(createParamMap(/*some param*/ )));
}

huangapple
  • 本文由 发表于 2020年7月26日 15:31:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63097344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定