Java消息服務(JMS)實現了異步消息的傳遞,在企業級的應用中,極大的簡化了異步通信系統的實現,使用消息傳遞的優點在於保持了系統的松散耦合。
JMS支持點到點(P2P)模式和發布-訂閱(pub/sub)模式。點到點消息模式依賴於消息隊列概念,它將消息發送到稱為隊列(Queue)的特定目的地,接受者從隊列中獲取消息,對其進行處理。發布-訂閱模式則是將消息發送到成為主題(Topic)的目的地,使用者通過訂閱消息來獲取。因此,我們可以簡單的將Queue和Topic理解為消息的存儲器,或者接受器,通過它將消息轉發給消息最終的接收者。對於消息,可以是一個簡單的字符串,一段二進制流,或者一個任意的對象,當前JMS支持五種類型的消息:TextMessage、MapMessage、ByteMessage、 StreamMessage、ObjectMessage。
下面來看一段P2P的完整實現,
sender:
Context context = new InitialContext();
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) context.lookup("QueueConnectionFactoryTest");
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) context.lookup("QueueTest");
QueueSender queueSender = queueSession.createSender(queue);
TextMessage message = queueSession.createTextMessage();
message.setText("Hello JMS!");
queueSender.send(message);
queueConnection.close();
Receiver:
Context context = new InitialContext();
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) context.lookup("QueueConnectionFactoryTest");
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) context.lookup("QueueTest");
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
queueConnection.start();
while (true) {
Message m = queueReceiver.receive();
if (m instanceof TextMessage) {
TextMessage message = (TextMessage) m;
System.out.println("Reading message: " + message.getText());
} else {
break;
}
}
queueConnection.close();
由上代碼可以看出,sender和receiver的實現基本是相同的,下面我們再來對比一下pub/sub模式的實現,
publisher:
Context context = new InitialContext();
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) context.lookup("TopicConnectionFactoryTest");
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic) context.lookup("TopicTest");
TopicPublisher topicPublisher = topicSession.createPublisher(topic);
TextMessage sentMessage = topicSession.createTextMessage();
sentMessage.setText("Here is a message" + i);
System.out.println("PUBLISHER THREAD: Publishing message: " + sentMessage.getText());
topicPublisher.publish(sentMessage);
topicConnection.close();
Subscriber:
Context context = new InitialContext();
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) context.lookup("TopicConnectionFactoryTest");
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic) context.lookup("TopicTest");
TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic, null, true);
topicConnection.start();
TextMessage inMessage = (TextMessage) topicSubscriber.receive();
System.out.println("SUBSCRIBER THREAD: Reading message: "+ inMessage.getText());
topicConnection.close();
以上是對P2P和pub/sub兩種模式最簡單的實現,對於JMS的基本使用來說足夠了,對比以上兩種模式的實現,我們不難發現一些共同點:都是通過 QueueConnectionFactory/TopicConnectionFactory來生成QueueConnection /TopicConnection的實例,QueueConnection/TopicConnection用來建立和JMS的連接並生成會話實例 QueueSession/TopicSession,QueueSender的send方法和TopicPublisher的publish方法發送消息到Destination。 QueueReceiver和TopicSubscriber直接使用父接口MessageConsumer中定義的方法receive、 recieveNoWait等方法來接收消息。
如果你想在main方法中運行這些程序,你需要先在Application Server中配置ConnectionFactory和Destination的JNDI,可參考我的上篇文章《Standalone Client Lookup JNDI from the GlassFish》。
同樣,我們可以采用Message Driven Bean(MDB)作為JMS消息的receiver,sender將消息發送到MDB所監聽的目的地,EJB容器會調用MDB的onMessage方法去接收消息。以這種方式去實現JMS,應該更J2EE點。
JMS的實現就是如此的簡單,但簡單並不代表簡陋,JMS還擁有眾多的特性,以幫助你在實現中達到更多的目的,同時,還有很多的JMS provider能夠更加簡化您的JMS應用,比如ActiveMQ,更多的精彩需要你自己的發掘。