JAX-RS Simple SSE Sample

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;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A complete and practical guide to building RESTful Web Services with the latest Java EE7 API Overview Learning about different client/server communication models including but not limited to client polling, Server-Sent Events and WebSockets. Efficiently use WebSockets, Server-Sent Events, and JSON in Java EE applications Learn about JAX-RS 2.0 new features and enhancements Improve scalability with asynchronous processing In Detail As the technology landscape moves focus towards smaller devices, the need for building scalable, flexible, lightweight, and real-time communications-based applications grows. HTML 5 and Java EE 7 provide a new synthesis of technologies that demonstrate tremendous scope and potential in areas of device independence, asynchronous communication, interoperability, and portability. Developing RESTful Services with JAX-RS 2.0, WebSockets, and JSON is a practical, hands-on guide that provides you with clear and pragmatic information to take advantage of the real power behind HTML5 and Java EE technologies. This book also gives you a good foundation for using them in your applications. Developing RESTful Services with JAX-RS 2.0, WebSockets, and JSON looks at the different HTML5-based Java EE 7 API, and takes a deep dive into the individual areas of technologies to cover basic to advanced concepts, and also provides best practices for each API. You will also learn how to build a REST-based Event Notification Application using the Twitter API, tying all the different technologies together that we will cover. You will also take a look at integrating different Java EE APIs to build a Library Application. If you want to take advantage of using the new HTML5 technologies and Java EE 7 platform, then this is the book for you. You will learn everything you need to know to build portable RESTful Web Services with JAX-RS 2.0, Web Sockets, JSON, and Server-Sent Events. What you will learn from this book Develop RESTful Web Services using the JAX-RS 2.0 API Build applications with the JavaScript and Java Client API for WebSockets and ServerSentEvents Understand Security and Fault tolerance with WebSockets and ServerSentEvents Produce, parse, and manipulate JSON data with the help of JSON-P API Cover best practices in building applications with WebSockets, ServerSentEvents, and JAX-RS 2.0 Learn the aspects of asynchronous programming to improve scalability Approach Written as an easy and practical guide, this book is a crash course on using JAX-RS 2.0, JSON, and WebSockets to develop RESTful services. Who this book is written for Developing RESTful Services with JAX-RS 2.0, WebSockets, and JSON is a perfect reading source for application developers who are familiar with Java EE and are keen to understand the new HTML5-related functionality introduced in Java EE 7 to improve productivity. To take full advantage of this book, you need to be familiar with Java EE and have some basic understanding of using the GlassFish application server. Table of Contents Chapter 1: Building RESTful Web Services Using JAX-RS Chapter 2: WebSockets and Server-sent Events Chapter 3: Understanding WebSockets and Server-sent Events in Detail Chapter 4: JSON and Asynchronous Processing Chapter 5: RESTful Web Services by Example Book Details Title: Developing RESTful Services with JAX-RS 2.0, WebSockets, and JSON Author: Bhakti Mehta, Masoud Kalali Length: 128 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2013-10-15 ISBN-10: 1782178120 ISBN-13: 9781782178125
您可以按照以下步骤将JAX-WS接口替换为JAX-RS接口: 1. 创建JAX-RS接口:创建一个新的Java接口来定义您的JAX-RS服务。在接口上使用`@Path`注解指定资源的URL路径。 ```java import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/your-resource") public interface YourResource { @GET @Produces(MediaType.APPLICATION_JSON) String getResource(); } ``` 2. 实现JAX-RS接口:创建一个类来实现您的JAX-RS接口,并实现接口中定义的方法。 ```java public class YourResourceImpl implements YourResource { @Override public String getResource() { // 实现您的业务逻辑 return "Hello JAX-RS!"; } } ``` 3. 注册JAX-RS服务:将您的JAX-RS服务注册到应用程序中。这可以通过创建一个`javax.ws.rs.core.Application`子类并在其中注册资源类来完成。 ```java import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; import java.util.HashSet; import java.util.Set; @ApplicationPath("/api") public class YourApplication extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> classes = new HashSet<>(); classes.add(YourResourceImpl.class); return classes; } } ``` 4. 配置JAX-RS:根据您使用的应用程序服务器,将JAX-RS的实现(如Jersey或RestEasy)添加到您的应用程序的构建配置文件中。您还需要确保在应用程序服务器上正确配置JAX-RS。 5. 测试JAX-RS接口:启动您的应用程序服务器,并使用JAX-RS客户端或浏览器等工具测试您的JAX-RS接口。 请注意,以上步骤是一般的指导,具体步骤可能因您使用的框架和工具而有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值