一、概述
訊息驅動 bean是一個非同步訊息消費者。當訊息到達訊息驅動bean服務的目的地或終端時,容器調用訊息驅動bean。訊息驅動bean執行個體是訊息驅動 bean類的執行個體。訊息驅動 bean 是為單個訊息類型定義的,與它僱用的訊息監聽器介面一致。
對於用戶端,訊息驅動 bean 是一個實現了一些商務邏輯運行在伺服器上的訊息消費者。用戶端通過將訊息發送到訊息驅動 bean 作為訊息監聽器的目的地或終端來訪問訊息驅動 bean。
訊息驅動 bean是匿名的。它們沒有用戶端可見的標識。
訊息驅動 bean執行個體沒有工作階段狀態。這意味著所有的 bean執行個體在服務於用戶端訊息時都是相等的。
訊息驅動 bean 執行個體由容器建立,它用於處理訊息。它的存留時間由容器控制。
訊息驅動bean 執行個體對特定用戶端來說沒有狀態。但是,訊息驅動bean 執行個體的執行個體變數可以在用戶端訊息處理的整個過程中保持狀態。例如,這種狀態是資料庫連接和對企業bean 的引用。
原文:
A message-drivenbean is an asynchronous message consumer. A message-driven bean is invoked by thecontainer as a result of the arrival of a message at the destination orendpoint that is serviced by the message-driven bean. A message-driven beaninstance is
an instance of a message-driven bean class. A message-driven beanis defined for a single messaging type, in accordance with the messagelistenerinterface it employs.
To a client, amessage-driven bean is a message consumer that implements some business logicrunning on the server. A client accesses a message-driven bean by sendingmessages to the destination or end-point for which the message-driven beanclass is the message
listener.
Message-drivenbeans are anonymous. They have no client-visible identity.
Message-drivenbean instances have no conversational state. This means that all bean instancesare equivalent when they are not involved in servicing a client message.
A message-drivenbean instance is created by the container to handle the processing of themessages for which the message-driven bean is the consumer. Its lifetime iscontrolled by the container.
A message-drivenbean instance has no state for a specific client. However, the instancevariables of the message-driven bean instance can contain state across thehandling of client messages. Examples of such state include an open databaseconnection and a reference
to an enterprise bean.
二、訊息驅動 bean執行個體和容器間的協議
從訊息驅動 bean 執行個體的建立到銷毀,它都生存在容器中。容器為訊息驅動bean 提供安全、並發、事務和其它服務。容器管理訊息驅動 bean 執行個體的生命週期,當調用bean時通知執行個體並提供全方位的服務來保證訊息驅動bean的實現是規模化的,且能支援大量訊息的並發處理。
從 bean提供者的視角來看,訊息驅動bean與容器具有相同的存留時間。當容器啟動時保證訊息驅動bean存在並在訊息轉寄開始前準備好bean執行個體來接收非同步訊息都是容器的責任。
容器本身對訊息驅動bean執行個體沒有服務需求。容器調用bean執行個體是為了讓執行個體擷取容器的服務和轉寄容器產生的通知。
由於所有的訊息驅動 bean 執行個體都是相等的,因此用戶端訊息可以被轉寄給任何一個執行個體。
原文:
Protocol Betweena Message-Driven Bean Instance and its Container
From itscreation until destruction, a message-driven bean instance lives in acontainer. The container provides security, concurrency, transactions, andother services for the message-driven bean. The con-tainer manages the lifecycle of the message-driven
bean instances, notifying the instances when bean actionmay be necessary, and providing a full range of services to ensure that themessage-driven bean implementation is scalable and can support the concurrentprocessing of a large number of messages.
From the BeanProvider’s point of view, a message-driven bean exists as long as its containerdoes. It is the container’s responsibility to ensure that the message-drivenbean comes into existence when the container is started up and that instances ofthe bean
are ready to receive an asynchronous message delivery before thedelivery of messages is started.
Containersthemselves make no actual service demands on the message-driven bean instances.The calls a container makes on a bean instance provide it with access tocontainer services and deliver notifica-tions issued by the container.
Since allinstances of a message-driven bean are equivalent, a client message can bedelivered to any available instance.
三、訊息驅動 bean方法的事務上下文
Bean的訊息監聽器和逾時回調方法的事務範圍由bean的中繼資料注釋符或部署檔案指定的事務屬性決定。如果指定 bean 使用容器管理的事務分割,那麼必須為訊息監聽器方法使用 REQUIRED 或 NOT_SUPPORTED 事務屬性,為逾時回調方法是用 REQUIRED、REQUIRES_NEW 或 NOT_SUPPORTED 事務屬性。
當訊息驅動 bean 使用 bean 管理的事務分割,用javax.transaction.UserTransaction介面來分割事務時,產生bean 調用的訊息接收不是事務的一部分。如果訊息接收是事務的一部分,必須使用事務屬性是REQUIRED的容器管理事務分割。
newInstance 方法,setMessageDrivenContext,訊息驅動 bean 的依賴注入方法,以及生命週期回調方法都在未指定的事務上下文中調用。
四、異常處理
訊息驅動 bean的訊息監聽器方法不能拋出java.rmi.RemoteException。
訊息驅動 bean通常不應當拋出RuntimeException。
從訊息驅動 bean 類的任何方法(包括訊息監聽器方法和由容器調用的回調方法)中拋出的 RuntimeException 會引起 bean 的狀態轉變成“不存在”狀態。
如果訊息驅動 bean 使用 bean 管理事務分割並拋出 RuntimeException,那麼容器不應當確認訊息。當多個這種方法應用到 bean類上時應用到生命週期回調攔截器方法的規則。
從用戶端視角來看,訊息消費者仍繼續存在。如果用戶端繼續向與 bean 關聯的目的地或終端發送訊息,那麼容器可以將訊息代理給另一個執行個體。
某些訊息類型的訊息監聽器方法可以拋出應用異常。應用異常被容器傳遞到資來源配接器。