Q

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)?
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 last published in November 2003

Dig Deeper on Java Web Services

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchSoftwareQuality

SearchCloudApplications

SearchAWS

TheServerSide

SearchWinDevelopment

Close