本文整理汇总了Java中org.springframework.web.servlet.mvc.method.annotation.SseEmitter.onCompletion方法的典型用法代码示例。如果您正苦于以下问题:Java SseEmitter.onCompletion方法的具体用法?Java SseEmitter.onCompletion怎么用?Java SseEmitter.onCompletion使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.web.servlet.mvc.method.annotation.SseEmitter
的用法示例。
在下文中一共展示了SseEmitter.onCompletion方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getResults
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
@GetMapping("/api/notification/feed")
public ResponseEntity<SseEmitter> getResults() {
if(sseEngine==null){
LOGGER.warn(()->"No SSE Engine available");
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
final Long fLoggedInUserId = getLoggedInUserId();
SseEmitter emitter = sseEngine.getEmitters().get(fLoggedInUserId);
if (emitter == null) {
LOGGER.debug(() -> "Creating new emitter");
SseEmitter fEmitter = emitter = new SseEmitter();
emitter.onCompletion(() -> {
LOGGER.debug(() -> "Emitter completed");
sseEngine.getEmitters().remove(fLoggedInUserId);
});
emitter.onTimeout(() -> {
fEmitter.complete();
});
sseEngine.getEmitters().put(fLoggedInUserId, emitter);
}
return new ResponseEntity<>(emitter, HttpStatus.OK);
}
示例2: progress
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
@GetMapping("/progress/{name}")
SseEmitter progress(@PathVariable String name) {
UUID key = UUID.fromString(name);
SseEmitter emitter = new SseEmitter();
progress.addListener(key, a -> {
try {
emitter.send(a);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
if (a >= 1) {
emitter.complete();
}
});
emitter.onCompletion(() -> progress.removeListener(key));
return emitter;
}
示例3: updateHTML
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
@RequestMapping("/userPriv/updates")
SseEmitter updateHTML() {
SseEmitter sseEmitter = new SseEmitter();
synchronized (this.sseEmitters) {
this.sseEmitters.add(sseEmitter);
sseEmitter.onCompletion(() -> {
synchronized (this.sseEmitters) {
this.sseEmitters.remove(sseEmitter);
}
});
}
return sseEmitter;
}
示例4: streamTweets
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
@RequestMapping("/tweetLocation")
public SseEmitter streamTweets() throws InterruptedException{
SseEmitter sseEmitter = new SseEmitter();
emitters.add(sseEmitter);
sseEmitter.onCompletion(() -> emitters.remove(sseEmitter));
streamTweetEventService.streamTweetEvent(emitters);
return sseEmitter;
}
示例5: emit
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
private SseEmitter emit(Route route, Message<Collection<Object>> message)
throws IOException {
SseEmitter emitter = new SseEmitter(Long.MAX_VALUE);
String path = route.getPath();
if (!emitters.containsKey(path)) {
emitters.putIfAbsent(path, new HashSet<>());
}
emitters.get(path).add(emitter);
emitter.onCompletion(() -> emitters.get(path).remove(emitter));
emitter.onTimeout(() -> emitters.get(path).remove(emitter));
for (Object body : message.getPayload()) {
emitter.send(body);
}
return emitter;
}
示例6: subscribeUpdates
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
@RequestMapping("/sse/updates")
SseEmitter subscribeUpdates() {
SseEmitter sseEmitter = new SseEmitter();
synchronized (this.sseEmitters) {
this.sseEmitters.add(sseEmitter);
sseEmitter.onCompletion(() -> {
synchronized (this.sseEmitters) {
this.sseEmitters.remove(sseEmitter);
}
});
}
return sseEmitter;
}
示例7: serverSideEmitter
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
@GetMapping(value = "/emitter/{dashboardId}")
public SseEmitter serverSideEmitter(@PathVariable String dashboardId) throws IOException {
LOGGER.info("Creating SseEmitter for dashboard {}", dashboardId);
SseEmitter sseEmitter = new NotCachedSseEmitter();
sseEmitter.onCompletion(() -> {
handler.removeFromSessionsMap(sseEmitter, dashboardId);
sseEmitter.complete();
});
sseEmitter.onTimeout(() -> {
handler.removeFromSessionsMap(sseEmitter, dashboardId);
sseEmitter.complete();
});
handler.addToSessionsMap(sseEmitter, dashboardId);
sseEmitter.send(SseEmitter.event().reconnectTime(0L));
return sseEmitter;
}
示例8: event
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
@RequestMapping(value="/v1/events", method=RequestMethod.GET)
public SseEmitter event(
@RequestHeader(value="Last-Event-ID", required=false) String lastEventIdHeader,
@RequestParam(required=true, value="topic") String topic,
@RequestParam(required=false, value="rewind") Integer rewind,
@RequestParam(required=false, value="lastEventId") Long lastEventIdOverride
) throws IOException
{
final SseEmitter sseEmitter = new SseEmitter();
final Emitter emitter = new Emitter(sseEmitter);
logger.debug("Opening emitter {}", emitter.hashCode());
this.emitters.add(emitter);
final Long lastEventId = lastEventIdOverride != null ? lastEventIdOverride : (lastEventIdHeader != null ? Long.parseLong(lastEventIdHeader) : null);
final KfkaPredicate p = new KfkaPredicate().topic(topic);
if (lastEventId != null)
{
p.messageId(lastEventId + 1);
p.relativeOffset(Integer.MIN_VALUE + 10);
}
else
{
final int offset = rewind != null ? Math.abs(rewind) * -1 : -20;
p.relativeOffset(offset);
}
final KfkaMessageListener l = kfkaManager.addListener((msg)->
{
try
{
emitter.emit(msg);
}
catch (IOException exc)
{
throw Throwables.propagate(exc);
}
}, p);
emitter.setListener(l);
// Remove on completion
sseEmitter.onCompletion(new Runnable()
{
@Override
public void run()
{
logger.debug("Emitter {} closed", emitter.hashCode());
emitters.remove(emitter);
kfkaManager.removeListener(emitter.getListener());
}
});
return sseEmitter;
}
示例9: subscribe
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
public SseEmitter subscribe(String username) {
SseEmitter sseEmitter = new SseEmitter(0l);
sseEmitter.onCompletion(new DeleteTask(this, username, sseEmitter));
this.subscriptions.put(username, sseEmitter);
return sseEmitter;
}
示例10: bindUnsubscribe
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
/**
* Unsubscribes the subscription when the emitter completes or times out
* @param emitter
* @param subscription
*/
public static void bindUnsubscribe(SseEmitter emitter, Subscription subscription) {
emitter.onCompletion(subscription::unsubscribe);
emitter.onTimeout(subscription::unsubscribe);
}