Search in sources :

Example 1 with Broadcaster

use of org.atmosphere.cpr.Broadcaster in project cxf by apache.

the class SseAtmosphereEventSinkImpl method close.

@Override
public void close() {
    if (!closed) {
        closed = true;
        LOG.fine("Closing Atmosphere SSE event output");
        if (resource.isSuspended()) {
            LOG.fine("Atmosphere resource is suspended, resuming");
            resource.resume();
        }
        final Broadcaster broadcaster = resource.getBroadcaster();
        resource.removeFromAllBroadcasters();
        try {
            final AtmosphereResponse response = resource.getResponse();
            try {
                // or stream.
                if (usingStream) {
                    response.getOutputStream().close();
                } else {
                    response.getWriter().close();
                }
            } catch (final IOException ex) {
                LOG.warning("Failed to flush AtmosphereResponse buffer: " + ex.getMessage());
            }
        } finally {
            try {
                resource.close();
            } catch (IOException ex) {
            // ignore
            }
            broadcaster.destroy();
            LOG.fine("Atmosphere SSE event output is closed");
        }
    }
}
Also used : AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) Broadcaster(org.atmosphere.cpr.Broadcaster) IOException(java.io.IOException)

Example 2 with Broadcaster

use of org.atmosphere.cpr.Broadcaster in project cxf by apache.

the class SseAtmosphereEventSinkContextProvider method createContext.

@Override
public SseEventSink createContext(Message message) {
    final HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
    if (request == null) {
        throw new IllegalStateException("Unable to retrieve HTTP request from the context");
    }
    final AtmosphereResource resource = (AtmosphereResource) request.getAttribute(AtmosphereResource.class.getName());
    if (resource == null) {
        throw new IllegalStateException("AtmosphereResource is not present, " + "is AtmosphereServlet configured properly?");
    }
    final Broadcaster broadcaster = resource.getAtmosphereConfig().getBroadcasterFactory().lookup(resource.uuid(), true);
    resource.removeFromAllBroadcasters();
    resource.setBroadcaster(broadcaster);
    final MessageBodyWriter<OutboundSseEvent> writer = new OutboundSseEventBodyWriter(ServerProviderFactory.getInstance(message), message.getExchange());
    return new SseAtmosphereEventSinkImpl(writer, resource);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) OutboundSseEvent(javax.ws.rs.sse.OutboundSseEvent) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) OutboundSseEventBodyWriter(org.apache.cxf.jaxrs.sse.OutboundSseEventBodyWriter) Broadcaster(org.atmosphere.cpr.Broadcaster)

Example 3 with Broadcaster

use of org.atmosphere.cpr.Broadcaster in project scheduling by ow2-proactive.

the class SchedulerStateRest method subscribe.

/*
     * Atmosphere 2.0 framework based implementation of Scheduler Eventing mechanism for REST
     * clients. It is configured to use WebSocket as the underneath protocol between the client and
     * the server.
     */
/**
 * Initialize WebSocket based communication channel between the client and
 * the server.
 */
@GET
@Path("/events")
public String subscribe(@Context HttpServletRequest req, @HeaderParam("sessionid") String sessionId) throws NotConnectedRestException {
    checkAccess(sessionId);
    HttpSession session = checkNotNull(req.getSession(), "HTTP session object is null. HTTP session support is requried for REST Scheduler eventing.");
    AtmosphereResource atmosphereResource = checkNotNull((AtmosphereResource) req.getAttribute(AtmosphereResource.class.getName()), "No AtmosphereResource is attached with current request.");
    // use session id as the 'topic' (or 'id') of the broadcaster
    session.setAttribute(ATM_BROADCASTER_ID, sessionId);
    session.setAttribute(ATM_RESOURCE_ID, atmosphereResource.uuid());
    Broadcaster broadcaster = lookupBroadcaster(sessionId, true);
    if (broadcaster != null) {
        atmosphereResource.setBroadcaster(broadcaster).suspend();
    }
    return null;
}
Also used : AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) HttpSession(javax.servlet.http.HttpSession) Broadcaster(org.atmosphere.cpr.Broadcaster) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

Broadcaster (org.atmosphere.cpr.Broadcaster)3 AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)2 IOException (java.io.IOException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 OutboundSseEvent (javax.ws.rs.sse.OutboundSseEvent)1 OutboundSseEventBodyWriter (org.apache.cxf.jaxrs.sse.OutboundSseEventBodyWriter)1 AtmosphereResponse (org.atmosphere.cpr.AtmosphereResponse)1