EJB annotation-2 (message-driven EJB learning Notes)

Source: Internet
Author: User
Tags message queue

Message-driven Bean
Message-driven Bean (MDB) is a component designed specifically to process message requests. An MDB class must implement the MessageListener interface. When the container detects a message waiting for a bean queue, it calls the onMessage () method to pass the message as a parameter. MDB decides how to process the message in OnMessage. You can use annotations to configure which queue the MDB listens.

Message-driven Bean server


@ MessageDriven (activationConfig = ...{
@ ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax. jms. Queue "),
@ ActivationConfigProperty (propertyName = "destination", propertyValue = "queue/test ")
})

Public class MessageBean implements MessageListener ...{

Public void onMessage (Message msg )...{
Try ...{
TextMessage tmsg = (TextMessage) msg;
// Do your action here
} Catch (Exception e )...{
E. printStackTrace ();
}
}
}
Use the @ MessageDriven annotation to indicate that this is a message-driven Bean and use the @ ActivationConfigProperty annotation to configure the message
The destinationType attribute specifies the message type. There are two types of messages: topics and queues. Below are the two types of messages.
Type introduction:
Topics can have multiple clients. Topic publishing allows one-to-multiple or multiple-to-multiple communication channels. The publisher of the message is called publisher.
The message recipient is subscriber. DestinationType attribute value: javax. jms. Topic.
Queue only allows one message to be sent to one customer. A sender puts a message into the message queue. The receiver extracts and obtains the message from the queue, and the message disappears in the queue. After the first receiver extracts and obtains the message, others cannot obtain it. DestinationType attribute value: javax. jms. Queue
The destination attribute is used to specify the message path. When a message-driven Bean is published, if the path does not exist, the container automatically creates the path.
When it is disabled, the path is automatically deleted.

Message-driven Bean client


QueueConnection cnn = null;
QueueSender sender = null;
QueueSession sess = null;
Queue queue = null;
Try ...{
Properties props = new Properties ();
Props. setProperty ("java. naming. factory. initial", "org. jnp. interfaces. NamingContextFactory ");
Props. setProperty ("java. naming. provider. url", "localhost: 1099 ");
Props. setProperty ("java. naming. factory. url. pkgs", "org. jboss. naming ");
InitialContext ctx = new InitialContext (props );
QueueConnectionFactory factory = (QueueConnectionFactory) ctx. lookup ("ConnectionFactory ");
Cnn = factory. createQueueConnection ();
Sess = cnn. createQueueSession (false, QueueSession. AUTO_ACKNOWLEDGE );
Queue = (Queue) ctx. lookup ("queue/test ");
} Catch (Exception e )...{
Out. println (e. getMessage ());
}
TextMessage msg = sess. createTextMessage ("message-driven Bean Test ");
Sender = sess. createSender (queue );
Sender. send (msg );
Sess. close (); steps
(1) Get a JNDI initialization Context (Context );
(2) search for a connection factory TopicConnectFactory/QueueConnectionFactory based on the context (there are two connection factories, according
Topic/queue to use the corresponding type );
(3) get a connection from the connection factory (Connect has two [TopicConnection/QueueConnection]);
(4) establish a Session through a connection );
(5) search for the destination (Topic/Queue );
(6) create a message producer (TopicPublisher/QueueSender) and a consumer (TopicSubscriber/QueueReceiver) based on the session and destination ).

Refer: EJB3.0 instance tutorial(Author: Li Fuming)

<

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.