KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tutorial > interceptor > bean > AccountsConfirmInterceptor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.tutorial.interceptor.bean;
23
24 import java.util.Date JavaDoc;
25
26 import javax.annotation.Resource;
27 import javax.interceptor.AroundInvoke;
28 import javax.interceptor.InvocationContext;
29 import javax.jms.JMSException JavaDoc;
30 import javax.jms.Message JavaDoc;
31 import javax.jms.Queue JavaDoc;
32 import javax.jms.QueueConnection JavaDoc;
33 import javax.jms.QueueConnectionFactory JavaDoc;
34 import javax.jms.QueueSender JavaDoc;
35 import javax.jms.QueueSession JavaDoc;
36 import javax.jms.Session JavaDoc;
37 import javax.persistence.EntityManager;
38 import javax.persistence.PersistenceContext;
39
40 /**
41  *
42  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
43  * @version $Revision: 44679 $
44  */

45 public class AccountsConfirmInterceptor extends AccountsInterceptor
46 {
47    @Resource(mappedName="java:ConnectionFactory")
48    QueueConnectionFactory JavaDoc cf;
49
50    @Resource(mappedName="queue/tutorial/accounts")
51    Queue JavaDoc queue;
52
53    @PersistenceContext
54    EntityManager em;
55
56    QueueConnection JavaDoc conn;
57
58    public Object JavaDoc intercept(InvocationContext ctx) throws Exception JavaDoc
59    {
60       //overrides AccountsInterceptor.intercept() so that will not be invoked
61
return null;
62    }
63
64
65    @AroundInvoke
66    public Object JavaDoc sendConfirmMessage(InvocationContext ctx) throws Exception JavaDoc
67    {
68       QueueSession JavaDoc session = null;
69       try
70       {
71          System.out.println("*** AccountsConfirmInterceptor intercepting");
72
73          long orderId = (Long JavaDoc)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 JavaDoc());
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 JavaDoc msg = session.createTextMessage("Confirming order " + orderId);
90          QueueSender JavaDoc sender = session.createSender(queue);
91          sender.send(msg);
92
93          return ctx.proceed();
94       }
95       catch(Exception JavaDoc e)
96       {
97          throw new RuntimeException JavaDoc(e);
98       }
99       finally
100       {
101          try{session.close();}catch(Exception JavaDoc e) {}
102          System.out.println("*** AccountsConfirmInterceptor exiting");
103       }
104    }
105
106    QueueConnection JavaDoc getConnection() throws JMSException JavaDoc
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