use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class SseTest method testSse.
@Test
public void testSse() throws Exception {
final ResourceConfig resourceConfig = new ResourceConfig(SseResource.class, SseFeature.class);
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
Client c = ClientBuilder.newClient();
c.register(SseFeature.class);
final List<String> data = new LinkedList<String>();
final CountDownLatch latch = new CountDownLatch(2);
final EventSource eventSource = new EventSource(c.target(baseUri).path("/sse")) {
@Override
public void onEvent(InboundEvent event) {
try {
data.add(event.readData());
latch.countDown();
} catch (ProcessingException e) {
// ignore
}
}
};
assertTrue(latch.await(2, TimeUnit.SECONDS));
eventSource.close();
assertEquals(2, data.size());
server.shutdownNow();
}
use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class App method startServer.
/**
* Starts Grizzly HTTP server exposing static content, JAX-RS resources
* and web sockets defined in this application.
*
* @param webRootPath static content root path.
* @return Grizzly HTTP server.
*/
public static HttpServer startServer(String webRootPath) {
final HttpServer server = new HttpServer();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
server.shutdownNow();
}
}));
final NetworkListener listener = new NetworkListener("grizzly", "localhost", PORT);
server.addListener(listener);
final ServerConfiguration config = server.getServerConfiguration();
// add handler for serving static content
config.addHttpHandler(new StaticContentHandler(webRootPath), APP_PATH);
// add handler for serving JAX-RS resources
config.addHttpHandler(RuntimeDelegate.getInstance().createEndpoint(createResourceConfig(), GrizzlyHttpContainer.class), API_PATH);
try {
// Start the server.
server.start();
} catch (Exception ex) {
throw new ProcessingException("Exception thrown when trying to start grizzly server", ex);
}
return server;
}
use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class ServerSentEventsTest method testEventSource.
/**
* Test consuming a single SSE event via event source.
*
* @throws Exception in case of a failure during the test execution.
*/
@Test
public void testEventSource() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> message = new AtomicReference<String>();
final EventSource eventSource = new EventSource(target().path(App.ROOT_PATH)) {
@Override
public void onEvent(InboundEvent inboundEvent) {
try {
final String value = inboundEvent.readData();
message.set(value);
latch.countDown();
} catch (ProcessingException e) {
e.printStackTrace();
}
}
};
target().path(App.ROOT_PATH).request().post(Entity.text("message"));
try {
assertTrue("Waiting for message to be delivered has timed out.", latch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS));
} finally {
eventSource.close();
}
assertThat("Unexpected SSE event data value.", message.get(), equalTo("message"));
}
use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class EventSourceWithNamedEventsTest method testWithEventSource.
@Test
public void testWithEventSource() throws IOException, NoSuchAlgorithmException, InterruptedException {
final WebTarget endpoint = target().register(SseFeature.class).path("events");
EventSource eventSource = EventSource.target(endpoint).build();
final CountDownLatch count = new CountDownLatch(MSG_COUNT);
final EventListener listener = new EventListener() {
@Override
public void onEvent(InboundEvent inboundEvent) {
try {
final Integer data = inboundEvent.readData(Integer.class);
System.out.println(inboundEvent.getName() + "; " + data);
Assert.assertEquals(SSE_NAME, inboundEvent.getName());
Assert.assertEquals(MSG_COUNT - count.getCount(), data.intValue());
count.countDown();
} catch (ProcessingException ex) {
throw new RuntimeException("Error when deserializing of data.", ex);
}
}
};
eventSource.register(listener, "message-to-client");
eventSource.open();
final boolean sent = latch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
Assert.assertTrue("Awaiting for SSE message has timeout. Not all message were sent.", sent);
final boolean handled = count.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
Assert.assertTrue("Awaiting for SSE message has timeout. Not all message were handled by the listener.", handled);
}
use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class SseEventSinkToEventSourceTest method testWithJerseyApi.
@Test
public void testWithJerseyApi() throws InterruptedException {
final WebTarget endpoint = target().path("events");
final EventSource eventSource = EventSource.target(endpoint).build();
transmitLatch = new CountDownLatch(MSG_COUNT);
final CountDownLatch receiveLatch = new CountDownLatch(MSG_COUNT);
final List<Integer> results = new ArrayList<>();
final EventListener listener = inboundEvent -> {
try {
results.add(inboundEvent.readData(Integer.class));
receiveLatch.countDown();
Assert.assertEquals(INTEGER_SSE_NAME, inboundEvent.getName());
} catch (ProcessingException ex) {
throw new RuntimeException("Error when deserializing of data.", ex);
}
};
eventSource.register(listener, INTEGER_SSE_NAME);
eventSource.open();
Assert.assertTrue(transmitLatch.await(5000, TimeUnit.MILLISECONDS));
Assert.assertTrue(receiveLatch.await(5000, TimeUnit.MILLISECONDS));
Assert.assertEquals(10, results.size());
}
Aggregations