1 22 package org.jboss.tutorial.interceptor.bean; 23 24 import java.util.Date ; 25 26 import javax.annotation.Resource; 27 import javax.interceptor.AroundInvoke; 28 import javax.interceptor.InvocationContext; 29 import javax.jms.JMSException ; 30 import javax.jms.Message ; 31 import javax.jms.Queue ; 32 import javax.jms.QueueConnection ; 33 import javax.jms.QueueConnectionFactory ; 34 import javax.jms.QueueSender ; 35 import javax.jms.QueueSession ; 36 import javax.jms.Session ; 37 import javax.persistence.EntityManager; 38 import javax.persistence.PersistenceContext; 39 40 45 public class AccountsConfirmInterceptor extends AccountsInterceptor 46 { 47 @Resource(mappedName="java:ConnectionFactory") 48 QueueConnectionFactory cf; 49 50 @Resource(mappedName="queue/tutorial/accounts") 51 Queue queue; 52 53 @PersistenceContext 54 EntityManager em; 55 56 QueueConnection conn; 57 58 public Object intercept(InvocationContext ctx) throws Exception 59 { 60 return null; 62 } 63 64 65 @AroundInvoke 66 public Object sendConfirmMessage(InvocationContext ctx) throws Exception 67 { 68 QueueSession session = null; 69 try 70 { 71 System.out.println("*** AccountsConfirmInterceptor intercepting"); 72 73 long orderId = (Long )ctx.getParameters()[0]; 74 75 if (em.find(Confirmation.class, orderId) == null) 76 { 77 System.out.println("*** AccountsConfirmInterceptor - recording confirmation"); 78 Confirmation confirmation = new Confirmation(orderId, new Date ()); 79 em.persist(confirmation); 80 } 81 else 82 { 83 System.out.println("*** AccountsConfirmInterceptor - order has already been confirmed aborting"); 84 return null; 85 } 86 87 System.out.println("*** AccountsConfirmInterceptor - notifying accounts dept " + ctx.getMethod().getName()); 88 session = getConnection().createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 89 Message msg = session.createTextMessage("Confirming order " + orderId); 90 QueueSender sender = session.createSender(queue); 91 sender.send(msg); 92 93 return ctx.proceed(); 94 } 95 catch(Exception e) 96 { 97 throw new RuntimeException (e); 98 } 99 finally 100 { 101 try{session.close();}catch(Exception e) {} 102 System.out.println("*** AccountsConfirmInterceptor exiting"); 103 } 104 } 105 106 QueueConnection getConnection() throws JMSException 107 { 108 if (conn == null) 109 { 110 synchronized(cf) 111 { 112 if (conn == null) 113 { 114 conn = cf.createQueueConnection(); 115 } 116 } 117 } 118 119 return conn; 120 } 121 } 122 | Popular Tags |