英文:
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:
- 如何将Thread(...).start()替换为异步启动方法?或者
- 如何使第二个示例起作用?
(请注意,我只回答了你提供的内容,并没有添加任何额外的信息。)
英文:
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(
() -> sendMessageToCustomer(createParamMap(/*some param*/))
).start();
}
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;
}
}
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 = "taskExecutor")
public Executor getAsyncExecutor() {
//some code
}
create a new service for async invocation
@Service
public class AsyncMessageSenderImpl implements AsyncMessageSender {
@Async("taskExecutor")
@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(
() -> 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;
}
}
But this is not working async.
QUESTION:
- How can I replace Thread(...).start() to start my method async. OR
- 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(() -> sendMessageToCustomer(createParamMap(/*some param*/ )));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论