Three. Information Driven Beans Introduction
Asynchronous messages can also be implemented by message-driven beans. In the EJB 1.1 specification, two types of EJBS are defined. are entity beans (Entity beans) and session beans. The client is usually a method of calling beans in a synchronized, blocking way. Message-driven beans combine the functionality of EJB and JMS.
As mentioned earlier, session beans usually implement business logic, and clients cannot share a session bean. Entity beans usually correspond to some entity entries in persistent storage. Both of these beans usually have remote and home interfaces to interact with the client. And, these interactions are synchronized, blocking the way. For example, a request is sent to a bean, through a blocking method call, the server returns a corresponding. The caller can proceed to the next process after receiving the return. Message-driven beans are typically configured to be a special subject (topic) or queue of clients, as consumers of messages. But message-driven beans do not have a home and remote interface. When a message creator writes a message to the topic or queue, it is not known that the consumer is a message-driven bean. This allows for a lot of flexibility when integrating a distributed computing system. Message-driven beans have no conversational state, and all instances are the same without processing the request, which is similar to stateless session beans. Putting the beans in the buffer pool is also an efficient way to handle the message-driven beans. A message-driven bean must be inherited from the Javax.ejb.MessageDrivenBean interface either indirectly or directly. This interface is inherited by Javax.jms.MessageListener. One of the parameters of this method is javax.jms.Message. can be any valid JMS message type. The declaration of a method does not contain a thrown statement. Therefore, in message processing, application exceptions are not still out. When a container receives a message, it first obtains a message-driven bean from a buffer pool, and then, if the configuration file requires it, the container also sets up a link to the transaction context. When these administrative tasks are completed, the received message is passed to the OnMessage () method. Once the method is complete, the transaction is confirmed or returned, and the beans are again returned to the buffer pool.
Ejbremove () is called when the message-driven beans are removed from any storage. and make clear operation and garbage collection. The resources used by instances of all beans must be released in the Ejbremove () method.
The Setmessagedrivenconnection () method has only one instance of a parameter-javax.ejb.messagedrivencontext. The Messagedrivencontext class is similar to the context in the entity and session beans. When an instance of a bean is created, the container passes in the context of the bean. The method of obtaining environmental information in the context and the JTA Usertranscation class are used in the processing of bean management affairs.
In addition, the bean provider must provide a ejbcreate (no parameters) method for setting the beans on the EJB server. Bean instance you can get any resource you need in the Ejbcreate () method.
Message driven bean greatly simplifies creating a JMS consumer, creating and configuring a JMS message consumer these features are left to the EJB container. Developers simply implement the message-driven bean interface, configured to the EJB server, to create a business logic component to receive messages.
Four. An example
To illustrate the above concepts, this article has written a message-driven bean, a publisher, and a subscriber code.
Let's talk about how to run the instance. This assumes that the reader has downloaded the Java EE SDK 1.3 bate from the Sun home page and has already been installed.
1. Create a Weatherreport
Theme (Topic):
j2eeadmin -addJmsDestination WeatherReport topic
You can use the following command to see if the creation is correct:
D:\j2sdkee1.3\bin>j2eeadmin-listjmsdestination jmsdestination
The general display results are as follows:
< JMS Destination : jms/Topic , javax.jms.Topic >
< JMS Destination : jms/Queue , javax.jms.Queue >
< JMS Destination : WeatherReport , javax.jms.Topic >