KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @version <tt>$Revision: 46471 $</tt>
45  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
46  */

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