8
8
9
9
import javax .ws .rs .client .Client ;
10
10
import javax .ws .rs .client .ClientBuilder ;
11
+ import javax .ws .rs .client .Entity ;
11
12
import javax .ws .rs .client .WebTarget ;
13
+ import javax .ws .rs .core .MediaType ;
14
+ import javax .ws .rs .sse .SseEventSource ;
15
+ import java .util .concurrent .Executors ;
16
+ import java .util .concurrent .ScheduledExecutorService ;
12
17
import java .util .concurrent .TimeUnit ;
18
+ import java .util .logging .Level ;
13
19
import java .util .logging .Logger ;
14
20
15
21
@ FixMethodOrder (MethodSorters .NAME_ASCENDING )
@@ -20,22 +26,37 @@ public class SseClientIntegrationTest {
20
26
private Client client ;
21
27
private WebTarget webTarget ;
22
28
29
+ private ScheduledExecutorService executorService ;
30
+
23
31
@ Before
24
32
public void setUp () {
25
33
client = ClientBuilder .newBuilder ()
26
34
.connectTimeout (5 , TimeUnit .SECONDS ).readTimeout (30 , TimeUnit .SECONDS )
27
35
.build ();
28
36
29
- webTarget = client .target ("http://localhost:8080" ).path ("/sse-service/api" );
37
+ webTarget = client .target ("http://localhost:8080" ).path ("/sse-service/api/events" );
38
+ executorService = Executors .newSingleThreadScheduledExecutor ();
30
39
}
31
40
32
41
@ After
33
42
public void tearDown () {
34
43
client .close ();
44
+ executorService .shutdown ();
35
45
}
36
46
37
47
@ Test
38
48
public void receiveSse () throws Exception {
39
- // TODO implement me
49
+
50
+ executorService .scheduleWithFixedDelay (() -> {
51
+ webTarget .request ().post (Entity .entity ("Hello SSE JAX-RS client." , MediaType .TEXT_PLAIN_TYPE ));
52
+ }, 250 , 500 , TimeUnit .MILLISECONDS );
53
+
54
+ try (SseEventSource eventSource = SseEventSource .target (webTarget ).build ()) {
55
+ eventSource .register ((e ) -> LOGGER .log (Level .INFO , "Recieved event {0} with data {1}." ,
56
+ new Object []{e .getName (), e .readData ()}));
57
+ eventSource .open ();
58
+
59
+ TimeUnit .SECONDS .sleep (3 );
60
+ }
40
61
}
41
62
}
0 commit comments