use of javax.ws.rs.sse.OutboundSseEvent in project jersey by jersey.
the class JerseySseBroadcasterTest method testOnErrorFromOnSubscribe.
@Test
public void testOnErrorFromOnSubscribe() throws InterruptedException {
try (JerseySseBroadcaster broadcaster = new JerseySseBroadcaster()) {
final CountDownLatch latch = new CountDownLatch(1);
broadcaster.onError((subscriber, throwable) -> {
if (TEST_EXCEPTION_MSG.equals(throwable.getMessage())) {
latch.countDown();
}
});
broadcaster.subscribe(new Flow.Subscriber<OutboundSseEvent>() {
@Override
public void onSubscribe(Flow.Subscription subscription) {
throw new RuntimeException(TEST_EXCEPTION_MSG);
}
@Override
public void onNext(OutboundSseEvent item) {
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onComplete() {
}
});
Assert.assertTrue(latch.await(2000, TimeUnit.MILLISECONDS));
}
}
use of javax.ws.rs.sse.OutboundSseEvent in project jersey by jersey.
the class JerseySseBroadcasterTest method testOnClose.
@Test
public void testOnClose() throws InterruptedException {
try (JerseySseBroadcaster broadcaster = new JerseySseBroadcaster()) {
final CountDownLatch latch = new CountDownLatch(1);
final Flow.Subscriber<OutboundSseEvent> subscriber = new Flow.Subscriber<OutboundSseEvent>() {
@Override
public void onSubscribe(Flow.Subscription subscription) {
subscription.request(Long.MAX_VALUE);
}
@Override
public void onNext(OutboundSseEvent item) {
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onComplete() {
}
};
broadcaster.subscribe(subscriber);
broadcaster.onClose((s) -> {
if (s.equals(subscriber)) {
latch.countDown();
}
});
broadcaster.close();
Assert.assertTrue(latch.await(2000, TimeUnit.MILLISECONDS));
}
}
use of javax.ws.rs.sse.OutboundSseEvent in project jersey by jersey.
the class JerseySseBroadcasterTest method testOnErrorFromOnNext.
@Test
public void testOnErrorFromOnNext() throws InterruptedException {
try (JerseySseBroadcaster broadcaster = new JerseySseBroadcaster()) {
final CountDownLatch latch = new CountDownLatch(1);
broadcaster.onError((subscriber, throwable) -> {
if (TEST_EXCEPTION_MSG.equals(throwable.getMessage())) {
latch.countDown();
}
});
broadcaster.subscribe(new Flow.Subscriber<OutboundSseEvent>() {
@Override
public void onSubscribe(Flow.Subscription subscription) {
subscription.request(Long.MAX_VALUE);
}
@Override
public void onNext(OutboundSseEvent item) {
throw new RuntimeException(TEST_EXCEPTION_MSG);
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onComplete() {
}
});
broadcaster.broadcast(JerseySse.getInstance().newEvent("ping"));
Assert.assertTrue(latch.await(2000, TimeUnit.MILLISECONDS));
}
}
use of javax.ws.rs.sse.OutboundSseEvent in project jersey by jersey.
the class JerseySseBroadcasterTest method testOnErrorFromOnSubscribeTwoListeners.
@Test
public void testOnErrorFromOnSubscribeTwoListeners() throws InterruptedException {
try (JerseySseBroadcaster broadcaster = new JerseySseBroadcaster()) {
final CountDownLatch latch = new CountDownLatch(2);
broadcaster.onError((subscriber, throwable) -> {
if (TEST_EXCEPTION_MSG.equals(throwable.getMessage())) {
latch.countDown();
}
});
broadcaster.onError((subscriber, throwable) -> {
if (TEST_EXCEPTION_MSG.equals(throwable.getMessage())) {
latch.countDown();
}
});
broadcaster.subscribe(new Flow.Subscriber<OutboundSseEvent>() {
@Override
public void onSubscribe(Flow.Subscription subscription) {
throw new RuntimeException(TEST_EXCEPTION_MSG);
}
@Override
public void onNext(OutboundSseEvent item) {
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onComplete() {
}
});
Assert.assertTrue(latch.await(2000, TimeUnit.MILLISECONDS));
}
}
use of javax.ws.rs.sse.OutboundSseEvent in project cxf by apache.
the class SseAtmosphereEventSinkContextProvider method createContext.
@Override
public SseEventSink createContext(Message message) {
final HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
if (request == null) {
throw new IllegalStateException("Unable to retrieve HTTP request from the context");
}
final AtmosphereResource resource = (AtmosphereResource) request.getAttribute(AtmosphereResource.class.getName());
if (resource == null) {
throw new IllegalStateException("AtmosphereResource is not present, " + "is AtmosphereServlet configured properly?");
}
final Broadcaster broadcaster = resource.getAtmosphereConfig().getBroadcasterFactory().lookup(resource.uuid(), true);
resource.removeFromAllBroadcasters();
resource.setBroadcaster(broadcaster);
final MessageBodyWriter<OutboundSseEvent> writer = new OutboundSseEventBodyWriter(ServerProviderFactory.getInstance(message), message.getExchange());
return new SseAtmosphereEventSinkImpl(writer, resource);
}
Aggregations