Example usage for org.springframework.web.servlet.mvc.method.annotation SseEmitter completeWithError

List of usage examples for org.springframework.web.servlet.mvc.method.annotation SseEmitter completeWithError

Introduction

In this page you can find the example usage for org.springframework.web.servlet.mvc.method.annotation SseEmitter completeWithError.

Prototype

public synchronized void completeWithError(Throwable ex) 

Source Link

Document

Complete request processing with an error.

Usage

From source file:com.bodybuilding.argos.controller.SseEmitterUtil.java

/**
 * Binds the supplied observable to the SseEmitter. onNext will call the send method on SseEmitter, the Observable
 * subscription will automatically be cancelled when the emitter completes or times out.
 * @param emitter SseEmitter/*from w ww  .j ava2 s.c o  m*/
 * @param observable Observable that will supply the data
 * @return Subscription to the Observable
 */
public static <T> Subscription bindObservable(SseEmitter emitter, Observable<T> observable) {
    Subscription subscription = observable.subscribe(new Subscriber<T>() {
        @Override
        public void onCompleted() {
            emitter.complete();
        }

        @Override
        public void onError(Throwable e) {
            LOG.warn("Error from stream observable", e);
            emitter.completeWithError(e);
        }

        @Override
        public void onNext(T t) {
            emitSse(emitter, t);
        }
    });
    bindUnsubscribe(emitter, subscription);
    return subscription;
}