springboot 长轮询实现

springboot 长轮询实现springboot长轮询实现基于@EnableAsync , @Sync@SpringBootApplication@EnableAsyncpublicclassDemoApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(DemoApplication.cla…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

springboot 长轮询实现

基于 @EnableAsync , @Sync

@SpringBootApplication
@EnableAsync
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}
@RequestMapping("/async")
@RestController
public class AsyncRequestDemo {

    @Autowired
    private AsyncRequestService asyncRequestService;

    @GetMapping("/value")
    public String getValue() {

        String msg =  null;
        Future<String> result = null;
        try{
            result = asyncRequestService.getValue();
            msg = result.get(10, TimeUnit.SECONDS);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (result != null){
                result.cancel(true);
            }
        }

        return msg;
    }

    @PostMapping("/value")
    public void postValue(String msg) {
        asyncRequestService.postValue(msg);
    }
}

@Service
public class AsyncRequestService {

    private String msg = null;

    @Async
    public Future<String> getValue() throws InterruptedException {

        while (true){
            synchronized (this){
                if (msg != null){
                    String resultMsg = msg;
                    msg = null;
                    return new AsyncResult(resultMsg);
                }
            }
            Thread.sleep(100);
        }
    }

    public synchronized void postValue(String msg) {
        this.msg = msg;
    }
}

备注

  1. @EnableAsync 开启异步
  2. @Sync 标记异步方法
  3. Future 用于接收异步返回值
  4. result.get(10, TimeUnit.SECONDS); 阻塞,超时获取结果
  5. Future.cancel() 中断线程
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/181940.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • IPaddr和IPaddr2的区别「建议收藏」

    IPaddr和IPaddr2的区别「建议收藏」Heartbeat资源配置中IPaddr和IPaddr2的区别:其主要的区别在于:IPaddr支持LVS,而IPaddr2则和LVS有冲突。参考:redflagLinux集群管理手册。 转载于:https://blog.51cto.com/dangzhiqiang/1397425…

    2022年7月27日
    14
  • [3] 算法之路 – 插入排序

    [3] 算法之路 – 插入排序

    2022年2月2日
    62
  • Drupal教程之安装篇

    Drupal教程之安装篇星期一,01/12/2009—似曾相识文章地址:[url]http://www.drupaluser.org/node/3[/url]象许多CMS一样,Drupal也是需要安装,其主要步骤如下(以[url]http://drupaluser.org/[/url]为例):1.在[url]http://drupaluser.org/[/u…

    2022年6月8日
    30
  • python语言中变量的命名规则是什么_Python中变量的命名规则

    python语言中变量的命名规则是什么_Python中变量的命名规则讲解对象:Python中变量的命名规则作者:融水公子rsgz➢>变量的命名理解Python需要使用标识符给变量命名,其实标识符就是用于给程序中变量、类、方法命名的符号(简单来说,标识符就是合法的名字)。➢>命名要求Pvthon语言的标识符必须以字母、下画线()开头,后面可以跟任意数目的字母、数字和下画线➢>注意此处的字母并不局限于26个英文字母可以包含中文字符、日文字符等…

    2022年5月4日
    61
  • win10下python环境变量设置

    win10下python环境变量设置我用的是python_2.7.3.msi,从官网下载之后,一路按照默认进行安装。安装之后配置环境变量的步骤如下:1,点“我的电脑”,右键选“属性”2,选择“高级系统设置”>选“环境变量”

    2022年7月5日
    25
  • 2.1.1 操作系统之进程的定义、特征、组成、组织

    文章目录1.进程的定义(1)程序的概念(2)进程的概念(2)进程的定义2.进程的特征3.进程的组成4.进程的组织(1)链接方式(2)索引方式1.进程的定义(1)程序的概念(2)进程的概念一个进程可以包含多个程序(2)进程的定义2.进程的特征3.进程的组成而其中最重要的就是进程控制块PCB(ProcessControlBlock)PCB简介:&nbsp…

    2022年4月13日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号