Skip to content

ADD convient method in MQProducer which return CompletableFuture<SendResult>(添加返回CompletableFuture<SendResult>的异步发送方法) #4120

@dugenkui03

Description

@dugenkui03

FEATURE REQUEST

I suggest that ADD convient method in MQProducer which return CompletableFuture<SendResult>.

*功能要求(中文描述)

DefaultMQProducer 中的异步发送方法没有返回Future,因此开发者想要在后续的业务处理中使用结果将会很困难。我建议新增带有 CompletableFuture<SendResult> 返回结果的异步发送方法。

  • 接口定义
    CompletableFuture<SendResult> sendAsync(final Message msg, final SendCallback sendCallback) throws MQClientException,
            RemotingException, InterruptedException;
  • 大致实现
   public CompletableFuture<SendResult> sendAsync(Message msg, SendCallback sendCallback) {
        CompletableFuture<SendResult> asyncResult = CompletableFuture.supplyAsync(() -> {
            try {
                return this.send(msg);
            } catch (MQClientException | MQBrokerException | InterruptedException | RemotingException e) {
                throw new RuntimeException(e);
            }
        }, getAsyncSenderExecutor());

       // 回调中可能对结果进行修改,因此获取回调完成之后的 Future
        CompletableFuture<SendResult> resultAfterCallBack = asyncResult.whenComplete((res, ex) -> {
            if (ex instanceof RuntimeException
                    && (ex.getCause() instanceof MQClientException
                    || ex.getCause() instanceof MQClientException
                    || ex.getCause() instanceof MQClientException
                    || ex.getCause() instanceof MQClientException)
            ) {
                sendCallback.onException(ex.getCause());
                return;
            }

            if (ex != null) {
                sendCallback.onException(ex);
                return;
            }

            sendCallback.onSuccess(res);
        });

        return resultAfterCallBack;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions