简体   繁体   English

即时消息传递-Java上的单连接或多线程

[英]Instant messaging— single connection or multithreading on Java

I'm supposed to send out instant emails to a set of contacts in a time-critical application. 我应该将即时电子邮件发送给时间紧迫的应用程序中的一组联系人。

Say, I have n contacts in the list. 说,我名单上有n位联络人。 So, i can do either one of the following: 因此,我可以执行以下任一操作:

i.) send out a single email to all of these n contacts as multiple receivers. i。)将一封电子邮件作为多个收件人发送给所有这n个联系人。

ii.)send n emails, separately one for each receiver, in n threads. ii。)在n个线程中发送n封电子邮件,每个收件人分别发送一封。

In the first case, i'm setting up a single connection at my end and just sending it out. 在第一种情况下,我将在一端建立单个连接,然后将其发送出去。

In the second, n connections to my smtp server. 第二,连接到我的smtp服务器。 However, it is concurrent. 但是,它是并发的。

Which one is faster? 哪一个更快?

第一种情况比较快,因为它只向服务器发送大约x个字节(消息的大小),第二种情况是通过(我假设)单根导线(无论字节是否交错)向服务器发送大约x * n个字节。

With your first option only one copy will be sent to every recipient and it should be comparatively faster. 使用您的第一个选项,只有一个副本将发送给每个收件人,并且它应该相对较快。 But depends the amount of recipient and server capacity for it. 但是取决于接收者和服务器的容量。 With your second option though its concurrent 'n' connections are made and same copies will be sent in each thread. 使用第二个选项,尽管会建立并发的'n'连接,并且在每个线程中将发送相同的副本。 Ratio :: A Thread run = A mail sent to individual recipient. 比率::线程运行=发送给单个收件人的邮件。

Yeas, second option will be better if you have to schedule task for sending mails to larger masses. 是的,如果您必须安排将邮件发送给更大数量的任务,则第二种选择会更好。 for ex: A MNC sending mails to its 80000 Employees in that case Recipient limit may come handy and mails would be sent in chunk of X' recipient ie 5000 employee at once. 例如:跨国公司向其80000名员工发送邮件,在这种情况下,收件人限制可能会很方便,并且邮件将以X收件人的块(即5000名员工)一次发送。 Whereas in this case first option may turn into failure because of server failure. 在这种情况下,由于服务器故障,第一个选项可能会变成故障。

Practical Example: I work in Infotech and here group of mail recipients are maintained depending upon department but there is one general group where mail id of all recipient are stored. 实际示例:我在Infotech工作,在这里根据部门维护一组邮件收件人,但是有一个通用组存储所有收件人的邮件ID。 By mistake one of the user triggered mail to that general group and after that session of mails began causing complete server failure. 用户之一错误地触发了向该常规组的邮件,并且在该会话之后开始导致服务器完全故障。

In this case factor like server capacity has larger impact. 在这种情况下,服务器容量等因素会产生较大影响。

email are not designed to be instant messaging. 电子邮件并非设计为即时消息。
If you want instant messaging you need p2p type of connection. 如果要即时消息传递,则需要p2p类型的连接。 If you intend to use emails than it does not matter how you will send them, too many other factors will interfere. 如果您打算使用电子邮件,则与如何发送它们无关紧要,太多其他因素也会干扰。 Generally 1st method will be (marginally) faster as it will allow mail server to optimise the way the messages are sent. 通常,第一种方法将(略微)更快(因为它可以使邮件服务器优化邮件的发送方式)。

See ServerSocket() http://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html and Socket() docs.oracle.com/javase/7/docs/api/java/net/Socket.html 请参阅ServerSocket() http://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html和Socket()docs.oracle.com/javase/7/docs/api/java/net /Socket.html

Again you will need p2p as above if you need guaranteed delivery, if speed is more important and the server/client are on the same network you may want to look at MulticastSocket http://download.java.net/jdk7/archive/b123/docs/api/java/net/ 同样,如果您需要保证交付,则您将如上所述需要p2p,如果速度更为重要并且服务器/客户端位于同一网络上,则可能需要查看MulticastSocket http://download.java.net/jdk7/archive/b123 / docs / api / java / net /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM