dubbo复习:(12)服务器端的异步和客户端的异步调用

一、服务器端接口的定义和实现:

package cn.edu.tju.service;

import java.util.concurrent.CompletableFuture;

public interface AsyncService {
    /**
     * 同步调用方法
     */
    String invoke(String param);
    /**
     * 异步调用方法
     */
    CompletableFuture<String> asyncInvoke(String param);
}

package cn.edu.tju.service;

import org.apache.dubbo.config.annotation.DubboService;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;

@DubboService
public class AsyncServiceImpl implements AsyncService {

    @Override
    public String invoke(String param) {
        try {
            long time = ThreadLocalRandom.current().nextLong(1000);
            Thread.sleep(time);
            StringBuilder s = new StringBuilder();
            s.append("AsyncService invoke param:").append(param).append(",sleep:").append(time);
            return s.toString();
        }
        catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return null;
    }

    @Override
    public CompletableFuture<String> asyncInvoke(String param) {
        // 建议为supplyAsync提供自定义线程池
        return CompletableFuture.supplyAsync(() -> {
            try {
                // Do something
                long time = ThreadLocalRandom.current().nextLong(1000);
                Thread.sleep(time);
                StringBuilder s = new StringBuilder();
                s.append("AsyncService asyncInvoke param:").append(param).append(",sleep:").append(time);
                return s.toString();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            return null;
        });
    }
}

二、客户端的异步调用:

package cn.edu.tju.service;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

@Component
public class AsyncConsumer implements CommandLineRunner {
    @DubboReference
    private AsyncService asyncService;

    @Override
    public void run(String... args) throws Exception {
        //调用异步接口
        CompletableFuture<String> future1 = asyncService.asyncInvoke("async call request1");
        future1.whenComplete((v, t) -> {
            if (t != null) {
                t.printStackTrace();
            } else {
                System.out.println("AsyncTask Response-1: " + v);
            }
        });
        //两次调用并非顺序返回
        CompletableFuture<String> future2 = asyncService.asyncInvoke("async call request2");
        future2.whenComplete((v, t) -> {
            if (t != null) {
                t.printStackTrace();
            } else {
                System.out.println("AsyncTask Response-2: " + v);
            }
        });
        //consumer异步调用
        CompletableFuture<String> future3 =  CompletableFuture.supplyAsync(() -> {
            return asyncService.invoke("invoke call request3");
        });
        future3.whenComplete((v, t) -> {
            if (t != null) {
                t.printStackTrace();
            } else {
                System.out.println("AsyncTask Response-3: " + v);
            }
        });

        System.out.println("AsyncTask Executed before response return.");
    }
}
相关推荐
《黑巧克力》18 小时前
【JavaEE】多线程进阶
java·spring·java-ee·maven·dubbo·idea
菜鸟long20 小时前
Dubbo源码解析-过滤器Filter
dubbo
基哥的奋斗历程3 天前
springboot整合Camunda实现业务
java·spring boot·dubbo
迷茫的羔羊羊4 天前
Dubbo负载均衡策略都有哪些(简单描述)
运维·负载均衡·dubbo
泰迪智能科技6 天前
泰迪智能科技实验室产品-云计算资源管理平台介绍
科技·云计算·dubbo
怪兽也会哭哭7 天前
微服务应用与开发知识点练习【Gateway,OpenFeign,Dubbo,RocketMQ和RabbitMQ,JPA,Redis,Mycat】
微服务·gateway·rabbitmq·dubbo·rocketmq
哒不溜-w17 天前
springBoot+mongoDB项目中,使用MongoFactory、MongoTemplate分页条件查询,增删查改
spring boot·mongodb·dubbo
中国胖子风清扬18 天前
【实战指南】SpringBoot结合Zookeeper/Nacos构建Dubbo微服务
spring boot·分布式·spring cloud·微服务·zookeeper·dubbo·java-zookeeper
文天大人20 天前
Dubbo-使用zookeeper作为注册中心时节点的概述
dubbo
wudongfang66621 天前
ensp防火墙web密码重置(前提通过console可以登录)
dubbo