轉自百度百科:
JMS
JMS(Java Messaging Service)是Java平台上有關面向消息中間件(MOM)的技術規范,它便於消息系統中的Java應用程序進行消息交換,並且通過提供標准的產生、發送、接收消息的接口簡化企業應用的開發,翻譯為Java消息服務。
簡介
JMS是一種與廠商無關的 API,用來訪問消息收發系統消息。它類似於 JDBC(Java Database Connectivity):這里,JDBC 是可以用來訪問許多不同關系數據庫的 API,而 JMS 則提供同樣與廠商無關的訪問方法,以訪問消息收發服務。許多廠商目前都支持 JMS,包括 IBM 的 MQSeries、BEA的 Weblogic JMS service和 Progress 的 SonicMQ,這只是幾個例子。 JMS 使您能夠通過消息收發服務(有時稱為消息中介程序或路由器)從一個 JMS 客戶機向另一個 JMS客戶機發送消息。消息是 JMS 中的一種類型對象, 由兩部分組成:報頭和消息主體。報頭由路由信息以及有關該消息的元數據組成。消息主體則攜帶着應用程序的數據或有效負載。根據有效負載的類型來划分,可以 將消息分為幾種類型,它們分別攜帶:簡單文本 (TextMessage)、可序列化的對象 (ObjectMessage)、屬性集合 (MapMessage)、字節流 (BytesMessage)、原始值流 (StreamMessage),還有無有效負載的消息 (Message)。
體系架構
JMS對象模型
1)連接工廠。連接工廠(ConnectionFactory)是由管理員創建,並綁定到JNDI樹中。客戶端使用JNDI查找連接工廠,然后利用連接工廠創建一個JMS連接。
2)JMS連接。JMS連接(Connection)表示JMS客戶端和服務器端之間的一個活動的連接,是由客戶端通過調用連接工廠的方法建立的。
3)JMS會話。JMS會話(Session)表示JMS客戶與JMS服務器之間的會話狀態。JMS會話建立在JMS連接上,表示客戶與服務器之間的一個會話線程。
4)JMS目的。JMS目的(Destination),又稱為消息隊列,是實際的消息源。
5)JMS生產者和消費者。生產者(Message Producer)和消費者(Message Consumer)對象由Session對象創建,用於發送和接收消息。
6)JMS消息通常有兩種類型: ① 點對點(Point-to-Point)。在點對點的消息系統中,消息分發給一個單獨的使用者。點對點消息往往與隊列(javax.jms.Queue)相關聯。 ② 發布/訂閱(Publish/Subscribe)。發布/訂閱消息系統支持一個事件驅動模型,消息生產者和消費者都參與消息的傳遞。生產者發布事件,而使用者訂閱感興趣的事件,並使用事件。該類型消息一般與特定的主題(javax.jms.Topic)關聯。
定義connection bean <bean id="connectionFactory" //it knows how to connect to ActiveMQ(message broker) class="org.apache.activemq.spring.ActiveMQConnectionFactory"> <propertyname="brokerURL"value="tcp://localhost:61616"/> //where the message broker
is located,jms消息提供者的地址 </bean> 定義消息隊列 <bean id="queue"class="org.apache.activemq.command.ActiveMQQueue"> <constructor-argvalue="spitter.queue"/> </bean> 或者 <bean id="topic"class="org.apache.activemq.command.ActiveMQTopic"> <constructor-argvalue="spitter.topic"/> </bean>
//send message ConnectionFactorycf=new ActiveMQConnectionFactory("tcp://localhost:61616"); Connectionconn=null; Session session=null; try { conn =cf.createConnection();//創建連接 session=conn.createSession(false,Session.AUTO_ACKNOWLEDGE); //創建session Destinationdestination=new ActiveMQQueue("spitter.queue"); //消息隊列 MessageProducerproducer=session.createProducer(destination); //創建生產者 TextMessagemessage=session.createTextMessage(); //創建消息 message.setText("Helloworld!"); producer.send(message); //發布 } catch(JMSExceptione){ // handleexception? } finally{ try { if (session!=null){ session.close(); } if (conn!=null){ conn.close(); } } catch(JMSExceptionex){ } } //receive message ConnectionFactorycf=new ActiveMQConnectionFactory("tcp://localhost:61616"); Connectionconn=null; Session session=null; try { conn =cf.createConnection(); conn.start(); session=conn.createSession(false,Session.AUTO_ACKNOWLEDGE); Destinationdestination=new ActiveMQQueue("spitter.queue"); MessageConsumerconsumer=session.createConsumer(destination); Messagemessage=consumer.receive(); TextMessagetextMessage=(TextMessage)message; System.out.println("GOTAMESSAGE:"+textMessage.getText()); conn.start(); } catch(JMSExceptione){ // handleexception? } finally{ try { if (session!=null){ session.close(); } if (conn!=null){ conn.close(); } } catch(JMSExceptionex){ } }
使用jmsTemplate可以簡化以上步驟,處理異常
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <propertyname="connectionFactory"ref="connectionFactory"/> <propertyname="defaultDestinationName" value="spittle.alert.queue"/> </bean>
//send message
public classAlertServiceImplimplementsAlertService{ public void sendSpittleAlert(finalSpittlespittle){ jmsTemplate.send( "spittle.alert.queue", //發送到的消息隊列,如果jsmTemplate有設置defaultDestinationName,則可以省略這個參數 new MessageCreator(){ public MessagecreateMessage(Sessionsession) throws JMSException{ return session.createObjectMessage(spittle); //ask for an object message from the session, giving it the Spittle object to build
the object message from. } } ); } @Autowired JmsTemplatejmsTemplate; }
//receive message public SpittlegetAlert(){ try { ObjectMessagereceivedMessage=(ObjectMessage) jmsTemplate.receive();//Receive message return(Spittle)receivedMessage.getObject();//Get object } catch(JMSExceptionjmsException){ throwJmsUtils.convertJmsAccessException(jmsException); } } //唯一一處使用jmstemplate 需要處理異常,由於ObjectMessage’s getObject()拋出
//receive()是同步的
消息監聽
<bean id="spittleHandler" class="com.habuma.spitter.alerts.SpittleAlertHandler"/> <jms:listener-containerconnection-factory="connectionFactory"> <jms:listenerdestination="spitter.alert.queue" ref="spittleHandler"method="processSpittle"/> </jms:listener-container>
public classSpittleAlertHandler{ public voidprocessSpittle(Spittlespittle){ // ...implementationgoeshere... } }
參考: http://blog.csdn.net/kkdelta/article/details/5604218
http://uh.9ria.com/space-63908-do-blog-id-7075.html 詳解
http://wenku.baidu.com/view/f8af7923ccbff121dd368314.html 技術原理