Skip to content

Commit 79ca4a4

Browse files
PAYARA-1939: SSE sample, serving from JAXRS-REST API to HTML5 JS listener
1 parent 3064e70 commit 79ca4a4

10 files changed

Lines changed: 5370 additions & 0 deletions

File tree

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<modules>
9494
<module>test-utils</module>
9595
<module>servlet</module>
96+
<module>sse</module>
9697
<module>security</module>
9798
<module>jsf</module>
9899
<module>cdi</module>

sse/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Java EE 8 Samples: Server Sent Events 1.0#
2+
3+
The [JSR 370](https://www.jcp.org/en/jsr/detail?id=370) specifies the JavaTM API for RESTful Web Services - Server-Sent Events (SSE).
4+
5+
## Samples ##
6+
7+
- producer
8+
- consumer

sse/log.log

Lines changed: 5084 additions & 0 deletions
Large diffs are not rendered by default.

sse/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>org.javaee8</groupId>
6+
<artifactId>samples-parent</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>sse</artifactId>
11+
<packaging>pom</packaging>
12+
13+
<name>Java EE 8 Samples: Server Sent Events</name>
14+
15+
<modules>
16+
<module>producer</module>
17+
</modules>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.javaee8</groupId>
22+
<artifactId>test-utils</artifactId>
23+
<version>${project.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.glassfish.jersey.media</groupId>
27+
<artifactId>jersey-media-sse</artifactId>
28+
<version>2.26</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>javax.ws.rs</groupId>
32+
<artifactId>javax.ws.rs-api</artifactId>
33+
<version>2.1</version>
34+
</dependency>
35+
</dependencies>
36+
</project>

sse/producer/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>org.javaee8</groupId>
6+
<artifactId>sse</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>sse-producer</artifactId>
11+
<packaging>war</packaging>
12+
<name>Java EE 8 Samples: Server Sent Events [Producer]</name>
13+
<dependencies>
14+
<dependency>
15+
<groupId>javax.ejb</groupId>
16+
<artifactId>javax.ejb-api</artifactId>
17+
<version>3.2</version>
18+
<scope>test</scope>
19+
<type>jar</type>
20+
</dependency>
21+
</dependencies>
22+
</project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.javaee8.sse.producer;
2+
3+
import javax.annotation.PostConstruct;
4+
import javax.inject.Singleton;
5+
import javax.ws.rs.GET;
6+
import javax.ws.rs.Path;
7+
import javax.ws.rs.Produces;
8+
import javax.ws.rs.core.Context;
9+
import javax.ws.rs.core.MediaType;
10+
import javax.ws.rs.sse.Sse;
11+
import javax.ws.rs.sse.SseBroadcaster;
12+
import javax.ws.rs.sse.SseEventSink;
13+
14+
/**
15+
* @author Daniel Contreras
16+
*/
17+
@Path("/")
18+
@Singleton
19+
public class SseResource {
20+
21+
@Context
22+
private Sse sse;
23+
24+
private volatile SseBroadcaster sseBroadcaster;
25+
26+
@PostConstruct
27+
public void init() {
28+
System.out.println("1");
29+
this.sseBroadcaster = sse.newBroadcaster();
30+
System.out.println("2");
31+
}
32+
33+
@GET
34+
@Path("register")
35+
@Produces(MediaType.SERVER_SENT_EVENTS)
36+
public void register(@Context SseEventSink eventSink) {
37+
System.out.println("3");
38+
eventSink.send(sse.newEvent("welcome!"));
39+
System.out.println("4");
40+
sseBroadcaster.register(eventSink);
41+
System.out.println("5");
42+
eventSink.send(sse.newEvent("event1"));
43+
eventSink.send(sse.newEvent("event2"));
44+
eventSink.send(sse.newEvent("event3"));
45+
46+
for (int i = 0; i < 20; i++) {
47+
48+
sseBroadcaster.broadcast(sse.newEvent("Repeated" + i));
49+
50+
try {
51+
Thread.sleep(1000);
52+
} catch (InterruptedException e) {
53+
e.printStackTrace();
54+
}
55+
}
56+
}
57+
58+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package org.javaee8.sse.rest;
7+
8+
import javax.ws.rs.ApplicationPath;
9+
import javax.ws.rs.core.Application;
10+
11+
/**
12+
*
13+
* @author daniel
14+
*/
15+
@ApplicationPath(value = "rest")
16+
public class RestApplication extends Application{
17+
18+
19+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
3+
<glassfish-web-app error-url="">
4+
<context-root>/</context-root>
5+
</glassfish-web-app>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<!--
3+
To change this license header, choose License Headers in Project Properties.
4+
To change this template file, choose Tools | Templates
5+
and open the template in the editor.
6+
-->
7+
<html>
8+
<head>
9+
<title>SSE</title>
10+
<meta charset="UTF-8">
11+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
12+
</head>
13+
<body>
14+
15+
<span id="foo"></span>
16+
17+
<br><br>
18+
<button onclick="start()">Start</button>
19+
20+
<script>
21+
22+
function start() {
23+
24+
var eventSource = new EventSource("rest/register");
25+
console.log(eventSource);
26+
27+
eventSource.onmessage = function (event) {
28+
console.log(event)
29+
var el = document.getElementById("foo");
30+
el.innerHTML += event.data + "<br/>";
31+
el.scrollTop += 50;
32+
};
33+
34+
eventSource.addEventListener('broadcast', function (event) {
35+
36+
console.log(event)
37+
var el = document.getElementById("foo");
38+
el.innerHTML += event.data + "lt;br/&>";
39+
el.scrollTop += 50;
40+
41+
}, false);
42+
}
43+
</script>
44+
</body>
45+
</html>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.javaee8.sse.producer;
2+
3+
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
4+
5+
import java.io.IOException;
6+
import java.net.URL;
7+
8+
import org.jboss.arquillian.container.test.api.Deployment;
9+
import org.jboss.arquillian.container.test.api.RunAsClient;
10+
import org.jboss.arquillian.junit.Arquillian;
11+
import org.jboss.arquillian.test.api.ArquillianResource;
12+
import org.jboss.shrinkwrap.api.spec.WebArchive;
13+
import org.junit.After;
14+
import org.junit.Before;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
18+
import javax.ws.rs.client.Client;
19+
import javax.ws.rs.client.ClientBuilder;
20+
import javax.ws.rs.client.WebTarget;
21+
import javax.ws.rs.core.Context;
22+
import javax.ws.rs.sse.SseEventSource;
23+
24+
/**
25+
* @author Daniel Contreras
26+
*/
27+
//@RunWith(Arquillian.class)
28+
public class SseResourceTest {
29+
/*
30+
@ArquillianResource
31+
private URL base;
32+
33+
private Client sseClient;
34+
WebTarget target;
35+
36+
@Deployment
37+
public static WebArchive createDeployment() {
38+
return create(WebArchive.class)
39+
.addClass(SseResource.class);
40+
}
41+
42+
@Before
43+
public void setup() {
44+
System.out.println("01");
45+
this.sseClient = ClientBuilder.newClient();
46+
System.out.println("02");
47+
}
48+
49+
@After
50+
public void teardown() {
51+
52+
}
53+
54+
@Test
55+
@RunAsClient
56+
public void testClient() throws IOException {
57+
System.out.println("Client");
58+
System.out.println(base + "producer/register");
59+
System.out.println(this.sseClient.getConfiguration().toString());
60+
}
61+
62+
@Test
63+
public void testServer() throws IOException {
64+
System.out.println("Server");
65+
System.out.println(base + "producer/register");
66+
//this.sseClient.target(base + "producer/register");
67+
System.out.println(this.sseClient.getConfiguration().getProperties().toString());
68+
System.out.println(this.sseClient.getConfiguration().getRuntimeType().toString());
69+
System.out.println(this.sseClient.getConfiguration().getInstances().toArray().toString());
70+
//this.sseClient.getHostnameVerifier();
71+
72+
System.out.println(base + "producer/register");
73+
WebTarget target = sseClient.target(base + "producer/register");
74+
System.out.println("1");
75+
try (SseEventSource source = SseEventSource.target(target).build()) {
76+
System.out.println("2");
77+
source.register(System.out::println);
78+
System.out.println("3");
79+
source.open();
80+
System.out.println("5");
81+
Thread.sleep(500); // Consume events for just 500 ms
82+
83+
} catch (Exception e) {
84+
85+
System.out.println("error!!!");
86+
System.out.println(e.getMessage());
87+
88+
}
89+
90+
}
91+
*/
92+
}

0 commit comments

Comments
 (0)