Search in sources :

Example 1 with OutboundSseEvent

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));
    }
}
Also used : OutboundSseEvent(javax.ws.rs.sse.OutboundSseEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Flow(javax.ws.rs.Flow) Test(org.junit.Test)

Example 2 with OutboundSseEvent

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));
    }
}
Also used : OutboundSseEvent(javax.ws.rs.sse.OutboundSseEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Flow(javax.ws.rs.Flow) Test(org.junit.Test)

Example 3 with OutboundSseEvent

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));
    }
}
Also used : OutboundSseEvent(javax.ws.rs.sse.OutboundSseEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Flow(javax.ws.rs.Flow) Test(org.junit.Test)

Example 4 with OutboundSseEvent

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));
    }
}
Also used : OutboundSseEvent(javax.ws.rs.sse.OutboundSseEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Flow(javax.ws.rs.Flow) Test(org.junit.Test)

Example 5 with OutboundSseEvent

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) OutboundSseEvent(javax.ws.rs.sse.OutboundSseEvent) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) OutboundSseEventBodyWriter(org.apache.cxf.jaxrs.sse.OutboundSseEventBodyWriter) Broadcaster(org.atmosphere.cpr.Broadcaster)

Aggregations

OutboundSseEvent (javax.ws.rs.sse.OutboundSseEvent)8 Test (org.junit.Test)7 CountDownLatch (java.util.concurrent.CountDownLatch)4 Flow (javax.ws.rs.Flow)4 Sse (javax.ws.rs.sse.Sse)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 OutboundSseEventBodyWriter (org.apache.cxf.jaxrs.sse.OutboundSseEventBodyWriter)1 AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)1 Broadcaster (org.atmosphere.cpr.Broadcaster)1