Java Code Examples for javax.ws.rs.sse.SseEventSource#close()

The following examples show how to use javax.ws.rs.sse.SseEventSource#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServerSentEventServiceTest.java    From mangooio with Apache License 2.0 6 votes vote down vote up
@Test
public void testCloseConnection() throws InterruptedException {
	//given
	ServerSentEventService ServerSentEventService = Application.getInstance(ServerSentEventService.class);
	Config config = Application.getInstance(Config.class);
	Client client = ClientBuilder.newClient();

	//when
	WebTarget webTarget = client.target("http://" + config.getConnectorHttpHost() + ":" + config.getConnectorHttpPort() + "/sse");
	SseEventSource sseEventSource = SseEventSource.target(webTarget).build();
	sseEventSource.register((sseEvent) -> {eventData = sseEvent.readData();}, (e) -> e.printStackTrace());
	sseEventSource.open();
	ServerSentEventService.close("/sse");
	sseEventSource.close();
	client.close();

	//then
	assertThat(ServerSentEventService.getConnections("/sse"), not(nullValue()));
	assertThat(ServerSentEventService.getConnections("/sse").size(), equalTo(0));
}
 
Example 2
Source File: ServerSentEventServiceTest.java    From mangooio with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendData() throws InterruptedException {
	//given
	Config config = Application.getInstance(Config.class);
	Client client = ClientBuilder.newClient();
	String data = UUID.randomUUID().toString();
	
	//when
	WebTarget webTarget = client.target("http://" + config.getConnectorHttpHost() + ":" + config.getConnectorHttpPort() + "/sse");
	SseEventSource sseEventSource = SseEventSource.target(webTarget).build();
	sseEventSource.register((sseEvent) -> {eventData = sseEvent.readData();}, (e) -> e.printStackTrace());
	sseEventSource.open();
       
       //then
       Application.getInstance(ServerSentEventService.class).send("/sse", data);
       await().atMost(2,  TimeUnit.SECONDS).untilAsserted(() -> assertThat(eventData, equalTo(data)));
       sseEventSource.close();
       client.close();
}
 
Example 3
Source File: PriceTest.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    // Check we don't have any prices
    List<Price> prices = RestAssured.get("/prices/all").as(new TypeRef<List<Price>>() {});
    Assertions.assertTrue(prices.isEmpty());

    // Stream the prices
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target(PRICES_SSE_ENDPOINT);
    List<Double> received = new CopyOnWriteArrayList<>();
    SseEventSource source = SseEventSource.target(target).build();
    source.register(inboundSseEvent -> received.add(Double.valueOf(inboundSseEvent.readData())));
    source.open();

    // Send the prices
    Price p1 = new Price();
    p1.value = 1.0;
    Price p2 = new Price();
    p2.value = 4.0;
    Price p3 = new Price();
    p3.value = 2.0;
    RestAssured.given().header("Content-Type", "application/json").body(p1).post("/").then().statusCode(204);
    RestAssured.given().header("Content-Type", "application/json").body(p2).post("/").then().statusCode(204);
    RestAssured.given().header("Content-Type", "application/json").body(p3).post("/").then().statusCode(204);

    await().atMost(100000, MILLISECONDS).until(() -> received.size() == 3);
    source.close();

    Assertions.assertTrue(received.contains(p1.value));
    Assertions.assertTrue(received.contains(p2.value));
    Assertions.assertTrue(received.contains(p3.value));

    prices = RestAssured.get("/prices/all").as(new TypeRef<List<Price>>() {});
    Assertions.assertEquals(prices.size(), 3);
}
 
Example 4
Source File: PriceResourceTest.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@Test
void testPricesEventStream() {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target(PRICES_SSE_ENDPOINT);

    List<Double> received = new CopyOnWriteArrayList<>();

    SseEventSource source = SseEventSource.target(target).build();
    source.register(inboundSseEvent -> received.add(Double.valueOf(inboundSseEvent.readData())));
    source.open();
    await().atMost(100000, MILLISECONDS).until(() -> received.size() == 3);
    source.close();
}
 
Example 5
Source File: JaxrsTest.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
@Test
public void testSSEApplication() throws
        InterruptedException, MalformedURLException, TimeoutException {

    AtomicInteger atomicInteger = new AtomicInteger();

    registerApplication(
        new TestSSEApplication(), JAX_RS_APPLICATION_BASE, "/sse");
    registerAddon(
        new SSEResource(), JAX_RS_APPLICATION_SELECT,
        "(" + JAX_RS_APPLICATION_BASE + "=/sse)");
    registerExtension(
        ContainerResponseFilter.class,
        (req, res) -> atomicInteger.incrementAndGet(), "Filter",
        JAX_RS_APPLICATION_SELECT,
        "(" + JAX_RS_APPLICATION_BASE + "=/sse)");

    SseEventSourceFactory sseFactory = createSseFactory();

    SseEventSource source1 = sseFactory.newSource(
        createDefaultTarget().path("/sse").path("/subscribe"));

    SseEventSource source2 = sseFactory.newSource(
        createDefaultTarget().path("/sse").path("/subscribe"));

    ArrayList<String> source1Events = new ArrayList<>();
    ArrayList<String> source2Events = new ArrayList<>();

    Phaser phaser = new Phaser(2);

    source1.register(
        event -> {source1Events.add(event.readData(String.class));phaser.arrive(); });
    source2.register(
        event -> {source2Events.add(event.readData(String.class));phaser.arrive(); });

    source1.open();
    source2.open();

    phaser.awaitAdvanceInterruptibly(0, 10, TimeUnit.SECONDS);

    //The filter IS invoked on the subscribe method
    assertEquals(2, atomicInteger.get());

    WebTarget broadcast = createDefaultTarget().path("/sse").path(
        "/broadcast");

    broadcast.request().post(
        Entity.entity("message", MediaType.TEXT_PLAIN_TYPE));

    phaser.awaitAdvanceInterruptibly(1, 10, TimeUnit.SECONDS);

    assertEquals(Arrays.asList("welcome", "message"), source1Events);
    assertEquals(Arrays.asList("welcome", "message"), source2Events);

    source2.close();
    phaser.arrive();

    atomicInteger.set(0);

    broadcast.request().post(
        Entity.entity("another message", MediaType.TEXT_PLAIN_TYPE));

    phaser.awaitAdvanceInterruptibly(2, 10, TimeUnit.SECONDS);

    assertEquals(
        Arrays.asList("welcome", "message", "another message"),
        source1Events);
    assertEquals(Arrays.asList("welcome", "message"), source2Events);

    source1.close();

    //The filter IS invoked when broadcasting events
    assertEquals(1, atomicInteger.get());
}