Free Data Structures and Algorithms Course

Subscribe below and get all best seller courses for free !!!


Subscribe to all free courses

"Online Tweeter Enterprise Application" : Creating a Message-Driven Bean in NetBeans EJB module - Part 4

Lets continue building "Online Tweeter Enterprise Application" in NetBeans. In this section of tutorial, you will create a Message-Driven Bean by name "". A Message-Driven Bean is a enterprise bean that acts as a JMS message listener which process message coming on the queue or topic over which it is bound to listen.
In our "Online Tweeter Enterprise Application" it basically processes and stores the tweets sent by user from the servlet to the queue in database.

Step 1: Open "tweeter-ejb" project and right click Source Packages and then select New and than Other as shown in fig below:

Step 2: On clicking Other a dialog box appears by name New File. In the Categories: list select Enterprise JavaBeans and in the File Types: select Message-Driven Bean as shown in fig below.

Step 3: Click

New Message-Driven Bean dialog box gets open. It prompts us to enter values for EJB Name: , Project: , Location: , Package: , Project: , Destinations:. Enter the values as per fig below or enter what suits you.

Step 4: Click Add button to add a valid message destination. Add Message Destination dialog box gets open. Enter values for Destination Name: and Destination Type: as shown in fig below.

Step 5: Click

Previously opened New Message-Driven Bean dialog box gets open with the values entered for the Project Destinations: as shown in fig below.

Step 6:  After providing Project Destinations: click


A new Message-Driven Bean gets created by name "" in the "tweeter-ejb" module in the package provided at the time of creating the bean. It has most of the source code already generated by NetBeans. Kindly add or remove additional code provided below.

package com.hubberspot.ejb;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

// @MessageDriven annotation provides information to the container
// that this java class is a Message-Driven Bean. It has few 
// additional info about JNDI name of the destination to which
// this bean is listening.

// @ActivationConfigProperty annotation informs container about 
// the type of destination it is listening and the acknowledge
// mode.

// Message-Driven Bean must implement MessageListener which has a
// method onMessage() which gets triggered as soon as any message
// arrives over the queue.

@MessageDriven(mappedName = "jms/tweets", activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", 
        propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", 
        propertyValue = "javax.jms.Queue")
public class ReceiveTweets implements MessageListener {
    // @Resource annotation injects the dependency for the resource
    // we intent to use. 
    MessageDrivenContext receiveTweetsContext;
    // @PersistenceContext injects dependency for the EntityManager
    // by loading persistence.xml
    // EntityManager provides an interface for database persistence
    // such as to persist, merge , load and query objects.
    EntityManager tweetsManager;
    public ReceiveTweets() {
    // The Object Message in the form of tweet entered
    // by the user in a Servlet is been persisted to 
    // database by EntityManager's persist() method.
    public void onMessage(Message message) {
        ObjectMessage tweetMessage = null;
        if(message instanceof ObjectMessage) {
            try {
                tweetMessage = (ObjectMessage) message;
                Tweet tweet = (Tweet) tweetMessage.getObject();
            } catch (JMSException ex) {
                        .log(Level.SEVERE, null, ex);

In the next section of this blog (part 5) you will learn how to create a Stateless Session Bean in NetBeans for this application.

© 2021 Learn Java by Examples Template by Hubberspot