欢迎来到天天文库
浏览记录
ID:23173701
大小:61.23 KB
页数:22页
时间:2018-11-05
《《jms框架学习》word版》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、Jms使用步骤:1.首先需要得到ConnectionFactory和Connection:ConnectionFactoryfactory=newActiveMQConnectionFactory(“vm://localhost”);Connectionconnection=factory.createConnection();2.由Connection创建一个session:Sessionsession=connection.createSession(false,Session.AUTO_ACKNOWLE
2、DGE);3.由Connection创建一个Destination,Destination是指消息发送的目的地,也就是前面说的Queue和Topic,创建好了一个消息后,只需要把这个消息发送到目的地,消息的发送者就可以继续做自己的事情,而不用等待消息被处理完成。至于这个消息什么时候,会被哪个消费者消费,完全取决于消息的接收者。Destinationdestination=session.createQueue(“testObject”);//点到点(P2P)模型//Destinationdestination
3、=session.createTopic(“testObject”);//发布/订阅(Pub/Sub)模型4.创建需要发送的Message:Messagemessage=session.createTextMessage(“Hello,JMS”);5.需要由Session和Destination创建一个消息生产者、发送消息:MessageProducerproducer=session.createProducer(Queue);Producer.send(message);6.创建一个消息消费者来接收这个消
4、息,并使用消息:MessageConsumercomsumer=session.createConsumer(queue);MessagerecyMessage=comsumer.receive();System.out.println(((TextMessage)recyMessage).getText());消息接收可以使用设置消息监听接口comsumer.setMessageListener(newMessageListener());来实现;DevelopingJMSapplicationsYouca
5、ncreateaJMSapplicationusingeitherthepoint-to-pointorthepublish/subscribemessagingmodel.Bothmodelssupportapplicationsthatarecapableof:·CreatingaJMSInitialContextobject·LookingupaConnectionFactoryobject·Creatingpermanentdestinations·Creatingconnections·Creati
6、ngsessions·Creatingmessageproducers·Creatingmessageconsumers·Implementingandinstallingmessagelisteners·Creatingmessages·Sendingmessages·Publishingmessages·Receivingmessages·Browsingmessages·EnablingJMStracing·Closingconnections,sessions,consumers,andproduce
7、rsYoucandownloadacopyoftheJMS1.0.2APIdocumentationfromtheJavaWebsite.CreatingaJMSInitialContextobjectAJMSclientapplicationmustinstantiateaJMSInitialContextobjectandsettheseproperties:·InitialContextfactory–setjava.naming.factory.initialto“com.sybase.jms.Ini
8、tialContextFactory”.·URL–setjava.naming.provider.urltothelocationwheretheclientcanconnecttoEAServer.·Username–validforaconnectionwithEAServer.·Password–validfortheusername.WheninstantiatingaJMSInitialC
此文档下载收益归作者所有