FileDocCategorySizeDatePackage
AccountsConfirmInterceptor.javaAPI DocJBoss 4.2.13795Fri Jul 13 20:54:52 BST 2007org.jboss.tutorial.interceptor.bean

AccountsConfirmInterceptor

public class AccountsConfirmInterceptor extends AccountsInterceptor
author
Kabir Khan
version
$Revision: 60233 $

Fields Summary
javax.jms.QueueConnectionFactory
cf
javax.jms.Queue
queue
EntityManager
em
javax.jms.QueueConnection
conn
Constructors Summary
Methods Summary
javax.jms.QueueConnectiongetConnection()

      if (conn == null)
      {
         synchronized(cf)
         {
            if (conn == null)
            {
               conn = cf.createQueueConnection();
            }
         }
      }

      return conn;
   
public java.lang.Objectintercept(javax.interceptor.InvocationContext ctx)

      //overrides AccountsInterceptor.intercept() so that will not be invoked
      return null;
   
public java.lang.ObjectsendConfirmMessage(javax.interceptor.InvocationContext ctx)

      QueueSession session = null;
      try
      {
         System.out.println("*** AccountsConfirmInterceptor intercepting");

         long orderId = (Long)ctx.getParameters()[0];

         if (em.find(Confirmation.class, orderId) == null)
         {
            System.out.println("*** AccountsConfirmInterceptor - recording confirmation");
            Confirmation confirmation = new Confirmation(orderId, new Date());
            em.persist(confirmation);
         }
         else
         {
            System.out.println("*** AccountsConfirmInterceptor - order has already been confirmed aborting");
            return null;
         }

         System.out.println("*** AccountsConfirmInterceptor - notifying accounts dept " + ctx.getMethod().getName());
         session = getConnection().createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
         Message msg = session.createTextMessage("Confirming order " + orderId);
         QueueSender sender = session.createSender(queue);
         sender.send(msg);

         return ctx.proceed();
      }
      catch(Exception e)
      {
         throw new RuntimeException(e);
      }
      finally
      {
         try{session.close();}catch(Exception e) {}
         System.out.println("*** AccountsConfirmInterceptor exiting");
      }