EJB2.0 JMS

zhaozj2021-02-17  55

EJB2.0 JMS from http://www.csuu.com

MessageDriveNbean In EJB 2.0, a basic change to the specification has added a new enterprise-class bean type, namely MESSAGEDRIVENBEAN. MessageDriveNbean is designed to process JMS messages in the network. For many developers, JMS is a new example, so this article will take some time to gradually explain the understanding of JMS, and their usage in EJB 2.0.

What is JMS? JMS is an API that is unrelated to the vendor to access the message transceiver system. It is similar to JDBC (Java Database Connectivity): Here, JDBC is an API that can be used to access many different relational databases, while JMS provides access methods that are also unrelated to vendors to access messaging services. Many vendors currently support JMS, including IBM's MQSeries, BEA's WebLogic JMS Service and Progress SonicMQ, which is just a few examples.

JMS enables you to send messages from a JMS client from a JMS client through a message transceiver service (sometimes called message intermediary program or router). The message is a type object in JMS, consisting of two parts: header and message main body. The header consists of routing information and metadata on the message. The message main body carries the application's data or payload. Depending on the type of payload, you can divide the message into several types, which carry: textMessage, sequential object (ObjectMessage), attribute set (MapMessage), BytesMessage, original StreamMessage, there is a message without payload (Message).

The message transceiver system is asynchronous, that is, the JMS client can send a message without waiting for a response. It can be seen that this is completely different from RPC-based (remote procedure) systems such as reference implementation of EJB 1.1, CORBA and Java RMI. In RPC, a method of the client calls a distributed object on the server. The client is blocked before the method call returns; the client must wait for the method call to end before the next instruction can be performed. In JMS, the client sends a message to a virtual channel (topic or queue), while other JMS clients book or listen to this virtual channel. When the JMS client sends a message, it does not wait for a response. It performs a sending operation and then proceeds to execute the next instruction. The message may eventually forward to a plurality of clients, which do not need to respond.

JMS EJB 2.0 in EJB 2.0 supports JMS integration in two ways: as a BEAN available resources, and as a MessageDriveNbean. When JMS is used as a resource, bean using JMS API is the generator or sender of the message. In this case, Bean sends a message to a virtual channel called a subject or queue. On the other hand, MessageDriveNbean is the user or recipient of the message. It monitors a specific virtual channel (topic or queue) and handles messages sent to this channel. In order to better understand the role of message generator and message users, use sessionBean Beans to send a message using JMS, and then use a new MessageDriveNbean to use the same message.

JMS session beans and entity beans as EJB 2.0 resources are RPC-based components, which are an excellent architecture in order to assemble various transactional components. However, in some cases, the synchronous properties of RPCs will become an obstacle, which is why access to JMS APIs in EJB 1.1 as a resource included. With the context named by JNDI environment, Bean can get a JMS factory and send an asynchronous message to the topic or queue (also obtained from JNDI) without waiting for a response. Below is an example of ShoppingCart Bean, which uses JMS to send ORDER's detailed information to the message transceiver topic. Public class shoppingcartBean implements sessionBean {// Order Details is a sequentially-sequential object that contains all orders. Public ORDERDETAIL ORDERDETAIL;

Public void processorder () {

// Processing the logic of the order begins with this.

// ... After processing the order, send a message to other systems inTILALCONText JNDienc = New InitialContext ();

// Get JMS Factory and Subject Identifier TopicConnectionFactory Factory = JNDIENC.LOOKUP ("Java: Comp / Env / JMS / TopicFactory");

Topic Ordertopic = JNDIENC.LOOKUP ("Java: Comp / Env / JMS / Ordertopic");

// Get a message sent to the publisher TopicConnection con = factory.createTopicConnection (); TopicSession session = con.createTopicSession (false, Session.AUTO_ACKNOWLEDGE); TopicPublisher publisher = session.createPublisher (orderTopic);

// Send an ObjectMessage to the topic (virtual channel) ObjectMessage Message = session.createObjectMessage (); Message.SetObject (ORDERDETAIL); publisher.publish; con.close ();} ...}

In this case, JMS is used to notify additional applications, and the order has been processed. These additional applications are not important for handling orders, but they will benefit because of a notification that has been processed. Such examples include automatically adjusting the inventory system, and can add customers to the sales app in the product catalog mailing list.

Use JMS to enable the bean to publish (send) messages without blocking. Bean does not know who will receive a message because it is sent to a topic (virtual channel) instead of sending it directly to another application. The application can choose to book this topic and receive notifications for new orders. This is possible to dynamically add or delete applications in a virtual channel, resulting in a more flexible system.

Applications for booking order topics will receive notifications for new orders, and applications can use them to handle this notification anywhere. Applications for various topics or applications that receive messages from each queue can be Java applications, EAI systems (for integrated legacy systems and ERP systems) or MessageDriveNbean components, all of them are considered in JMS terms. Is a JMS client. JMS and MessageDriveNbean Although most JMS vendors provide a message intermediary tool to route messages from the sender to the recipient, the JMS client that builds (receiving) messages is the responsibility of the application developer. In many cases, the application that receives the message must be strong, safe, and quickly and retractable; it needs to be the same as the EJB application.

Since this needs, EJB 2.0 now includes the MessageDriveNbean type, which can use JMS messages, and handle these messages in the same robust, component-based infrastructure, which is very good for session beans. it works. MessageDriveNbean Type (Message Bean) is an enterprise-class bean component that is designed to use asynchronous JMS messages.

In addition to providing the container infrastructure, EJB has another important advantage: concurrency processing. In EJB, a deployed message bean represents a single message user, but this bean itself provides services by many bean instances. Each bean instance can use the message to receive the message. This means that the message bean does not have to use a message as a regular JMS client. The message bean can use the received multiple messages in concurrently, which can achieve much more throughput and much more scalability than the traditional JMS application.

To illustrate the role of the message bean, it has developed a MarketingBean class and deploy it from the order topic to the usage. MarketingBean extracts the ORDERDETAIL object from the message and uses it to add the customer to the appropriate directory mailing list. This is the most exquisite massive mailing system.

Below is the definition of the MarketingBean class, this class uses a message published to the order topic.

Public class marketingbean imports javax.ejb.MessageDriveNbean {

Public void onMessage (Message message) {

ObjectMessage OrderMessage = (ObjectMessage) OrderMessage: ORDERDETAIL ORDERDETAIL = (OrderDetail) OrderMessage.GetObject ();

Integer Customerid = OrderDetail.getCustomerid ();

InitialContext jndienc = new initialContext (); cataloghome catalogHOME = (CatalogHome) JNDienc.lookup ("Java: Comp / ENV / EJB / CATALOG");

Iterator productIDs = orderDetail.getProductsIDs (); while (productIDs.hasNext ()) {Integer productID = (Integer) productIDs.next (); Catalog cat = CatalogHome.findByProductID (productID); cat.addCustomerToMailingList (customerID);}}} Just like a session bean and entity bean, MessageDriveNbean is also a complete enterprise-class bean, but there are still some important differences. The message bean does not have a remote interface or a local interface. This is because the message bean is not an RPC component. It does not have a business method for EJB client calls. Message bean listens to virtual message channels (topic or queue) and send messages sent to this channel using other JMS clients.

Each message bean constitutes a bean class, which implements the MessageDriveNbean interface and an XML deployment descriptor. Below is the definition of the MessageDriveNbean interface, all messages beans must implement this interface.

Package javax.ejb; import javax.jms.message; import javax.jms.MESSAGELISTENER;

public interface MessageDrivenBean extends MessageListener {public void onMessage (Message message); public void ejbCreate (); public void ejbRemove (); public void setMessageDrivenContext (MessageDrivenContext mdc);}

When a message-driven bean is deployed, it is assigned to handle messages in a specific topic or queue. Any message sent by the JMS client (Java application, bean or local client) will forward the message router to the message bean, which is assigned to receive messages from the virtual channel. When a message is sent to a message bean, the EJB container selects an instance of the bean from a pool to handle this message. When the onMessage () method is called when the bean instance is called, it will receive this message in any way it thinks is appropriate. Once this message is used, the message is not transmitted to any other instance of this message bean as long as the transaction is not abnormal.

The message bean is similar to the stateless session bean in a certain point, that is, the two beans do not maintain any status between the two requests. Therefore, the message-driven bean is stateless, but just like a stateless session bean, they can also have instance variables, which are maintained during the entire survival of this bean instance.

The last point of the message bean is that it is important to understand that the message used by the bean is not necessarily generated by other beans. Message beans can use messages in any topic or queue provided by Vendors that meet JMS. Messages used by the message can be from other beans (session beans, entity beans, beans), non-EJB Java applications, or even non-Java applications (if their vendor meets JMS). For example, legacy applications may use IBM's MQSeries to send messages to queues, and the message can be used by other legacy applications, can also be used by message beans. Conclusion Compared to previous specifications, ENTERPRISE JAVABeans 2.0 made some considerable changes. The new CMP model is much flexible than the previous model, which allows various entities to establish models for complex object maps, while providing greater portability of the container. It is urgent to look forward to defining a common query language for finding and selecting operations, and it will also help improve portability.

This new MessageDriveNbean type will help make this powerful message to become an important focus, just like EJB. Messages are an extremely important part of the distributed hybrid calculation, which includes a proof of its importance within EJB.

转载请注明原文地址:https://www.9cbs.com/read-30851.html

New Post(0)