KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmsra > bean > JMSSessionBean


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.test.jmsra.bean;
23
24 import javax.ejb.*;
25 import javax.jms.*;
26 import javax.naming.*;
27
28 import org.jboss.test.util.ejb.SessionSupport;
29
30 public class JMSSessionBean extends SessionSupport
31 {
32    public void sendToQueueAndTopic()
33    {
34       try
35       {
36          InitialContext ctx = new InitialContext();
37
38          QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup("java:/JmsXA");
39          Queue q = (Queue) ctx.lookup("queue/testQueue");
40          QueueConnection qc = qcf.createQueueConnection();
41          QueueSession qs = null;
42          try
43          {
44             qs = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
45             QueueSender sender = qs.createSender(q);
46             sender.send(qs.createMessage());
47          }
48          finally
49          {
50             if (qs != null)
51                qs.close();
52             if (qc != null)
53                qc.close();
54          }
55
56          TopicConnectionFactory tcf = (TopicConnectionFactory) ctx.lookup("java:/JmsXA");
57          Topic t = (Topic) ctx.lookup("topic/testTopic");
58          TopicConnection tc = tcf.createTopicConnection();
59          TopicSession ts = null;
60          try
61          {
62             ts = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
63             TopicPublisher publisher = ts.createPublisher(t);
64             publisher.publish(ts.createMessage());
65          }
66          finally
67          {
68             if (ts != null)
69                ts.close();
70             if (tc != null)
71                tc.close();
72          }
73       }
74       catch (Exception JavaDoc e)
75       {
76          throw new EJBException(e);
77       }
78    }
79 }
Popular Tags