KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > strictpool > StrictlyPooledMDB


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.ejb3.test.strictpool;
23
24 import javax.annotation.Resource;
25 import javax.ejb.ActivationConfigProperty JavaDoc;
26 import javax.ejb.EJBException JavaDoc;
27 import javax.ejb.MessageDriven JavaDoc;
28 import javax.ejb.MessageDrivenContext JavaDoc;
29 import javax.jms.JMSException JavaDoc;
30 import javax.jms.Message JavaDoc;
31 import javax.jms.MessageListener JavaDoc;
32 import javax.jms.Queue JavaDoc;
33 import javax.jms.QueueConnection JavaDoc;
34 import javax.jms.QueueConnectionFactory JavaDoc;
35 import javax.jms.QueueSender JavaDoc;
36 import javax.jms.QueueSession JavaDoc;
37 import javax.jms.TextMessage JavaDoc;
38 import javax.naming.InitialContext JavaDoc;
39
40 import org.jboss.annotation.ejb.PoolClass;
41 import org.jboss.annotation.ejb.PoolClass;
42
43 /**
44  * Adapted from the EJB 2.1 tests (org.jboss.test.cts.ejb.StrictlyPooledMDB)
45  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
46  * @version $Revision: 46471 $
47  */

48 @MessageDriven JavaDoc(activationConfig =
49         {
50         @ActivationConfigProperty JavaDoc(propertyName="destinationType", propertyValue="javax.jms.Queue"),
51         @ActivationConfigProperty JavaDoc(propertyName="destination", propertyValue="queue/queueA"),
52         @ActivationConfigProperty JavaDoc(propertyName="maxMessages", propertyValue="10"),
53         @ActivationConfigProperty JavaDoc(propertyName="minSession", propertyValue="10"),
54         @ActivationConfigProperty JavaDoc(propertyName="maxSession", propertyValue="10")
55         })
56 @PoolClass (value=org.jboss.ejb3.StrictMaxPool.class, maxSize=StrictlyPooledMDB.maxActiveCount, timeout=10000)
57 public class StrictlyPooledMDB implements MessageListener JavaDoc
58 {
59    /** The class wide max count of instances allows */
60    public static final int maxActiveCount = 2;
61    /** The class wide count of instances active in business code */
62    private static int activeCount;
63
64    private MessageDrivenContext JavaDoc ctx = null;
65    private QueueConnection JavaDoc queConn;
66    private QueueSession JavaDoc session;
67    private QueueSender JavaDoc sender;
68
69    private static synchronized int incActiveCount()
70    {
71       return activeCount ++;
72    }
73    private static synchronized int decActiveCount()
74    {
75       return activeCount --;
76    }
77
78    @Resource public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx)
79       throws EJBException JavaDoc
80    {
81       System.out.println("setMessageDrivenContext()");
82       this.ctx = ctx;
83       try
84       {
85          InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
86          QueueConnectionFactory JavaDoc factory = (QueueConnectionFactory JavaDoc) iniCtx.lookup("java:/ConnectionFactory");
87          queConn = factory.createQueueConnection();
88          session = queConn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
89          Queue JavaDoc queue = (Queue JavaDoc) iniCtx.lookup("queue/queueB");
90          sender = session.createSender(queue);
91       }
92       catch(Exception JavaDoc e)
93       {
94          System.out.println("Setup failure");
95          e.printStackTrace();
96          throw new EJBException JavaDoc("Setup failure", e);
97       }
98    }
99
100    public void ejbCreate()
101    {
102    }
103
104    public void ejbRemove()
105    {
106       try
107       {
108          if( sender != null )
109             sender.close();
110          if( session != null )
111             session.close();
112          if( queConn != null )
113             queConn.close();
114       }
115       catch(Exception JavaDoc e)
116       {
117          System.out.println("Failed to close JMS resources");
118          e.printStackTrace();
119       }
120    }
121
122    public void onMessage(Message JavaDoc message)
123    {
124       int count = incActiveCount();
125       System.out.println("Begin onMessage, activeCount="+count+", ctx="+ctx);
126       try
127       {
128          Message JavaDoc reply = null;
129          if( count > maxActiveCount )
130          {
131             String JavaDoc msg = "IllegalState, activeCount > maxActiveCount, "
132                   + count + " > " + maxActiveCount;
133             // Send an exception
134
Exception JavaDoc e = new IllegalStateException JavaDoc(msg);
135             reply = session.createObjectMessage(e);
136          }
137          else
138          {
139             TextMessage JavaDoc tm = (TextMessage JavaDoc) message;
140             // Send an ack
141
reply = session.createTextMessage("Recevied msg="+tm.getText());
142          }
143          Thread.currentThread().sleep(1000);
144          sender.send(reply);
145       }
146       catch(JMSException JavaDoc e)
147       {
148          System.out.println("Failed to send error message");
149          e.printStackTrace();
150       }
151       catch(InterruptedException JavaDoc e)
152       {
153       }
154       finally
155       {
156          count = decActiveCount();
157          System.out.println("End onMessage, activeCount="+count+", ctx="+ctx);
158       }
159    }
160 }
161
Popular Tags