JAX-RS Simple SSE Sample
2014-09-20 19:45
239 查看
package com.oracle.gcs.pguo.examples.jaxrs.sse.simple; import java.io.IOException; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import org.glassfish.jersey.media.sse.EventOutput; import org.glassfish.jersey.media.sse.OutboundEvent; import org.glassfish.jersey.media.sse.SseFeature; @Path("sse") public class SimpleSseResource { @GET @Path("simpleEvents") @Produces(SseFeature.SERVER_SENT_EVENTS) public EventOutput getServerSentEvents() { final EventOutput eventOutput = new EventOutput(); new Thread(new Runnable() { @Override public void run() { try { // push message to client every 2 seconds // while(true) { for (int i = 0; i < 20; i++) { try { Thread.sleep(2 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } final OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder(); eventBuilder.name("message-to-client"); eventBuilder.data(String.class, "Hello world " + i + "!"); final OutboundEvent event = eventBuilder.build(); eventOutput.write(event); } } catch (IOException e) { throw new RuntimeException("Error when writing the event.", e); } finally { try { // no more events, closed connection eventOutput.close(); } catch (IOException ioClose) { throw new RuntimeException( "Error when closing the event output.", ioClose); } } } }).start(); return eventOutput; } }
相关文章推荐
- Java EE 7 / JAX-RS 2.0: Simple REST API Authentication & Authorization with Custom HTTP Header--reference
- JAVA的 WebService规范 JAX-RS
- JAX-RS入门 六: 数据处理(1)
- 【绝对干货来啦】巧用jax-rs之jersey实现不确定数量、多类型文件批量一次上传接口
- Simple sample for transforming XML to HTML by using XSLT
- JAX-RS入门 二 :运行
- 使用JAX-RS创建RESTful Web Service
- Restlet - 基于JAX-RS的Restlet开发实例
- 使用JAX-RS创建RESTful Web Service
- restful web service学习1-JAX-RS 2.0入门
- 使用 JAX-RS 简化 REST 应用开发
- JAX-RS入门 四: 注入
- Jersey (JAX-RS) implements a Cross domain filter
- JAX-RS入门 二 :运行
- JAX-RS简介
- JAX-RS之上传文件
- JAX-RS RESTful webservice 服务端及客户端实现(基于HTTPS双向认证)
- 最给力的java REST入门:使用 JAX-RS 简化 REST 应用开发(转自IBM)
- Spring MVC与JAX-RS比较与分析
- Exploring the SimpleConsumer and Default Camel CXFRS binding styles