use of javax.ws.rs.sse.InboundSseEvent in project cxf by apache.
the class SsePublisher method start.
private void start() {
if (isStarted.compareAndSet(false, true)) {
executor.execute(() -> {
try (BufferedReader br2 = br) {
SseEventBuilder builder = new SseEventBuilder(providers);
String line = br.readLine();
while (line != null && !subscriptions.isEmpty()) {
if (line.startsWith("data:")) {
builder.data(removeSpace(line.substring(5)));
} else if (line.startsWith("id:")) {
builder.id(removeSpace(line.substring(3)));
} else if (line.startsWith("event:")) {
builder.name(removeSpace(line.substring(6)));
} else if (line.startsWith(":")) {
builder.comment(removeSpace(line.substring(1)));
} else if ("".equals(line)) {
InboundSseEvent event = builder.build();
for (SseSubscription subscription : subscriptions) {
subscription.fireEvent(event);
}
builder = new SseEventBuilder(providers);
}
line = br.readLine();
}
for (SseSubscription subscription : subscriptions) {
subscription.complete();
}
} catch (IOException ex) {
for (SseSubscription subscription : subscriptions) {
subscription.fireError(ex);
}
}
});
}
}
use of javax.ws.rs.sse.InboundSseEvent in project cxf by apache.
the class InboundSseEventProcessor method process.
private Callable<?> process(Response response, InputStream is, ClientProviderFactory factory, Message message) {
return () -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
String line = reader.readLine();
InboundSseEventImpl.Builder builder = null;
while (line != null && !Thread.interrupted() && !closed) {
if (StringUtils.isEmpty(line) && builder != null) {
/* empty new line */
final InboundSseEvent event = builder.build(factory, message);
builder = null;
/* reset the builder for next event */
listener.onNext(event);
} else {
// https://www.w3.org/TR/eventsource/#parsing-an-event-stream
if (line.startsWith(EVENT)) {
int beginIndex = findFirstNonSpacePosition(line, EVENT);
builder = getOrCreate(builder).name(line.substring(beginIndex));
} else if (line.startsWith(ID)) {
int beginIndex = findFirstNonSpacePosition(line, ID);
builder = getOrCreate(builder).id(line.substring(beginIndex));
} else if (line.startsWith(COMMENT)) {
int beginIndex = findFirstNonSpacePosition(line, COMMENT);
builder = getOrCreate(builder).comment(line.substring(beginIndex));
} else if (line.startsWith(RETRY)) {
int beginIndex = findFirstNonSpacePosition(line, RETRY);
builder = getOrCreate(builder).reconnectDelay(line.substring(beginIndex));
} else if (line.startsWith(DATA)) {
int beginIndex = findFirstNonSpacePosition(line, DATA);
builder = getOrCreate(builder).appendData(line.substring(beginIndex));
}
}
line = reader.readLine();
}
if (builder != null) {
//
if (discardIncomplete) /* default */
{
LOG.fine("Discarding incomplete SSE event");
} else {
listener.onNext(builder.build(factory, message));
}
}
// complete the stream
listener.onComplete();
} catch (final Exception ex) {
listener.onError(ex);
}
if (response != null) {
LOG.fine("Closing the response");
response.close();
}
return null;
};
}
use of javax.ws.rs.sse.InboundSseEvent in project jersey by jersey.
the class SseEventSinkToEventSourceTest method testWithSimpleSubscriber.
@Test
public void testWithSimpleSubscriber() {
transmitLatch = new CountDownLatch(MSG_COUNT);
final WebTarget endpoint = target().path("events");
final List<InboundSseEvent> results = new ArrayList<>();
try (final SseEventSource eventSource = SseEventSource.target(endpoint).build()) {
final CountDownLatch receivedLatch = new CountDownLatch(3 * MSG_COUNT);
eventSource.subscribe((event) -> {
results.add(event);
receivedLatch.countDown();
});
eventSource.open();
final boolean allTransmitted = transmitLatch.await(5000, TimeUnit.MILLISECONDS);
final boolean allReceived = receivedLatch.await(5000, TimeUnit.MILLISECONDS);
Assert.assertTrue(allTransmitted);
Assert.assertTrue(allReceived);
Assert.assertEquals(30, results.size());
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
use of javax.ws.rs.sse.InboundSseEvent in project cxf by apache.
the class SseSubscription method request.
@Override
public void request(long n) {
if (canceled.get()) {
return;
}
if (n < 1) {
fireError(new IllegalArgumentException("Only positive values may be requested - passed-in " + n));
return;
}
requested.addAndGet(n);
synchronized (buffer) {
synchronized (delivered) {
InboundSseEvent bufferedEvent;
while (delivered.get() < requested.get() && (bufferedEvent = buffer.pollFirst()) != null) {
subscriber.onNext(bufferedEvent);
delivered.incrementAndGet();
}
}
}
}
Aggregations