io.atomix.cluster.ClusterMembershipEventListener Java Examples

The following examples show how to use io.atomix.cluster.ClusterMembershipEventListener. 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: ClusterResource.java    From atomix with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/events")
@Produces(MediaType.APPLICATION_JSON)
public void getEvent(@Context ClusterMembershipService clusterMembershipService, @Context EventManager events, @Suspended AsyncResponse response) {
  EventLog<ClusterMembershipEventListener, ClusterMembershipEvent> eventLog = events.getOrCreateEventLog(ClusterResource.class, "", l -> e -> l.addEvent(e));
  if (eventLog.open()) {
    clusterMembershipService.addListener(eventLog.listener());
  }

  eventLog.nextEvent().whenComplete((result, error) -> {
    if (error == null) {
      response.resume(Response.ok(new NodeEvent(result.subject().id(), result.type())));
    } else {
      LOGGER.warn("{}", error);
      response.resume(Response.serverError().build());
    }
  });
}
 
Example #2
Source File: ClusterResource.java    From atomix with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/events/{id}")
@Produces(MediaType.APPLICATION_JSON)
public void getEvent(@PathParam("id") String listenerId, @Context ClusterMembershipService clusterMembershipService, @Context EventManager events, @Suspended AsyncResponse response) {
  EventLog<ClusterMembershipEventListener, ClusterMembershipEvent> eventLog = events.getEventLog(ClusterResource.class, listenerId);
  if (eventLog == null) {
    response.resume(Response.status(Status.NOT_FOUND).build());
    return;
  }

  eventLog.nextEvent().whenComplete((result, error) -> {
    if (error == null) {
      response.resume(Response.ok(new NodeEvent(result.subject().id(), result.type())));
    } else {
      LOGGER.warn("{}", error);
      response.resume(Response.serverError().build());
    }
  });
}
 
Example #3
Source File: ClusterResource.java    From atomix with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/nodes/{node}/events")
@Produces(MediaType.APPLICATION_JSON)
public void getNodeEvent(@PathParam("node") String memberId, @Context ClusterMembershipService clusterMembershipService, @Context EventManager events, @Suspended AsyncResponse response) {
  EventLog<ClusterMembershipEventListener, ClusterMembershipEvent> eventLog = events.getOrCreateEventLog(ClusterResource.class, memberId, l -> e -> {
    if (e.subject().id().id().equals(memberId)) {
      l.addEvent(e);
    }
  });
  if (eventLog.open()) {
    clusterMembershipService.addListener(eventLog.listener());
  }

  eventLog.nextEvent().whenComplete((result, error) -> {
    if (error == null) {
      response.resume(Response.ok(new NodeEvent(result.subject().id(), result.type())));
    } else {
      LOGGER.warn("{}", error);
      response.resume(Response.serverError().build());
    }
  });
}
 
Example #4
Source File: ClusterResource.java    From atomix with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/nodes/{node}/events/{id}")
@Produces(MediaType.APPLICATION_JSON)
public void getNodeEvent(@PathParam("node") String memberId, @PathParam("id") String listenerId, @Context ClusterMembershipService clusterMembershipService, @Context EventManager events, @Suspended AsyncResponse response) {
  EventLog<ClusterMembershipEventListener, ClusterMembershipEvent> eventLog = events.getEventLog(ClusterResource.class, getNodeListener(memberId, listenerId));
  if (eventLog == null) {
    response.resume(Response.status(Status.NOT_FOUND).build());
    return;
  }

  eventLog.nextEvent().whenComplete((result, error) -> {
    if (error == null) {
      response.resume(Response.ok(new NodeEvent(result.subject().id(), result.type())));
    } else {
      LOGGER.warn("{}", error);
      response.resume(Response.serverError().build());
    }
  });
}
 
Example #5
Source File: ClusterResource.java    From atomix with Apache License 2.0 5 votes vote down vote up
@POST
@Path("/events")
@Produces(MediaType.APPLICATION_JSON)
public Response addListener(@Context ClusterMembershipService clusterMembershipService, @Context EventManager events) {
  String listenerId = UUID.randomUUID().toString();
  EventLog<ClusterMembershipEventListener, ClusterMembershipEvent> eventLog = events.getOrCreateEventLog(ClusterResource.class, listenerId, l -> e -> l.addEvent(e));
  if (eventLog.open()) {
    clusterMembershipService.addListener(eventLog.listener());
  }
  return Response.ok(listenerId).build();
}
 
Example #6
Source File: ClusterResource.java    From atomix with Apache License 2.0 5 votes vote down vote up
@DELETE
@Path("/events/{id}")
public void removeListener(@PathParam("id") String listenerId, @Context ClusterMembershipService clusterMembershipService, @Context EventManager events) {
  EventLog<ClusterMembershipEventListener, ClusterMembershipEvent> eventLog = events.removeEventLog(ClusterResource.class, listenerId);
  if (eventLog != null && eventLog.close()) {
    clusterMembershipService.removeListener(eventLog.listener());
  }
}
 
Example #7
Source File: ClusterResource.java    From atomix with Apache License 2.0 5 votes vote down vote up
@POST
@Path("/nodes/{node}/events")
@Produces(MediaType.APPLICATION_JSON)
public Response addNodeListener(@PathParam("node") String memberId, @Context ClusterMembershipService clusterMembershipService, @Context EventManager events) {
  String id = UUID.randomUUID().toString();
  EventLog<ClusterMembershipEventListener, ClusterMembershipEvent> eventLog = events.getOrCreateEventLog(ClusterResource.class, getNodeListener(memberId, id), l -> e -> {
    if (e.subject().id().id().equals(memberId)) {
      l.addEvent(e);
    }
  });
  if (eventLog.open()) {
    clusterMembershipService.addListener(eventLog.listener());
  }
  return Response.ok(id).build();
}
 
Example #8
Source File: ClusterResource.java    From atomix with Apache License 2.0 5 votes vote down vote up
@DELETE
@Path("/nodes/{node}/events/{id}")
public void removeNodeListener(@PathParam("node") String memberId, @PathParam("id") String listenerId, @Context ClusterMembershipService clusterMembershipService, @Context EventManager events) {
  EventLog<ClusterMembershipEventListener, ClusterMembershipEvent> eventLog = events.removeEventLog(ClusterResource.class, getNodeListener(memberId, listenerId));
  if (eventLog != null && eventLog.close()) {
    clusterMembershipService.removeListener(eventLog.listener());
  }
}