迭代hashmap时丢失条目

时间:2016-05-07 12:55:27

标签: java hashmap glassfish javabeans

我在应用程序范围的javabean中使用hashmap来存储用户会话。我使用以下代码使用socketconnection推送到这些会话:

public void sendToAllFollowers(User user, JsonObject message) throws IOException
{
    List<User> followingUsers = user.getFollowers();


    System.out.println(hashmap.size() + " active sessions before.");

    for(User u : followingUsers)
    {
        System.out.println(u.getUserName() + " found in followers list");

        Iterator it = hashmap.entrySet().iterator();
        while (it.hasNext()) {
            HashMap.Entry pair = (HashMap.Entry)it.next();
            User tempUser = (User) pair.getKey();
            it.remove(); // avoids a ConcurrentModificationException

            if(tempUser.getUserName().equals(u.getUserName()))
            {
                System.out.println(u.getUserName() + " found in hashmap: has active session");
                Session sendtosession = (Session) pair.getValue();
                sendtosession.getBasicRemote().sendText(message.toString());                      
            }
        }
    }
System.out.println(hashmap.size() + " active sessions after.");
}

如您所见,我将hashmap的大小写入服务器日志两次。在迭代之前和之后一次。迭代后,大小总是为零......

有什么可能导致这种情况的想法吗?正如我所说,它位于应用程序范围的bean中,并使用glassfish 4.1.0

部署在Web应用程序中

此外,我有时会在启动应用时遇到此错误:

Severe:   java.lang.RuntimeException: Thread's context class loader is null
at org.glassfish.weld.ACLSingletonProvider$ACLSingleton.getClassLoader(ACLSingletonProvider.java:147)
at org.glassfish.weld.ACLSingletonProvider$ACLSingleton.get(ACLSingletonProvider.java:108)
at org.jboss.weld.Container.instance(Container.java:65)

这可能与它有关吗?

控制台中的当前输出:

Info:   1 active sessions before.
Info:   jan found in followers list
Info:   jan found in hashmap: has active session
Info:   merel found in followers list
Info:   0 active sessions after.

0 个答案:

没有答案