Java 如何使用 Atmosphere 设计推送通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19929175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to design push notifications using Atmosphere
提问by Saurabh Kumar
I want to use atmosphere to develop a notification System.
我想用大气来开发一个通知系统。
I am very new to Atmosphere so apologies if I am wrong somewhere. What i understood is when a Actor publishes something I save the notification action to the database. What i don't understand how the receiver will receive those notifications in realtime.
我对 Atmosphere 很陌生,所以如果我在某处错了,我深表歉意。我的理解是,当演员发布某些内容时,我会将通知操作保存到数据库中。我不明白接收者将如何实时接收这些通知。
The sender i know will do something like following
我认识的发件人将执行以下操作
event.getBroadcaster().broadcast(
objectMapper.writeValueAsString("Some Message"));
Now i am not able to figure out how the receiver can receive this message.
现在我无法弄清楚接收者如何接收此消息。
For example . I want to add a User Object as Friend. So when User1 adds User2 User1 broadcast but than how i push the notification to User2. I have difficulty in understanding this.
例如 。我想添加一个用户对象作为朋友。因此,当 User1 添加 User2 User1 广播时,而不是我如何将通知推送到 User2。我很难理解这一点。
Technically i want something similar like facebook or gmail notification where on user activity other users get notifications.
从技术上讲,我想要类似 facebook 或 gmail 通知的东西,在用户活动中,其他用户会收到通知。
采纳答案by elusive-code
Basically what you need is to implement Publish-subscribeon top of Atmosphere.
基本上你需要的是在 Atmosphere 之上实现发布订阅。
Atmosphere consists of two parts: client-side (javascript-based) and server-side(java-based).
Atmosphere 由两部分组成:客户端(基于javascript)和服务器端(基于java)。
First of all you need to configure server-side: Installing Atmosphere
首先你需要配置服务器端:安装大气
Namely servlet or filter, it is required so that it could add AtmosphereResourceto the HttpServletRequest.
即 servlet 或过滤器,它是必需的,以便它可以将AtmosphereResource添加到HttpServletRequest。
AtmosphereResourcerepresents a single client connection on the server-side.
AtmosphereResource表示服务器端的单个客户端连接。
Broadcasteris actually a container for these resources, so that you don't need to handle lookup/iteration/concurrency when you need to send to multiple connections. (Note that multiple connections can be produced by single client).
Broadcaster实际上是这些资源的容器,所以当你需要发送到多个连接时,你不需要处理查找/迭代/并发。(请注意,单个客户端可以产生多个连接)。
On the server-side you need to provide clients an endpoint to subscribe for notifications. For example, if you are using Spring-MVC, it could go like this (omitting validations/authentications, etc.):
在服务器端,您需要为客户端提供一个端点来订阅通知。例如,如果您使用的是 Spring-MVC,它可能是这样的(省略验证/身份验证等):
@RequestMapping(value = "/user-notifications/{userId}")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public void watch(@PathVariable("userId") String userId,
HttpServletRequest request) throws Exception {
//Atmosphere framework puts filter/servlet that adds ATMOSPHERE_RESOURCE to all requests
AtmosphereResource resource = (AtmosphereResource)request.getAttribute(ApplicationConfig.ATMOSPHERE_RESOURCE);
//suspending resource to keep connection
resource.suspend();
//find broadcaster, second parameter says to create broadcaster if it doesn't exist
Broadcaster broadcaster = BroadcasterFactory.getDefault().lookup(userId,true);
//saving resource for notifications
broadcaster.addAtmosphereResource(resource);
}
When something happens you can notify clients like this:
当发生某些事情时,您可以像这样通知客户:
public void notify(User user, Event event){
Broadcaster b = BroadcasterFactory.getDefault().lookup(user.getId());
if (b!=null){
b.broadcast(event);
}
}
On the client side you need to send a subscribe request and listen for subsequent events, like this:
在客户端,您需要发送订阅请求并监听后续事件,如下所示:
var request = new atmosphere.AtmosphereRequest();
request.url = '/user-notifications/'+userId;
request.transport = 'websocket';
request.fallbackTransport = 'streaming';
request.contentType = 'application/json';
request.reconnectInterval = 60000;
request.maxReconnectOnClose = 1000;
request.onMessage = function(response){
console.log(response);
alert('something happend<br>'+response);
};
that.watcherSocket = atmosphere.subscribe(request);
So, to sum it up:
所以,总结一下:
- Client sends request "I want to receive this kind of notifications".
- Server receives request, suspends and saves connection somewhere (either in your code or in Broadcaster).
- When something happens server looks for suspended connection and sends notification in it.
- Client receives notification and callback is invoked.
- Profit!!!
- 客户端发送请求“我想接收此类通知”。
- 服务器接收请求,在某处(在您的代码或广播中)暂停并保存连接。
- 当发生某些事情时,服务器会查找挂起的连接并在其中发送通知。
- 客户端收到通知并调用回调。
- 利润!!!
This wikihas explanations for some concepts behind Atmosphere and links to other documentation.
这个 wiki解释了 Atmosphere 背后的一些概念,并链接到其他文档。
回答by Dario
Atmosphere supports WebSocket. If you want to write a java client you may want to try https://github.com/TooTallNate/Java-WebSocket
Atmosphere 支持 WebSocket。如果你想写一个 java 客户端,你可能想尝试https://github.com/TooTallNate/Java-WebSocket
See section Writing your own WebSocket Client
请参阅编写您自己的 WebSocket 客户端部分