Message BEB-based EJB Asynchronous Communication Bromon Original Remarks Copyright
Asynchronous communication is a very important means of improving the efficiency of programs, while performance issues are also very concerned about the topic of EJB. The reason for the EJB execution efficiency is that the client needs to query and call the remote interface, and itself in processing data is very fast. After all, EJB has a relatively complete buffer mechanism, and hundreds of thousands of app server is not white. If the client program can use asynchronous communication, it is only responsible for sending requests and data in the form of messages. You don't have to wait for the system query, call the remote interface, save a lot of time, which is an important reason for the introduction of the message driver bean in EJB 2.0. . J2EE's message mechanism allows you to pass objects in the form of a message, we can use it to achieve a lot of applications.
In a test system that is recently written, the system records students in real time in real time in real time, so there will be more frequent database operations, which have a relatively large impact on performance, but can make each time for students. The answers have been permanently saved, avoiding the loss of the answers submitted by accidents due to accidental cranes or abnormal exits. There is no doubt that the security of the data is the first, but hundreds of students concurrently operated, this load is also very huge. In order to make a balance in stability and performance, we consider such a model:
◆ Create a permanent queue on the server side, which guarantees data security, even if the server is unexpected, the object is not lost. And there is a mechanism to ensure that the subjects in the queue will only be processed once.
◆ The client encapsulates the answer data into JavaBean, then submit it into this queue, and does not have to wait for the data processing to complete. Directly perform the future operation, continue to send the new data, do not have to care about the data will be deal with.
◆ The server handles the data packets in the queue in the order of FIFO.
The structure is approximately as shown below:
?
Obviously, only one one-way communication is provided here, but it is not forced to completely set another queue, store returned messages, and the corresponding message is associated by the message ID. The Java Message Service (JMS) based on the message bean is actually such a framework.
Firstly design a class for an operation answering data sheet:
?
It contains methods of increasing the answer and modifying an existing answer. In the actual item, it is a typical session facade schema by a mapping data table CMP Entity Bean and a package program logic. It is a typical session facade mode. The actual system is roughly like this:
In a system, use CMP and Hibernate to make mappings, is a very weird design, which is just to make the system more research and discuss the value, and I am afraid I have to carefully consider whether I use BMP instead of hibernate.
Before writing a message bean, what is the object to be passed in the message:
?
Here is the code of the message bean:
/ *? * Message bean, processing the ANSWER operation? * Created on 2004-7-27? * / Package org.bromon.examer.Message;
Import javax.ejb.messagedrivenbean; import javax.jms. *; import org.bromon.examer.base. *; import org.bromon.examer.Session. *;
public class AnswerMessageBean implements MessageDrivenBean, MessageListener {private javax.ejb.MessageDrivenContext messageContext = null;?? // define the context public void setMessageDrivenContext (?? javax.ejb.MessageDrivenContext messageContext) ?? throws javax.ejb.EJBException {???? This.MessageContext;?}? public void ejbremove ()? {?? messageContext = null ;?}
? // After receiving the message, this method will be executed? Public void onmessage (javax.jms.Message Message)? {?? try ?? {??? if (Message InstanceOf ObjectMessage) ??? {???? Answeroperate AP = (Answeroperate) (") .GETOBJECT (); ???? // Call EJB to process the AnsweropeRate object ???} ???? system.out.printn (" message processing ");? ?} Catch (Exception E) ?? {??? system.out.println (e); ??}?
}
How do messages guarantees security? There are two means, one is to use message verification, the customer must provide a matching account password to access the data in the message. The second is to encrypt the object in the message, and the customer must hold the corresponding key to get the object.