public void registerNewClient(EventOutput eventOutput) {
   sseBroadcaster.add(eventOutput);
   OutboundEvent event =
       eventBuilder
           .name("new-client")
           .mediaType(MediaType.TEXT_PLAIN_TYPE)
           .data(String.class, "One more client")
           .build();
   sseBroadcaster.broadcast(event);
 }
Esempio n. 2
0
 @POST
 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
 public void addItem(@FormParam("name") String name) {
   ITEMS.add(name);
   // Broadcasting an un-named event with the name of the newly added item in data
   BROADCASTER.broadcast(new OutboundEvent.Builder().data(String.class, name).build());
   // Broadcasting a named "add" event with the current size of the items collection in data
   BROADCASTER.broadcast(
       new OutboundEvent.Builder().name("size").data(Integer.class, ITEMS.size()).build());
 }
 @GET
 @Path("events")
 @Produces(SseFeature.SERVER_SENT_EVENTS)
 public EventOutput subscribe() {
   EventOutput eventOutput = new EventOutput();
   broadcaster.add(eventOutput);
   return eventOutput;
 }
Esempio n. 4
0
 @GET
 @Path("events")
 @Produces(SseFeature.SERVER_SENT_EVENTS)
 public EventOutput itemEvents() {
   final EventOutput eventOutput = new EventOutput();
   BROADCASTER.add(eventOutput);
   return eventOutput;
 }
 @GET
 @Produces(SseFeature.SERVER_SENT_EVENTS)
 public EventOutput get() {
   EventOutput output = new EventOutput();
   broadcaster.add(output);
   scheduler.scheduleAtFixedRate(this::broadcastStatistics, 0, 1000, TimeUnit.MILLISECONDS);
   return output;
 }
Esempio n. 6
0
    public void run() {
      try {
        if (latch != null) {
          // wait for all test EventSources to be registered
          latch.await(5, TimeUnit.SECONDS);
        }

        broadcaster.broadcast(
            new OutboundEvent.Builder()
                .name("domain-progress")
                .data(String.class, "starting domain " + id + " ...")
                .build());
        broadcaster.broadcast(
            new OutboundEvent.Builder().name("domain-progress").data(String.class, "50%").build());
        broadcaster.broadcast(
            new OutboundEvent.Builder().name("domain-progress").data(String.class, "60%").build());
        broadcaster.broadcast(
            new OutboundEvent.Builder().name("domain-progress").data(String.class, "70%").build());
        broadcaster.broadcast(
            new OutboundEvent.Builder().name("domain-progress").data(String.class, "99%").build());
        broadcaster.broadcast(
            new OutboundEvent.Builder().name("domain-progress").data(String.class, "done").build());
        broadcaster.closeAll();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
 @Override
 public void message(String author, String message) {
   OutboundEvent event =
       eventBuilder
           .name("tweet")
           .mediaType(MediaType.TEXT_PLAIN_TYPE)
           .data(String.class, message)
           .build();
   sseBroadcaster.broadcast(event);
 }
Esempio n. 8
0
 public void removeListener(int id, String key) {
   // TODO Close the event output stream.
   if (outputs.containsKey(id)) {
     EventOutput oput = outputs.get(id).remove(key);
     if (oput != null) {
       try {
         oput.close();
       } catch (IOException e) {
         System.out.println("State.java: Line 321 error");
       }
     }
   } else {
     // if
     SseBroadcaster bdcast = listeners.remove(key);
     if (bdcast != null) // Check if there is residue EventOutput in the
     // broadcaster
     {
       bdcast.closeAll();
     }
   }
   // Done
 }
  @DELETE
  @Path("broker")
  public void deleteConfiguration() {
    broker.setConfiguration(new Configuration());

    // Broadcast new stream list
    OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
    OutboundEvent event =
        eventBuilder
            .name("broker")
            .mediaType(MediaType.APPLICATION_JSON_TYPE)
            .data(Configuration.class, broker.getConfiguration())
            .build();
    broadcaster.broadcast(event);
  }
  @DELETE
  @Path("broker/{routing-id}")
  public void deleteRouting(@PathParam("routing-id") String name) {
    broker.removeRouting("^" + name + "$");

    // Broadcast new stream list
    OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
    OutboundEvent event =
        eventBuilder
            .name("broker")
            .mediaType(MediaType.APPLICATION_JSON_TYPE)
            .data(Configuration.class, broker.getConfiguration())
            .build();
    broadcaster.broadcast(event);
  }
  @PUT
  @Path("broker/{routing-id}")
  public void addRouting(@PathParam("routing-id") String name, Routing routing) {
    routing.setName(name); // Ensure that name is the same as the one specified on the URL
    broker.addRouting(routing);

    // Broadcast new stream list
    OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
    OutboundEvent event =
        eventBuilder
            .name("broker")
            .mediaType(MediaType.APPLICATION_JSON_TYPE)
            .data(Configuration.class, broker.getConfiguration())
            .build();
    broadcaster.broadcast(event);
  }
  @PUT
  @Path("broker")
  @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public void setConfiguration(Configuration configuration) {
    broker.setConfiguration(configuration);

    // Broadcast new stream list
    OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
    OutboundEvent event =
        eventBuilder
            .name("broker")
            .mediaType(MediaType.APPLICATION_JSON_TYPE)
            .data(Configuration.class, broker.getConfiguration())
            .build();
    broadcaster.broadcast(event);
  }
  private void broadcastStatistics() {
    List<URI> monitoringEndpoints = monitoringApp.getMonitoredApps();
    List<MonitoringData> monitoringData =
        monitoringEndpoints
            .stream()
            .map(
                (endpointUri) -> {
                  Response response = client.target(endpointUri).path("monitoring").request().get();
                  MonitoringData data = response.readEntity(MonitoringData.class);
                  data.setNode(endpointUri.getHost() + ":" + endpointUri.getPort());
                  return data;
                })
            .collect(Collectors.toList());

    OutboundEvent event =
        new OutboundEvent.Builder()
            .mediaType(MediaType.APPLICATION_JSON_TYPE)
            .data(monitoringData)
            .build();
    broadcaster.broadcast(event);
  }