Asynchronous business method on a session bean w/out MDB

Asynchronous business method on a session bean w/out MDB

Is there any way to call a business method on a session bean (Stateless) in an asynchronous manner (without using a MDB)?

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to keep you informed on recent service-oriented architecture (SOA) and SOA-related topics such as integration, governance, Web services, Cloud and more.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchSOA.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchSOA.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

You can create your own asynchronous, JMS-enabled, message listener/receiver and make calls to the session bean from its onMessage callback, as in the following example:
import javax.jms.*;

public class MyAsyncReceiver
{
  public MyAsyncReceiver()
  {
    String queueName = "myqueue";

    try {
      Context jndiContext = new InitialContext();
      QueueConnectionFactory queueConnectionFactory =
         (QueueConnectionFactory)
            jndiContext.lookup("QueueConnectionFactory");
      QueueConnection queueConnection =
         queueConnectionFactory.createQueueConnection();
      QueueSession queueSession =
         queueConnection.createQueueSession(false,
                               Session.AUTO_ACKNOWLEDGE);
      Queue queue = (Queue)jndiContext.lookup(queueName);
      QueueReceiver queueReceiver =
         queueSession.createReceiver(queue);
      queueReceiver.setMessageListener(new MyMsgListener());

      queueConnection.start();

      // wait code should be inserted here
    } catch (JMSException e) {
      System.err.println("Exception: " + e.toString());
    } finally {
      if (queueConnection != null) {
        try {
          queueConnection.close();
        } catch (JMSException e) {
           System.err.println("Exception: " + e.toString());
        }
      }
    }
  }
}

class MyMsgListener implements MessageListener
{
  private MyEJB myEJB = null;

  public void onMessage(Message message)
  {
     System.out.println("Message received");
     callEJB();
  }

  private void callEJB()
  {
     try {
        if (myEJB == null) {
           Context initial = new InitialContext();
           Object objref =
              initial.lookup("java:comp/env/ejb/MyEJB");
           MyEJBHome home =
              (MyEJBHome)PortableRemoteObject.narrow(objref,
                                           MyEJBHome.class);
           myEJB = home.create();
        }
        // Call the EJB's business method
        myEJB.retrieveMessage();
     } catch (Exception e) {
        System.err.println("Exception: " + e.toString());
     }
  }
}

This was first published in November 2003