在这篇文章中会描述在我们收到了消息、分型指标和读回复之后,如何立刻把它们发往客户端。内容会包含我们是如何使用Play框架和Akka Actor Model来管理长连接、由服务器主动发送事件的。我们也会分享一些在生产环境中我们是如何在服务器上做负载测试,来管理数十万条并发长连接的,还有一些心得。最后,我们会分享在整个过程中我们用到的各种优化方法。
// Client A connects to the server and is assigned connectionIdA
public Result listen() {
return ok(EventSource.whenConnected(eventSource -> {
String connectionId = UUID.randomUUID().toString();
// construct an Akka Actor with the new EventSource connection identified by a random connection identifier
Akka.system().actorOf(
ClientConnectionActor.props(connectionId, eventSource),
connectionId);
}));
}
// User B sends a message to User A
// We identify the Actor which manages the connection on which User A is connected (connectionIdA)
ActorSelection actorSelection = Akka.system().actorSelection("akka://application/user/" + connectionIdA);
// Send B's message to A's Actor
actorSelection.tell(new ClientMessage(data), ActorRef.noSender());
java.lang.OutOfMemoryError: unable to create new native thread
下面关于我们服务器上JVM线程数量的图也证实了我们当时出现了线程泄露,内存也快耗尽了。
我们把JVM进程的线程状态打出来查看了一下,发现了许多处于如下状态的睡眠线程:
"Hashed wheel timer #11327" #27780 prio=5 os_prio=0 tid=0x00007f73a8bba000 nid=0x27f4 sleeping[0x00007f7329d23000] java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at org.jboss.netty.util.HashedWheelTimer$Worker.waitForNextTick(HashedWheelTimer.java:445)
at org.jboss.netty.util.HashedWheelTimer$Worker.run(HashedWheelTimer.java:364)
at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
at java.lang.Thread.run(Thread.java:745)