KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > jms > managed > JMSTestBean


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.jms.managed;
23
24 import javax.ejb.EJBException JavaDoc;
25 import javax.ejb.SessionContext JavaDoc;
26 import javax.jms.Connection JavaDoc;
27 import javax.jms.ConnectionFactory JavaDoc;
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.MessageProducer JavaDoc;
30 import javax.jms.Queue JavaDoc;
31 import javax.jms.Session JavaDoc;
32 import javax.jms.TextMessage JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34
35 import javax.ejb.Remove JavaDoc;
36 import javax.ejb.Init JavaDoc;
37
38 import org.jboss.ejb3.Container;
39 import org.jboss.logging.Logger;
40
41 public class JMSTestBean
42    implements JMSTest
43 {
44    
45    private static Logger log = Logger.getLogger(JMSTestBean.class);
46    
47    private Connection JavaDoc connection;
48    
49    private Queue JavaDoc queue;
50    
51    private SessionContext JavaDoc ctx;
52    
53    public void setSessionContext(SessionContext JavaDoc sc)
54    {
55       ctx = sc;
56    }
57    
58    @Init JavaDoc
59    public void ejbCreate()
60    {
61       try
62       {
63          InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
64  
65          ConnectionFactory JavaDoc cf =
66             (ConnectionFactory JavaDoc) iniCtx.lookup(Container.ENC_CTX_NAME + "/env/jms/MyConnectionFactory");
67          
68          connection = cf.createConnection();
69          connection.start();
70          queue = (Queue JavaDoc)iniCtx.lookup(Container.ENC_CTX_NAME + "/env/jms/MyQueue");
71       }
72       catch (Throwable JavaDoc t)
73       {
74          log.error(t);
75       }
76    }
77    
78    
79    public void test1() throws EJBException JavaDoc
80    {
81       Session JavaDoc session = null;
82       
83       try
84       {
85          session =
86             connection.createSession(true, 0);
87          
88          MessageProducer JavaDoc producer = session.createProducer(queue);
89          
90          TextMessage JavaDoc message = session.createTextMessage();
91          
92          message.setText("Testing 123");
93          
94          producer.send(message);
95       }
96       catch (JMSException JavaDoc e)
97       {
98          log.error(e);
99          ctx.setRollbackOnly();
100          throw new EJBException JavaDoc(e.toString());
101       }
102       finally
103       {
104          try
105          {
106             if (session != null) session.close();
107          }
108          catch (JMSException JavaDoc e)
109          {
110          }
111       }
112    }
113    
114    @Remove JavaDoc
115    public void remove()
116    {
117       
118       try
119       {
120          if (connection != null)
121             connection.close();
122       }
123       catch (Exception JavaDoc e)
124       {
125          e.printStackTrace();
126       }
127       
128    }
129 }
130
Popular Tags