use of org.ow2.proactive_grid_cloud_portal.scheduler.dto.eventing.EventSubscription in project scheduling by ow2-proactive.
the class SchedulerEventReceiver method openAndReceive.
@SuppressWarnings("rawtypes")
private void openAndReceive(final SchedulerEventListener eventListener, boolean myEventsOnly, SchedulerEvent... events) throws IOException {
Client client = ClientFactory.getDefault().newClient();
AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
if (insecure) {
builder = builder.setAcceptAnyCertificate(true).setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
}
AsyncHttpClientConfig httpClientConfig = builder.build();
AsyncHttpClient httpClient = new AsyncHttpClient(httpClientConfig);
socket = client.create(client.newOptionsBuilder().runtime(httpClient).reconnect(false).build());
EventNotificationHandler notificationHandler = new EventNotificationHandler(eventListener);
socket.on(Event.MESSAGE, notificationHandler);
socket.on(Event.CLOSE, new Function() {
public void on(Object t) {
SchedulerEventReceiver.logger.info("#### Websocket connection is closed ####");
if (eventListener instanceof DisconnectionAwareSchedulerEventListener) {
((DisconnectionAwareSchedulerEventListener) eventListener).notifyDisconnection();
}
}
});
// initialize the connection
RequestBuilder requestBuilder = client.newRequestBuilder();
requestBuilder.method(Request.METHOD.GET);
requestBuilder.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
// authentication header
requestBuilder.header("sessionid", sessionId);
requestBuilder.uri(eventingUrl(restServerUrl));
requestBuilder.encoder(new EventSubscriptionEncoder());
requestBuilder.decoder(new EventNotificationDecoder());
requestBuilder.transport(Request.TRANSPORT.WEBSOCKET);
socket.open(requestBuilder.build());
// submit subscription request
EventSubscription eventSubscription = new EventSubscription(myEventsOnly, asStringArray(events));
socket.fire(EventCodecUtil.toJsonString(eventSubscription));
}
use of org.ow2.proactive_grid_cloud_portal.scheduler.dto.eventing.EventSubscription in project scheduling by ow2-proactive.
the class SchedulerStateRest method publish.
/**
* Accepts an {@link EventSubscription} instance which specifies the types
* of SchedulerEvents which interest the client. When such Scheduler event
* occurs, it will be communicated to the client in the form of
* {@link EventNotification} utilizing the WebSocket channel initialized
* previously.
*/
@POST
@Path("/events")
@Produces("application/json")
public EventNotification publish(@Context HttpServletRequest req, EventSubscription subscription) throws NotConnectedRestException, PermissionRestException {
HttpSession session = req.getSession();
String broadcasterId = (String) session.getAttribute(ATM_BROADCASTER_ID);
final SchedulerProxyUserInterface scheduler = checkAccess(broadcasterId);
SchedulerEventBroadcaster eventListener = new SchedulerEventBroadcaster(broadcasterId);
try {
final SchedulerEventBroadcaster activedEventListener = PAActiveObject.turnActive(eventListener);
scheduler.addEventListener(activedEventListener, subscription.isMyEventsOnly(), EventUtil.toSchedulerEvents(subscription.getEvents()));
AtmosphereResource atmResource = getAtmosphereResourceFactory().find((String) session.getAttribute(ATM_RESOURCE_ID));
atmResource.addEventListener(new WebSocketEventListenerAdapter() {
@Override
public void onDisconnect(@SuppressWarnings("rawtypes") WebSocketEvent event) {
try {
logger.info("#### websocket disconnected remove listener ####");
scheduler.removeEventListener();
} catch (Exception e) {
logger.error(e);
}
PAActiveObject.terminateActiveObject(activedEventListener, true);
}
});
} catch (NotConnectedException e) {
throw new NotConnectedRestException(e);
} catch (PermissionException e) {
throw new PermissionRestException(e);
} catch (ActiveObjectCreationException | NodeException e) {
throw new RuntimeException(e);
}
return new EventNotification(EventNotification.Action.NONE, null, null);
}
Aggregations