开发者

Does Smack work well in Java EE

Does Smack function properly in Java EE?? I am having issues with presence. I get the credentials from the login form via doPost method..I can able to successfully authenticate as well as connection.getRoster() also works.Next I want to show only users who are online so when I get the presence of user,presence object stores default value "unavailable" for all users even when they are available!!

The whole chat app works without flaw in a normal java class without any change..

            String userName = request.getParameter("username");
    String password = request.getParameter("password");

        HttpSession session=request.getSession();
        session.setAttribute("username", userName);

    SmackAPIGtalkServlet gtalk = new SmackAPIGtalkServlet();

    ConnectionConfiguration config = new ConnectionConfiguration(
            "talk.google.com", 5222, "gmail.com");
    connection = new XMPPConnection(config);
    config.setSASLAuthenticationEnabled(false);
    try {
        connection.connect();
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    try {
        connection.login(userName, password);
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    System.out.println(connection.isAuthenticated());
    boolean status = connection.isAuthenticated();
    if (status == true) {
        gtalk.displayOnlineBuddyList();
        response.sendRedirect("Roster.jsp");

    }
    else
    {
        response.sendRedirect("Failed.jsp");
    }
}

public void displayOnlineBuddyList() {
    Roster roster = connection.getRoster();
    Collection<RosterEntry> entries = roster.getEntries();
    int count1 = 0;
    int count2 = 0;
    for (RosterEntry r : entries) {
        Presence presence = roster.getPresence(r.getUser());
        if (presence.getType() == Presence.Type.unavailable) {
            // System.out.println(user + "is offline");
            count1++;
        } else {
            System.out.println(name+user + "is online");
            count2++;
        }
    }
    roster.addRosterListener(new RosterListener() {
        // Ignored events public void entriesAdded(Collection<String>
        // addresses) {}
        public void entriesDeleted(Collection<String> addresses) {
        }

        public void entriesUpdated(Collection<String> addresses) {
        }

        public void presenceChanged(Presence presence) {
            Sys开发者_StackOverflow社区tem.out.println("Presence changed: " + presence.getFrom()
                    + " " + presence);
        }

        @Override
        public void entriesAdded(Collection<String> arg0) {
            // TODO Auto-generated method stub

        }
    });
}

I am stuck with this and not able to get the code working with servlets..Can anyone help me out??


Will Smack work inside of Java EE, yes and no. Smack will work inside of a web container, but since it creates its own threads it will NOT work inside of an EJB container. So it will work depending on where you are running it.

To understand some of your issues, you have to understand that the lifecycle of your objects in a servlet is tied to the request/response cycle of each request. This is not the same as a standard java app where the objects will typically live as long as you need them to, since you control their lifecycle.

For example, in the code you have shown, you create the connection for each request (I assume, since not all the code is shown). Therefore registering listeners against that connection will be pointless since it will pass out of scope as soon as you leave the method, and eventually get garbage collected. You will have to maintain the connections outside of the scope of the servlet requests for this to work, otherwise you will be opening and closing connections for each request.

XMPP is completely asynchronous by nature whereas servlet requests are synchronous. You have to put some effort in to making them work together, so don't expect code that works in a standalone app to simply work in this environment.


You have to implement the RosterListener interface in which you have to override the presenceChanged method in that you can get the presence of the users.

It works for me.

When you are getting the rosters of GTalk all will have status as unavailable.

But after sometime their presence changes and the presence can be get from the presenceChanged method in the RosterListner but for that you have to implement the RosterListener's presenceChnaged method.

And ya it works well in Java EE, Android as well as WAP.


Does Smack function properly in Java EE?? I am having issues with presence. I get the credentials from the login form via doPost method..I can able to successfully authenticate as well as connection.getRoster() also works.Next I want to show only users who are online so when I get the presence of user,presence object stores default value "unavailable" for all users even when they are available!! here my code

<%
 Roster rst = roster;
 rst.addRosterListener(new RosterListener() {
public void entriesAdded(final Collection args) {}

public void entriesDeleted(final Collection<String> addresses) {}

public void entriesUpdated(final Collection<String> addresses) {}

public void presenceChanged(final Presence presence) {
    final Presence prsence1 = presence;
    prsenceChanged(prsence1);
    if (prsence1.isAvailable()) {
        System.out.println("Is Available: " + presence.isAvailable());
    } 


}
});

 %> 



<%!void prsenceChanged(Presence presence){ if(null != presence){%>
<script language="javascript"> 
    alert("hai");   

</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜