Sunday 28 February 2016

OCEJBCD -7.Developing Message-Driven Beans

OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification

7.1. Understand the short-comings of using session beans as messaging consumers

Using a session bean as consumer has the problem of blocking server resources, because it will be a synchronous process (instead of asynchronous processing with message-driven bean ).

Using session bean receive() method will block the thread indefinitely until the message becomes available :

Message m = consumer.receive()

How to handle the blocking synchronous problem with session bean? Using a timed synchronous receive.

A parameter can be used to indicate the time waiting the message to arrive:
Message m = consumer.receive(1); //time in milliseconds
Message m = consumer.receiveNoWait();

See an example of Writing the Clients for the Synchronous Receive.

7.2. Describe the properties and life cycle of message-driven beans




7.3. Create a JMS message-driven bean


To create a message-driven bean the bean class:
  • is annotated with @MessageDriven
    • @ActivationConfigProperty Settings(*):
      • Acknownledge_Mode
        • Auto-Acknownledge
        • Client-Acknownledge
        • Dups-ok-aknownledge
      • DestinationType
        • Queue
        • Topic
      • Subscription durability
        • Durable
        • Non-durable
      • ClientId
      • SusbscriptionName
      • MessageSelector 
      • AddressList
  • implements MessageListener inteface
  • overrides onMessage() method

(*)Example of @ActivationConfigProperty configuration:
@MessageDriven(mappedName = "jms/Topic", activationConfig =  {
    @ActivationConfigProperty(propertyName = "messageSelector",
            propertyValue = "NewsType = 'Sports' ")
    , @ActivationConfigProperty(propertyName = "subscriptionDurability",
            propertyValue = "Durable")
    , @ActivationConfigProperty(propertyName = "clientId",
            propertyValue = "MyID")
    , @ActivationConfigProperty(propertyName = "subscriptionName",
            propertyValue = "MySub")
    })

There is an example of a message-driven bean class at the last section of this post.

7.4. Create life cycle event handlers for a JMS message-driven bean

See this post : Configuring a JMS environment, to know how to configure a server and include jms resources as connection factory and destination

Once the connection factory and the destination(Queue or topic) have been created, we can use dependency injection to use them in our message beans
like:
  • Connection factory
@Resource(name="jms/MyConnectionFactory")
private QueueConnectionFactory connectionFactory;
  • Destination
@Resource(name="jms/MyQueue")

private Queue queue;

With the connectionFactory we can define the context to create the producer and the consumer.
  • Connection and Session
Connection con = connectionFactory.createConnection();
Session session = con.createSession(true,0);
  • MessageProducer
MessageProducer producer = session.createProducer(topic);
  • Message types(TextMessage, MapMessage, ObjectMessage and others).
TextMessage message = session.createTextMessage();


7.5. Configure a JMS message-driven bean

Here is an example of using a message-driven bean:

  • A client where the user request to send a message (with a jsp page and a servlet, in the same way as examples described in previous posts : calling a session bean from a servlet).
  • A session bean (stateless) that will define a producer that put the message in the JMS queue.
  • A message-driven bean that will consume the message from the queue.


Session bean:

Message bean:

When executing the application in our configured server:
Write a message in the textarea and submit the message.
In the log can be read the producer sending the message and the consumer receiving it:
producer sends the message...
consumer receives message...

OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification

No comments:

Post a Comment