comet4j取不到map值
package com.sec.comet4j;import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.comet4j.core.CometConnection;
import org.comet4j.core.CometContext;
import org.comet4j.core.CometEngine;
import org.comet4j.core.event.ConnectEvent;
import org.comet4j.core.listener.ConnectListener;
public class HelloWorld extends ConnectListener implements
ServletContextListener {
private static final String CHANNEL = "hello";
Map<String, String> map = new HashMap<String, String>();;
CometContext cometContext = CometContext.getInstance();
public void contextInitialized(ServletContextEvent arg0) {
CometContext cc = CometContext.getInstance();
cc.registChannel(CHANNEL);
Thread helloAppModule = new Thread(new HelloAppModule(),
"Sender App Module");
helloAppModule.setDaemon(true);
CometEngine engine = cometContext.getEngine();
engine.addConnectListener(new HelloWorld());
helloAppModule.start();
}
class HelloAppModule implements Runnable {
CometEngine engine = cometContext.getEngine();
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (Exception ex) {
ex.printStackTrace();
}
String out = (String) cometContext.getServletContext()
.getAttribute("out");
List<CometConnection> connections = engine.getConnections();
System.out.println(map.size() + "集合的大小");
engine.sendTo(CHANNEL, connections, "");
engine.sendToAll(CHANNEL, out);
if (connections.size() >= 1) {
cometContext.getServletContext().removeAttribute("out");
}
}
}
}
public void contextDestroyed(ServletContextEvent arg0) {
}
public boolean handleEvent(ConnectEvent arg0) {
System.out.println("进入易做图");
// TODO Auto-generated method stub
System.out.println(cometContext.getServletContext().getAttribute(
"usersName"));
System.out.println(arg0.getConn().getId());
map.put((String) cometContext.getServletContext().getAttribute(
"usersName"), arg0.getConn().getId());
(这里在后台已经拿到值了,在这也put进去了,但是在上边判断集合大小时总是为0,为啥呢)
return true;
}
}
补充:Java , Web 开发