Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
642 views
in Technique[技术] by (71.8m points)

jsf - Notification system with p:poll/push

I've tried to implement the basic notification system for a basic social network with p:poll on view layer and a simple NotificationService class which gets the new notifications from DB and refreshes the notifications list of NotificationBean which is viewscoped for each user. Process flow similar to this:

-Poll calls NotificationBean.getNewNotifications for example every 15 sec.
--getNewNotifications calls NotificationService and DAO methods
---notificationList of user is refreshed
----DataTable on view layer shows new notifications

But the concern of p:poll is about it's performance because it sends a query at every interval expiration.

PrimeFaces has PrimePush which based on Atmosphere Framework, it opens web-sockets and seems like more suitable for creating notifications system.

But I don't know which components and which properties of them should be used. It has p:socket component with channel property. Should I use usernames as a channel values? Below code coming from PrimeFaces showcase and summarizes the last sentences:

<p:socket onMessage="handleMessage" channel="/notifications" /> 

As far as I understood from this showcase example this p:socket listens notifications channel. And pusher code snippet is:

PushContext pushContext = PushContextFactory.getDefault().getPushContext();       
pushContext.push("/notifications", new FacesMessage(summary, detail));

But this will notify all user pages, I need a pusher which notifies specific user. Let say there are 2 users and assume that User1 adds User2 as a friend. There must be sth. like that:

pushContext.push("User2/notifications", new FacesMessage("friendship request", "from User1"));

But I am not sure this is the correct usage for this kind of functional requirement or not. Considering scalability of the app there can be expensive cost of opening so many channels per a process.

Thanks for helping.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

PrimeFaces push supports one or more channels to push. To be able to create private channels for specific reasons; for example per user like in your case, you can create more than one channels. I had used unique ids for this purpose.

Basically, I've implemented a managed bean which is application scoped that handles user channel matching which should be considered. You can maintain it in different ways.

@ManagedBean
@ApplicationScoped
public class ChannelsBean {

    Map<String, String> channels = new HashMap<String, String>();

    public void addChannel(String user, String channel) {
        channels.put(user, channel);
    }

    public String getChannel(String user) {
        return channels.get(user);
    }

}

Then inject this bean in your backing bean which sends notifications.

@ManagedBean
@SessionScoped
public class GrowlBean {

    private String channel;

    @ManagedProperty(value = "#{channelsBean}")
    private ChannelsBean channels;

    private String sendMessageUser;

    private String user;

    @PostConstruct
    public void doPostConstruction() {
        channel = "/" + UUID.randomUUID().toString();
        channels.addChannel(user, channel);
    }

    public void send() {
        PushContext pushContext = PushContextFactory.getDefault().getPushContext();

        pushContext.push(channels.getChannel(sendMessageUser), new FacesMessage("Hi ", user));
    }

    //Getter Setters
}

You should give the channel value to p:socket. Here is the kickoff example of the page;

<p:growl widgetVar="growl" showDetail="true" />  

<h:form>  
    <p:panel header="Growl">  
        <h:panelGrid columns="2">  
            <p:outputLabel for="user" value="User: " />   
            <p:inputText id="user" value="#{growlBean.sendMessageUser}" required="true" />  


        </h:panelGrid>  

        <p:commandButton value="Send" actionListener="#{growlBean.send}" />  
    </p:panel>  
</h:form>  

<p:socket onMessage="handleMessage" channel="#{growlBean.channel}" />  

<script type="text/javascript">  
function handleMessage(facesmessage) {  
    facesmessage.severity = 'info';  

    growl.show([facesmessage]);  
}  
</script>  

For scalability issues, you should maintain the active or inactive channels. You can remove the one which is not in session or inactive for some time. Remove channels by using @PreDestroy annotation when beans are destroying. There is one channel for one user session in my solution.

My suggestion is; do not use usernames explicitly on the pages. It is not good for security reasons.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...