Ejb mdb (Message-driven bean) III-MDB class and client code specifications

Source: Internet
Author: User

VII. themessage-driven Bean class

A message drivenbean must be annotated with the messagedriven annotation or denoted in the deploymentdescriptor as a message-driven bean. The Bean class need not implement the javax. EJB. messagedrivenbeaninterface

The class mustbe defined as public.

The class cannotbe defined as abstract or final.

It must containa public constructor with no arguments.

It must notdefine the Finalize method.

It isrecommended, but not required, that a message-driven Bean class implement themessage listener interface for the message type it supports. A bean thatsupports the jms api implements the javax. JMS. messagelistener interface.

Unlike sessionbeans and entities, message-driven beans do not have the remote or localinterfaces that define client access. client components do not locatemessage-driven beans and invoke methods on them. although message-driven beansdo not have business
Methods, they may contain helper methods that are invokedinternally by the onmessage method.

For example server, the @ messagedriven annotation typically contains a mappednameelement that specifies the JNDI name of the destination from which the beanwill consume messages. For complex message-driven beans, there can also be anactivationconfig
Element containing @ activationconfigproperty annotations usedby the bean.

A message-drivenbean can also inject a messagedrivencontext resource. Commonly you use thisresource to call the setrollbackonly method to handle exceptions for a beanthat uses container-managed transactions.

Therefore, thefirst few lines of the simplemessagebean class look like this:

 

@ Messagedriven (mappedname = "JMS/queue", activationconfig = {

@ Activationconfigproperty (propertyname = "acknowledgemode ",

Propertyvalue = "auto-acknowledge "),

@ Activationconfigproperty (propertyname = "destinationtype ",

Propertyvalue = "javax. JMS. queue ")

})

Public class simplemessagebean implementsmessagelistener {

@ Resource

Private messagedrivencontext MDC;

...

Property name

Description

Acknowledgemode

Acknowledgment mode; see controlling Message Acknowledgment for information

Destinationtype

EitherJavax. JMS. QueueOrJavax. JMS. Topic

Subscriptiondurability

For durable subscribers, setDurable; See creating durable subscriptionsfor Information

Clientid

For durable subscribers, the client ID for the connection

Subscriptionname

For durable subscribers, the name of the subscribers

Messageselector

A string that filters messages; see JMS message selectors for information, and see
Application that uses the jms api with a session bean for an example

Addresslist

Remote system or systems to communicate with; see an application example that consumes messages from a remote server for an example

 

The onmessage Method

When the queuereceives a message, the EJB container invokes the message listener method ormethods. For a bean that uses JMS, This Is The onmessage method of themessagelistener interface.

A messagelistener method must follow these rules:

The method mustbe declared as public.

The method mustnot be declared as final or static.

The onmessagemethod is called by the bean's container when a message has arrived for thebean to service. this method contains the business logic that handles the processingof the message. it is the message-driven Bean's responsibility to parse themessage
And perform the necessary business logic.

The onmessagemethod has a single argument: the incoming message.

The signature ofthe onmessage method must follow these rules:

The return typemust be void.

The method musthave a single argument of Type javax. JMS. Message.

In thesimplemessagebean class, the onmessage method casts the incoming message to atextmessage and displays the text:

 

Public void onmessage (Message inmessage ){

Textmessage MSG = NULL;

 

Try {

If (inmessage instanceof textmessage ){

MSG = (textmessage) inmessage;

Logger.info ("message Bean: Message received:" +

MSG. gettext ());

} Else {

Logger. Warning ("message of wrongtype:" +

Inmessage. getclass (). getname ());

}

} Catch (jmsexception e ){

E. printstacktrace ();

MDC. setrollbackonly ();

} Catch (throwable Te ){

Te. printstacktrace ();

}

}

8. Client

@ Resource (mappedname = "JMS/connectionfactory ")

Private Static connectionfactoryconnectionfactory;

 

@ Resource (mappedname = "JMS/queue ")

Private Static queue;

 

// Next, the client creates the connection, session, and message producer:

Connection = connectionfactory. createconnection ();

Session = connection. createsession (false, session. auto_acknowledge );

Messageproducer = session. createproducer (Queue );

 

// Finally, the client sends severalmessages to the queue:

Message = session. createtextmessage ();

For (INT I = 0; I <num_msgs; I ++ ){

Message. settext ("this is message" + (I + 1 ));

System. Out. println ("sending message:" + message. gettext ());

Messageproducer. Send (Message );

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.