JMS框架学习.docx
上传人:sy****28 上传时间:2024-09-14 格式:DOCX 页数:22 大小:61KB 金币:16 举报 版权申诉
预览加载中,请您耐心等待几秒...

JMS框架学习.docx

JMS框架学习.docx

预览

免费试读已结束,剩余 12 页请下载文档后查看

16 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

Jms使用步骤:1.首先需要得到ConnectionFactory和Connection:ConnectionFactoryfactory=newActiveMQConnectionFactory(“vm://localhost”);Connectionconnection=factory.createConnection();2.由Connection创建一个session:Sessionsession=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);3.由Connection创建一个Destination,Destination是指消息发送的目的地,也就是前面说的Queue和Topic,创建好了一个消息后,只需要把这个消息发送到目的地,消息的发送者就可以继续做自己的事情,而不用等待消息被处理完成。至于这个消息什么时候,会被哪个消费者消费,完全取决于消息的接收者。Destinationdestination=session.createQueue(“testObject”);//点到点(P2P)模型//Destinationdestination=session.createTopic(“testObject”);//发布/订阅(Pub/Sub)模型4.创建需要发送的Message:Messagemessage=session.createTextMessage(“Hello,JMS”);5.需要由Session和Destination创建一个消息生产者、发送消息:MessageProducerproducer=session.createProducer(Queue);Producer.send(message);6.创建一个消息消费者来接收这个消息,并使用消息:MessageConsumercomsumer=session.createConsumer(queue);MessagerecyMessage=comsumer.receive();System.out.println(((TextMessage)recyMessage).getText());消息接收可以使用设置消息监听接口comsumer.setMessageListener(newMessageListener());来实现;DevelopingJMSapplicationsYoucancreateaJMSapplicationusingeitherthepoint-to-pointorthepublish/subscribemessagingmodel.Bothmodelssupportapplicationsthatarecapableof:HYPERLINK"http://192.87.31.188/docs/easpg/lnkCHDEGHHB.htm"\t"_top"CreatingaJMSInitialContextobjectHYPERLINK"http://192.87.31.188/docs/easpg/lnkCHDJECJB.htm"\t"_top"LookingupaConnectionFactoryobjectHYPERLINK"http://192.87.31.188/docs/easpg/lnkCHDCFDHF.htm"\t"_top"CreatingpermanentdestinationsHYPERLINK"http://192.87.31.188/docs/easpg/lnkCHDJBIHF.htm"\t"_top"CreatingconnectionsHYPERLINK"http://192.87.31.188/docs/easpg/lnkCHDCBFDB.htm"\t"_top"CreatingsessionsHYPERLINK"http://192.87.31.188/docs/easpg/lnkCHDEBABB.htm"\t"_top"CreatingmessageproducersHYPERLINK"http://192.87.31.188/docs/easpg/lnkCHDHABGI.htm"\t"_top"CreatingmessageconsumersHYPERLINK"http://192.87.31.188/docs/easpg/lnkCHDDAHHB.htm"\t"_top"ImplementingandinstallingmessagelistenersHYPERLINK"http://192.87.31.188/docs/