`
m635674608
  • 浏览: 4905389 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Java EE 8 将支持 SSE 服务器端事件

    博客分类:
  • java
 
阅读更多

SSE (Server-Sent Event) 是一个标准的机制用于服务器通过 HTTP 推送消息到客户端。人们经常把 SSE 和 WebSocket 进行比较,因为 HTML5 对两者都已支持。

对于 REST 应用而言,这是单向发布-订阅模型一个非常好的互补,REST 客户端可订阅并获取来自 REST 终端的提醒信息。Jersey 事实上已经支持 SSE。

而最新的 Servlet 规范已经实现对 SSE 的支持,你可以直接在 Servlet API 中使用 SSE。这里有一些例子

我们正在考虑 Java EE 8 对 SSE 的支持,目前主要是讨论在什么地方实现 SSE:

  • the Servlet API

  • the WebSocket API

  • JAX-RS

  • 或者是一个单独的 SSE API 或者 JSR

via oracle

 

  • SSE(server send event)是一种服务器端向浏览器推送消息的技术,而不是我们常规的浏览器像server请求然后响应;
  • 当我们需要使用server向浏览器主动推送数据的时候,请考虑使用该项技术,而不是考虑具有双向通讯功能的websocket;
  • 以前我们用ajax轮询server也能实现,服务器负担大;
  • sse原理是向server请求一次后,server会挂住请求不放(此时浏览器里请求状态是pending),等有数据后才返回给浏览器,然后再发起下一次请求,以此类推;
  • 所有主流浏览器均支持服务器发送事件,除了 Internet Explorer(6,7,8,9);

5.2 示例

  • 服务器推送控制器
package com.wisely.web;

import java.util.Random;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SSEController {
    //注意produces="text/event-stream"
    @RequestMapping(value="/push",produces="text/event-stream")
    public @ResponseBody String push(){
         Random r = new Random();
         try {
                 Thread.sleep(5000);
         } catch (InterruptedException e) {
                 e.printStackTrace();
         }
         return "data:Testing 1,2,3" + r.nextInt() +"\n\n";
    }
}
  • 页面代码
<div id="msg_from_server"></div>
<script type="text/javascript" src="<c:url value="/js/jquery.js" />"></script>
<script type="text/javascript">
if (!!window.EventSource) {
       var source = new EventSource('push'); //为http://localhost:8080/testSpringMVC/push
       s='';
       source.addEventListener('message', function(e) {

           s+=e.data+"<br/>"
           $("#msg_from_server").html(s);

       });

       source.addEventListener('open', function(e) {
            console.log("连接打开.");
       }, false);

       source.addEventListener('error', function(e) {
            if (e.readyState == EventSource.CLOSED) {
               console.log("连接关闭");
            } else {
                console.log(e.readyState);    
            }
       }, false);
    } else {
            console.log("没有sse");
    }
</script>
  • 效果

 

 

http://wiselyman.iteye.com/blog/2214626

分享到:
评论

相关推荐

    Practical JSF in Java EE 8 pdf

    Practical JSF in Java EE 8 pdf Master the Java EE 8 and JSF (JavaServer Faces) APIs and web framework with this practical, projects-driven guide to web development. This book combines theoretical ...

    Java EE 8 Microservices.epub

    java ee 8实现的微服务,涵盖范围广泛,所有java ee技术条线的微服务技术都有介绍,完整的样例。

    Java EE 8 Cookbook epub

    Java EE 8 Cookbook 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    JAVA EE 8 RECIPES

    是一本关于JAVA EE经典开发的书,分为:JPA; JSF; FACELET;EJB;CDI;JPA;JTA;互操作:jax-rs;jax-ws

    源代码+书Java EE 8 High Performance

    By the end of the book, you will have gained insights into various techniques and solutions that will help create high-performance applications in the Java EE 8 environment. What you will ...

    Java(TM) EE 8 Specification APIs(Java(TM) EE 8规范API)

    Java(TM) EE 8 Specification APIs,即Java(TM) EE 8规范API。本CHM文档是根据由javadoc(1.8.0_144)于2017年9月生成的HTML文档制作而成的,原版英文文档。

    java_ee_sdk-8u1.zip

    Java EE 8 continues to improve API and programming models needed for today's applications and adds features requested by our world-wide community. This release modernizes support for many industry ...

    [Java EE] Java EE 7 开发者手册 (英文版)

    Understand the architecture of the Java EE platform and then apply the new Java EE 7 enhancements to benefit your own business-critical applications Learn about integration test development on Java EE...

    Java EE 8 Recipes, 2nd Edition--2018

    Develop Java Enterprise applications using the latest in Java EE technologies Build great-looking user interfaces using Java Server Faces Employ Java Servlet technology and standard frameworks in ...

    Pro CDI 2 in Java EE 8.pdf

    In Pro CDI 2 in Java EE 8, use CDI and the CDI 2.0 to automatically manage the life cycle of your enterprise Java, Java EE, or Jakarta EE application’s beans using predefined scopes and define custom...

    Pro CDI 2 in Java EE 8

    Pro CDI 2 in Java EE 8

    Java+EE+8+Application+Development-Packt+Publishing(2017).epub

    Java Enterprise Edition 8, the latest version of the Java EE specification, adds several new features to the specification. Several existing Java EE APIs have seen major improvements in this version ...

    [Java EE] Java EE 6 高级主题 权威指南 第4版 (英文版)

    The Java EE 6 Tutorial: Advanced Topics, Fourth Edition, is a task-oriented, example-driven guide to developing enterprise applications for the Java Platform, Enterprise Edition 6 (Java EE 6)....

    Java EE 8 and Angular epub

    Java EE 8 and Angular 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    java ee基础使用教程

    java ee基础使用教程郑阿奇里面含有书中的所有ppt和代码

    java ee api

    java ee

    Building RESTful Web Services with Java EE 8

    Learn the fundamentals of Java EE 8 APIs to build effective web services Java Enterprise Edition is one of the leading application programming platforms for enterprise Java development. With Java EE 8...

    精通Java EE项目案例-基于Eclipse Spring Struts Hibernate光盘源码.rar

    精通Java EE项目案例-基于Eclipse Spring Struts Hibernate光盘源码.rar精通Java EE项目案例-基于Eclipse Spring Struts Hibernate光盘源码.rar精通Java EE项目案例-基于Eclipse Spring Struts Hibernate光盘源码....

    Practical JSF in Java EE 8

    In a sense, this book teaches you to develop Java EE applications with JSF as user interface. It’s a book for Java enthusiasts. Knowledge about web technologies is helpful, but not required. For ...

    基于 java EE .Java权限后台管理系统.pdf

    基于 java EE .Java权限后台管理系统.pdf基于 java EE .Java权限后台管理系统.pdf基于 java EE .Java权限后台管理系统.pdf基于 java EE .Java权限后台管理系统.pdf基于 java EE .Java权限后台管理系统.pdf基于 java ...

Global site tag (gtag.js) - Google Analytics