Many complex enterprise applications use a combination of many J2EE technologies, including EJBs and JMS. Using a JMS system, you can trigger back-end business logic in a batch process, from remote clients, or without user interaction. Invoking an EJB with a JMS message requires creating a message-driven EJB and setting up the correct JMS message destination in the application server running the EJB application, and creating a message-driven bean to receive JMS messages in order to start business logic methods. For example, the EJB class in the sample below defines a message-driven EJB. Notice that it extends a specific EJB interface, and also implements the JMS MessageListener interface.

import javax.ejb.*;
import javax.jms.*;
import javax.naming.*;

public class MessageBean implements MessageDrivenBean, MessageListener {
  private MessageDrivenContext _context;
  // EJB life-cycle methods:
  public void ejbRemove() { }
  public void ejbPassivate() { }
  public void ejbCreate() throws CreateException {}

  public void setMessageDrivenContext(
    MessageDrivenContext context) {
    _context = ctx;

  // receives JMS messages:
  public void onMessage(Message message) {
    try {
      String command = ((TextMessagemessage).getText();
      // perform or delegate business logic
    catch (JMSException ex) {