我對activeMQ偵聽器有以下兩種情況的代碼。我期待的情況下2要快,但它不是...在單獨的線程或同一線程中創建ActiveMQ偵聽器的差異
案例1:我在這裏的主要功能創建的ActiveMQ多個監聽
public class FileReceiver {
public static void main(String[] args) {
List<MessageReceiver> messageReceiverList = new ArrayList<MessageReceiver>();
MessageReceiver msgReceiver1 = new MessageReceiver();
messageReceiverList.add(msgReceiver1);
msgReceiver1.run();
MessageReceiver msgReceiver2 = new MessageReceiver();
messageReceiverList.add(msgReceiver2);
msgReceiver2.run();
MessageReceiver msgReceiver3 = new MessageReceiver();
messageReceiverList.add(msgReceiver3);
msgReceiver3.run();
}
}
案例2:我在這裏在多線程
public class FileReceiver {
public static void main(String[] args) {
List<MessageReceiver> messageReceiverList = new ArrayList<MessageReceiver>();
for(int i = 0 ; i < 3 ; i++) {
MessageReceiver msgReceiver = new MessageReceiver();
Thread thread = new Thread(msgReceiver);
thread.start();
messageReceiverList.add(msgReceiver);
System.out.println("Listener " + i + " started.");
}
}
}
這裏創造的ActiveMQ多個監聽器是MessageReceiver類我使用...
class MessageReceiver implements Runnable,MessageListener {
private int numMsgsReceived = 0;
private void start() {
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"tcp://localhost:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
ActiveMQSession session = (ActiveMQSession) connection
.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination ftQueue = session.createQueue("TEMP.DEST");
MessageConsumer consumer = session.createConsumer(ftQueue);
consumer.setMessageListener(this);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onMessage(Message msg) {
incNumMsgsReceived();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
start();
}
}
在這兩種情況下,我每秒只能收到30封郵件。 情況2不應該更快,因爲它在三個獨立的線程上運行?