Skip to content

Commit 9fdf75c

Browse files
committed
Merge remote-tracking branch 'upstream/master' into format
2 parents 1eed5f9 + 3e4dcc4 commit 9fdf75c

File tree

3 files changed

+51
-25
lines changed

3 files changed

+51
-25
lines changed
Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package io.avaje.jex.http.sse;
22

3+
import java.io.Closeable;
4+
import java.util.function.Consumer;
5+
36
import io.avaje.jex.http.Context;
47
import io.avaje.jex.http.ExchangeHandler;
58
import io.avaje.jex.spi.JsonService;
6-
import java.io.Closeable;
7-
import java.util.function.Consumer;
89

910
/**
10-
* A client for Server-Sent Events (SSE). This class handles the setup of the SSE connection, sending events and
11-
* comments to the client, and managing the lifecycle of the connection. It ensures proper headers are set and provides
12-
* methods for sending various types of data.
11+
* A client for Server-Sent Events (SSE). This class handles the setup of the SSE connection,
12+
* sending events and comments to the client, and managing the lifecycle of the connection. It
13+
* ensures proper headers are set and provides methods for sending various types of data.
1314
*
14-
* <p>This class implements {@link Closeable} to allow for proper resource management. The connection is automatically
15-
* closed if the client disconnects or if an error occurs during event emission.
15+
* <p>This class implements {@link Closeable} to allow for proper resource management. The
16+
* connection is automatically closed if the client disconnects or if an error occurs during event
17+
* emission.
1618
*/
1719
public interface SseClient extends Closeable {
1820

@@ -33,14 +35,23 @@ static ExchangeHandler handler(Consumer<SseClient> consumer) {
3335
Context ctx();
3436

3537
/**
36-
* By blocking the SSE connection, you can share this client outside the handler to notify it from other sources.
37-
* Keep in mind that this function will block the handler until the SSE client is released by another thread.
38+
* By blocking the SSE connection, you can share this client outside the handler to notify it from
39+
* other sources. Keep in mind that this function will block the handler until the SSE client is
40+
* released by another thread.
3841
*/
3942
void keepAlive();
4043

4144
/**
42-
* Attempt to send a comment. If the {@link Emitter} fails to emit (remote client has disconnected), the
43-
* {@link #close()} function will be called instead.
45+
* Add a callback that will be called either when connection is closed through {@link #close()},
46+
* or when the {@link Emitter} is detected as closed.
47+
*
48+
* @param task task to run
49+
*/
50+
void onClose(Runnable task);
51+
52+
/**
53+
* Attempt to send a comment. If the {@link Emitter} fails to emit (remote client has
54+
* disconnected), the {@link #close()} function will be called instead.
4455
*/
4556
void sendComment(String comment);
4657

@@ -51,19 +62,19 @@ static ExchangeHandler handler(Consumer<SseClient> consumer) {
5162
void sendEvent(String event, Object data);
5263

5364
/**
54-
* Attempt to send an event. If the {@link Emitter} fails to emit (remote client has disconnected), the
55-
* {@link #close()} function will be called instead.
65+
* Attempt to send an event. If the {@link Emitter} fails to emit (remote client has
66+
* disconnected), the {@link #close()} function will be called instead.
5667
*
5768
* @param event The name of the event.
58-
* @param data The data to send in the event. This can be a String, an InputStream, or any object that can be
59-
* serialized to JSON using the configured {@link JsonService}.
69+
* @param data The data to send in the event. This can be a String, an InputStream, or any object
70+
* that can be serialized to JSON using the configured {@link JsonService}.
6071
* @param id The ID of the event.
6172
*/
6273
void sendEvent(String event, Object data, String id);
6374

6475
/**
65-
* Returns true if {@link #close()} has been called. This can either be by the user, or by Jex upon detecting that
66-
* the {@link Emitter} is closed.
76+
* Returns true if {@link #close()} has been called. This can either be by the user, or by Jex
77+
* upon detecting that the {@link Emitter} is closed.
6778
*/
6879
boolean terminated();
6980
}

avaje-jex/src/main/java/io/avaje/jex/http/sse/SseClientImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,24 @@ final class SseClientImpl implements SseClient {
1919
private final JsonService jsonService;
2020
private final Context ctx;
2121
private CompletableFuture<?> blockingFuture;
22+
private Runnable closeCallback = () -> {};
2223

2324
SseClientImpl(Context ctx) {
2425
this.emitter = new Emitter(ctx.exchange().getResponseBody());
2526
jsonService = ctx.jsonService();
2627
this.ctx = ctx;
2728
}
2829

30+
@Override
31+
public void onClose(Runnable task) {
32+
this.closeCallback = task;
33+
}
34+
2935
@Override
3036
public void close() {
31-
if (terminated.getAndSet(true) && blockingFuture != null) {
37+
if (terminated.getAndSet(true)) return;
38+
closeCallback.run();
39+
if (blockingFuture != null) {
3240
blockingFuture.complete(null);
3341
}
3442
}

avaje-jex/src/main/java/io/avaje/jex/spi/JsonService.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public non-sealed interface JsonService extends JexExtension {
1515
/**
1616
* **Writes a Java Object as JSON to an OutputStream**
1717
*
18-
* <p>Serializes a Java object into JSON format and writes the resulting JSON to the specified output stream.
18+
* <p>Serializes a Java object into JSON format and writes the resulting JSON to the specified
19+
* output stream.
1920
*
2021
* @param bean the Java object to be serialized
2122
* @param os the output stream to write the JSON data to
@@ -25,7 +26,8 @@ public non-sealed interface JsonService extends JexExtension {
2526
/**
2627
* **Writes a Java Object as a JSON string**
2728
*
28-
* <p>Serializes a Java object into JSON string format and writes the resulting JSON to the specified output stream.
29+
* <p>Serializes a Java object into JSON string format and writes the resulting JSON to the
30+
* specified output stream.
2931
*
3032
* @param bean the Java object to be serialized
3133
* @return the serialized JSON string
@@ -35,7 +37,8 @@ public non-sealed interface JsonService extends JexExtension {
3537
/**
3638
* **Reads JSON from an InputStream**
3739
*
38-
* <p>Reads a JSON-formatted input stream and deserializes it into a Java object of the specified type.
40+
* <p>Reads a JSON-formatted input stream and deserializes it into a Java object of the specified
41+
* type.
3942
*
4043
* @param type the Class object of the desired type
4144
* @param is the input stream containing the JSON data
@@ -46,7 +49,8 @@ public non-sealed interface JsonService extends JexExtension {
4649
/**
4750
* **Reads JSON from an InputStream**
4851
*
49-
* <p>Reads a JSON-formatted input stream and deserializes it into a Java object of the specified type.
52+
* <p>Reads a JSON-formatted input stream and deserializes it into a Java object of the specified
53+
* type.
5054
*
5155
* @param type the Type object of the desired type
5256
* @param is the input stream containing the JSON data
@@ -55,11 +59,14 @@ public non-sealed interface JsonService extends JexExtension {
5559
<T> T fromJson(Type type, InputStream is);
5660

5761
/**
58-
* Serializes a stream of Java objects into a JSON-Stream format, using the {@code x-json-stream} media type. Each
59-
* object in the stream is serialized as a separate JSON object, and the objects are separated by newlines.
62+
* Serializes a stream of Java objects into a JSON-Stream format, using the {@code x-json-stream}
63+
* media type. Each object in the stream is serialized as a separate JSON object, and the objects
64+
* are separated by newlines.
6065
*
6166
* @param iterator the stream of objects to be serialized
6267
* @param os the output stream to write the JSON-Stream data to
6368
*/
64-
<E> void toJsonStream(Iterator<E> iterator, OutputStream os);
69+
default <E> void toJsonStream(Iterator<E> iterator, OutputStream os) {
70+
throw new UnsupportedOperationException("toJsonStream is unimplemented in this JsonService");
71+
}
6572
}

0 commit comments

Comments
 (0)