企业级应用系统体系架构EJB0中的MDB.PPT

上传人:国*** 文档编号:1050836 上传时间:2018-11-26 格式:PPT 页数:97 大小:996.50KB
下载 相关 举报
企业级应用系统体系架构EJB0中的MDB.PPT_第1页
第1页 / 共97页
企业级应用系统体系架构EJB0中的MDB.PPT_第2页
第2页 / 共97页
企业级应用系统体系架构EJB0中的MDB.PPT_第3页
第3页 / 共97页
企业级应用系统体系架构EJB0中的MDB.PPT_第4页
第4页 / 共97页
企业级应用系统体系架构EJB0中的MDB.PPT_第5页
第5页 / 共97页
点击查看更多>>
资源描述

1、企业级应用系统体系架构 (五) EJB3.0中的 MDB*From:OReilly: Enterprise JavaBeans 3.0, 5th Edition, May.20061Topicn Message-Driven Beansn JMS and Message-Driven Beansn JMS-Based Message-Driven Beansn The Life Cycle of a Message-Driven Beann Connector-Based Message-Driven Beansn Message Linking2JMS and Message-Driven

2、Beansn All EJB 3.0 vendors must support a JMS provider. Most vendors have a JMS provider built in and must support other JMS providers through the JCA. n However, regardless of whether your vendor has its own JMS provider or allows you to integrate some other provider, a JMS provider is an absolute

3、necessity for supporting message-driven beans. n By forcing the adoption of JMS, Sun has guaranteed that EJB developers can expect to have a working JMS provider on which messages can be both sent and received.3JMS as a Resourcen JMS is a vendor-neutral API that can be used to access enterprise mess

4、aging systems. Enterprise messaging systems (a.k.a. message-oriented middleware) facilitate the exchange of messages between software applications over a network. The role of JMS isnt unlike the role of JDBC: just as JDBC provides a common API for accessing many different relational databases, JMS p

5、rovides vendor-independent access to enterprise messaging systems. n Although messaging products arent as familiar as database products, theres no shortage of messaging systems that support JMS, including JBossMQ, IBMs MQSeries, BEAs WebLogic JMS service, Sun Microsystems Sun ONE Message Queue, and

6、Sonics SonicMQ. Software applications that use the JMS API for sending or receiving messages are portable from one JMS vendor to another.n Applications that use JMS are called JMS clients , and the messaging system that handles routing and delivery of messages is called the JMS provider . A JMS appl

7、ication is a business system composed of many JMS clients and, generally, one JMS provider. A JMS client that sends a message is called a producer , and a JMS client that receives a message is called a consumer . A single JMS client can be both a producer and a consumer.n In EJB, enterprise beans of

8、 all types can use JMS to send messages. The messages are consumed by other Java applications or by message-driven beans. JMS facilitates sending messages from enterprise beans using a messaging service , sometimes called a message broker or router. n Message brokers have been around for a couple of

9、 decades-the oldest and most established is IBMs MQSeries but JMS is fairly new, and it is specifically designed to deliver a variety of message types from one Java application to another.4Reimplementing the TravelAgent EJB with JMSn We can modify the TravelAgent EJB so that it uses JMS to alert som

10、e other Java application that a reservation has been made. The following code shows how to modify the bookPassage( ) method so that the TravelAgent EJB sends a simple text message based on a description obtained from the TicketDO object:Resource(mappedName=“ConnectionFactoryNameGoesHere“) private Co

11、nnectionFactory connectionFactory; Resource(mappedName=“TicketTopic“) private Topic topic; Removepublic TicketDO bookPassage(CreditCardDO card, double price) throws IncompleteConversationalState if (customer = null | cruise = null | cabin = null) throw new IncompleteConversationalState( ); try Reser

12、vation reservation = new Reservation( customer, cruise, cabin, price, new Date( ); entityManager.persist(reservation); process.byCredit(customer, card, price); TicketDO ticket = new TicketDO(customer, cruise, cabin, price);5Reimplementing the TravelAgent EJB with JMSn Connection connect = factory.cr

13、eateConnection( ); Session session = connect.createSession(true,0); MessageProducer producer = session.createProducer(topic); TextMessage textMsg = session.createTextMessage( );textMsg.setText(ticketDescription); producer.send(textMsg);connect.close( );return ticket; catch(Exception e) throw new EJB

14、Exception(e); n While all the code we added might look a little overwhelming, the basics of JMS are not all that complicated.6ConnectionFactory and Topicn In order to send a JMS message, we need a connection to the JMS provider and a destination address for the message. A JMS connection factory make

15、s the connection to the provider possible; the destination address is identified by a Topic object. Both the connection factory and the Topic object are obtained by using javax.annotation.Resource to inject these objects directly into the fields of the TravelAgent EJB:Resource(mappedName=“Connection

16、FactoryNameGoesHere“) private ConnectionFactory connectionFactory; Resource(mappedName=“TicketTopic“)private Topic topic; n The ConnectionFactory is similar to a DataSource in JDBC. Just as the DataSource provides a JDBC connection to a database, the ConnectionFactory provides a JMS connection to a

17、message router.n The Topic object itself represents a network-independent destination to which the message will be addressed. In JMS, messages arent sent directly to applications; theyre sent to topics or queues. A topic is analogous to an email list or newsgroup; any application with the proper cre

18、dentials can receive messages from and send messages to a topic. When a JMS client receives messages from a topic, the client is said to subscribe to that topic. JMS decouples applications by allowing them to send messages to each other through a destination, which serves as a virtual channel. A que

19、ue is another type of destination that well discuss in detail later.7Connection and Sessionn The ConnectionFactory is used to create a Connection, which is an actual connection to the JMS provider:Connection connect = connectionFactory.createConnection( );Session session = connect.createSession(true

20、,0); n Once you have a Connection , you can use it to create a Session. A Session allows you to group the actions of sending and receiving messages. In this case, you need only a single Session. Using multiple Sessions is helpful if you wish to produce and consume messages in different threads. Sess

21、ion objects use a single-threaded model, which prohibits concurrent access to a single Session from multiple threads. The thread that creates a Session is usually the thread that uses that Sessions producers and consumers (i.e., MessageProducer and MessageConsumer objects). If you wish to produce an

22、d consume messages using multithreading, you must create a different Session object for each thread.8Connection and Sessionn The createSession( ) method has two parameters:createSession(boolean transacted, int acknowledgeMode) n According to the EJB specifications, these arguments are ignored at run

23、time because the EJB container manages the transaction and acknowledgment mode of any JMS resource obtained from the JNDI ENC. The specification recommends that developers use the arguments true for transacted and 0 for acknowledgeMode, but since they are supposed to be ignored, it should not matter

24、 what you use. Unfortunately, not all vendors adhere to this part of the specification. Some vendors ignore these parameters; others do not.n Its good programming practice to close a Connection after it has been used:Connection connect = factory.createConnection( ); .connect.close( ); 9MessageProduc

25、ern The Session is used to create a MessageProducer, which sends messages from the TravelAgent EJB to the destination specified by the Topic object. Any JMS clients that subscribe to that topic will receive a copy of the message:MessageProducer producer = session.createProducer(topic); TextMessage textMsg = session.createTextMessage( );textMsg.setText(ticketDescription); producer.send(textMsg); 10

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 重点行业资料库 > 1

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。