本文整理汇总了Java中org.springframework.web.servlet.mvc.method.annotation.SseEmitter.send方法的典型用法代码示例。如果您正苦于以下问题:Java SseEmitter.send方法的具体用法?Java SseEmitter.send怎么用?Java SseEmitter.send使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.web.servlet.mvc.method.annotation.SseEmitter
的用法示例。
在下文中一共展示了SseEmitter.send方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendNotification
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
public void sendNotification(String username, Notification notification){
Consumer consumer = this.consumers.findByUsername(username);
notification = notifications.save(notification);
consumer.addNotifications(notification);
consumers.save(consumer);
if (this.subscriptions.containsKey(username)) {
for (SseEmitter sse : this.subscriptions.get(username)) {
try {
sse.send(notification, MediaType.APPLICATION_JSON);
} catch (IOException ex) {
this.logger.warn("Broken SSE Emitter..discarding it...");
}
}
}
}
示例2: getRealTimeJudgeResultAction
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
/**
* 获取实时的评测结果.
* @param submissionId - 提交记录的唯一标识符
* @return 包含评测结果信息的StreamingResponseBody对象
* @throws IOException
*/
@RequestMapping("/getRealTimeJudgeResult.action")
public SseEmitter getRealTimeJudgeResultAction(
@RequestParam(value="submissionId") long submissionId,
@RequestParam(value="csrfToken") String csrfToken,
HttpServletRequest request, HttpServletResponse response) throws IOException {
User currentUser = HttpSessionParser.getCurrentUser(request.getSession());
boolean isCsrfTokenValid = CsrfProtector.isCsrfTokenValid(csrfToken, request.getSession());
Submission submission = submissionService.getSubmission(submissionId);
if ( !isCsrfTokenValid || submission == null ||
!submission.getUser().equals(currentUser) ||
!submission.getJudgeResult().getJudgeResultSlug().equals("PD") ) {
throw new ResourceNotFoundException();
}
response.addHeader("X-Accel-Buffering", "no");
SseEmitter sseEmitter = new SseEmitter();
submissionEventListener.addSseEmitters(submissionId, sseEmitter);
sseEmitter.send("Established");
return sseEmitter;
}
示例3: submissionEventHandler
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
/**
* 提交事件的处理器.
* @param event - 提交记录事件
* @throws IOException
*/
@EventListener
public void submissionEventHandler(SubmissionEvent event) throws IOException {
long submissionId = event.getSubmissionId();
String judgeResult = event.getJudgeResult();
String message = event.getMessage();
boolean isCompleted = event.isCompleted();
SseEmitter sseEmitter = sseEmitters.get(submissionId);
if ( sseEmitter == null ) {
LOGGER.warn(String.format("CANNOT get the SseEmitter for submission #%d.", submissionId));
return;
}
Map<String, String> mapMessage = new HashMap<>(3, 1);
mapMessage.put("judgeResult", judgeResult);
mapMessage.put("message", message);
sseEmitter.send(mapMessage);
if ( isCompleted ) {
sseEmitter.complete();
removeSseEmitters(submissionId);
}
}
示例4: sendEventUpdateMessage
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
@Override
public void sendEventUpdateMessage(EventType event, String dashboardId) {
List<SseEmitter> emitters = emittersPerDashboard.get(dashboardId);
if(emitters != null){
if(event != EventType.PING) {
sendEventUpdateMessage(EventType.PING, dashboardId);
}
LOGGER.info("Notifying {} dashboards with name {} and event type {}", emitters.size(), dashboardId, event);
for(int i = emitters.size(); i > 0; i--) {
SseEmitter sseEmitter = emitters.get(i-1);
try {
Map<String, String> message = new HashMap<>();
message.put("type", event.getValue());
String jsonMessage = objectMapper.writeValueAsString(message);
sseEmitter.send(jsonMessage, MediaType.APPLICATION_JSON);
} catch (IOException e) {
this.removeFromSessionsMap(sseEmitter, dashboardId);
LOGGER.error("Exception while sending message to emitter for dashboard {}", dashboardId);
}
}
}
}
示例5: send
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
public void send(long id, Object data) throws IOException {
SseEmitter emitter = emitters.get(id);
if (emitter == null) {
LOGGER.warn(() -> "Failed to locate emitter - id=" + id);
} else {
emitter.send(data);
LOGGER.info(() -> "Sent data via emitter - id=" + id);
}
}
示例6: submissionEventHandler
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
@EventListener
public void submissionEventHandler(SubmissionEvent event) throws IOException {
String key = event.getKey();
Object message = event.getMessage();
SseEmitter sseEmitter = sseEmitters.get(key);
if ( sseEmitter == null ) {
return;
}
sseEmitter.send(message, MediaType.APPLICATION_JSON);
}
开发者ID:quebic-source,项目名称:microservices-sample-project,代码行数:15,代码来源:SseEmitterApplicationEventListener.java
示例7: sendEvent
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
private void sendEvent(SseEventBuilder event){
synchronized (sseEmitters) {
for(SseEmitter emitter: sseEmitters){
try {
System.out.println("Enviando el evento");
emitter.send(event);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
示例8: 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;
}
示例9: append
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
private void append(String name, Message<?> message) {
String key = (String) message.getHeaders().get(ROUTE_KEY);
if (message.getHeaders().getReplyChannel() instanceof MessageChannel) {
MessageChannel replyChannel = (MessageChannel) message.getHeaders()
.getReplyChannel();
replyChannel.send(message);
return;
}
Route route = new Route(key, name);
String path = route.getPath();
if (!queues.containsKey(path)) {
Bridge<Message<?>> flux = new Bridge<>();
queues.putIfAbsent(path, flux);
}
queues.get(path).send(message);
if (emitters.containsKey(path)) {
Set<SseEmitter> list = new HashSet<>(emitters.get(path));
for (SseEmitter emitter : list) {
try {
emitter.send(message.getPayload());
}
catch (IOException e) {
emitters.get(path).remove(emitter);
}
}
}
}
示例10: jsonCreate
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
@RequestMapping(path = "/", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
Comment jsonCreate(Comment comment) throws IOException {
Comment newComment = this.commentRepository.save(comment);
synchronized (this.sseEmitters) {
for (SseEmitter sseEmitter : this.sseEmitters) {
// Servlet containers don't always detect ghost connection, so we must catch exceptions ...
try {
sseEmitter.send(newComment, MediaType.APPLICATION_JSON);
} catch (Exception e) {}
}
}
return comment;
}
示例11: 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;
}
示例12: streamTweetEvent
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //导入方法依赖的package包/类
public void streamTweetEvent(List<SseEmitter> emitters) throws InterruptedException{
List<StreamListener> listeners = new ArrayList<StreamListener>();
StreamListener streamListener = new StreamListener() {
@Override
public void onWarning(StreamWarningEvent warningEvent) {
}
@Override
public void onTweet(Tweet tweet) {
//log.info("User '{}', Tweeted : {}, from ; {}", tweet.getUser().getName() , tweet.getText(), tweet.getUser().getLocation());
Integer connectedUsers = emitters.size();
//log.info("Streaming to :" + connectedUsers +" connected Users");
if (connectedUsers!=0) {
for (SseEmitter emiter : emitters) {
try {
emiter.send(SseEmitter.event().name("streamLocation").data(tweet.getUser().getLocation()));
StringBuilder hashTag = new StringBuilder();
List<HashTagEntity> hashTags = tweet.getEntities().getHashTags();
for (HashTagEntity hash : hashTags) {
hashTag.append("#"+hash.getText() + " ");
}
//System.out.println(hashTag);
emiter.send(SseEmitter.event().name("streamHashtags").data(hashTag));
} catch (IOException e) {
System.out.println("User Disconnected from the Stream");
//e.printStackTrace();
}
}
}else{
//Close Stream when all Users are disconnected.
userStream.close();
log.info("Zero Connected Users - Closing Stream");
}
}
@Override
public void onLimit(int numberOfLimitedTweets) {
}
@Override
public void onDelete(StreamDeleteEvent deleteEvent) {
}
};
//Start Stream when a User is connected
if (emitters.size()==1) {
listeners.add(streamListener);
userStream = twitter.streamingOperations().sample(listeners);
}
// Stream from a specific Location:
// Float west=-122.75f;
// Float south=36.8f;
// Float east=-121.75f;
// Float north = 37.8f;
//
// FilterStreamParameters filterStreamParameters = new FilterStreamParameters();
// filterStreamParameters.addLocation(west, south, east, north);
//Stream userStream = twitter.streamingOperations().filter(filterStreamParameters, listeners);
}