[英]Java Socket Client pattern
My java application has to send messages(multithreaded) to a socket server. 我的java应用程序必须将消息(多线程)发送到套接字服务器。 The application can send around 100-200 messages a second.
该应用程序每秒可以发送大约100-200条消息。
I want to know which is a better approach to do this ? 我想知道哪种方法更好?
Which is a better practical approach ? 哪种更实用?
I would propose 3. : Open an socket per thread, and reuse threads (for example via thread pool). 我建议3.:每个线程打开一个套接字,并重用线程(例如通过线程池)。 Then handle reconnection inside thread, or just dispose it properly and create new one.
然后处理线程内部的重新连接,或者只是正确处理它并创建新的线程。 This way you can avoid blocking and synchronisation issues
这样您就可以避免阻塞和同步问题
100-200 messages per second isn't that much. 每秒100-200条消息并不多。 I wouldn't re-connect every time as this is expensive.
我不会每次都重新连接,因为这很昂贵。 If you re-use your connection, it will be much faster.
如果您重新使用连接,它会快得多。
If you are worried about losing messages, you can send a batch of messages or one at a time and wait for a confirmation from the server they have been received. 如果您担心丢失消息,可以一次发送一批消息,然后等待收到服务器的确认。 You can still send thousands of messages per second this way.
您仍然可以通过这种方式每秒发送数千封邮件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.