KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cts > jms > MsgSender


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.cts.jms;
23
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.QueueConnection JavaDoc;
26 import javax.jms.QueueConnectionFactory JavaDoc;
27 import javax.jms.QueueSender JavaDoc;
28 import javax.jms.QueueSession JavaDoc;
29 import javax.jms.Session JavaDoc;
30 import javax.jms.TextMessage JavaDoc;
31 import javax.jms.Queue JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35
36 public class MsgSender
37 {
38    org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass());
39    
40   public final static String JavaDoc JMS_FACTORY="ConnectionFactory";
41   public final static String JavaDoc QUEUE="queue/testQueue";
42
43   private QueueConnectionFactory JavaDoc qconFactory;
44   private QueueConnection JavaDoc qcon;
45   private QueueSession JavaDoc qsession;
46   private QueueSender JavaDoc qsender;
47   private TextMessage JavaDoc msg;
48   private Queue JavaDoc queue;
49
50   public MsgSender ( )
51   {
52   }
53
54   /**
55    * Create all the necessary objects for receiving
56    * messages from a JMS queue.
57    */

58   public void init(Context JavaDoc ctx, String JavaDoc queueName)
59        throws NamingException JavaDoc, JMSException JavaDoc
60   {
61     qconFactory = (QueueConnectionFactory JavaDoc) ctx.lookup(JMS_FACTORY);
62     qcon = qconFactory.createQueueConnection();
63     qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
64     try
65     {
66       queue = (Queue JavaDoc) ctx.lookup(queueName);
67     }
68     catch (NamingException JavaDoc ne)
69     {
70       queue = qsession.createQueue(queueName);
71       ctx.bind(queueName, queue);
72     }
73     qcon.start();
74   }
75
76   /**
77    * Close JMS objects.
78    */

79   public void close()
80        throws JMSException JavaDoc
81   {
82     if( qcon != null ) {
83         qsender.close();
84         qsession.close();
85         qcon.close();
86         qcon = null;
87     }
88   }
89
90   public void sendMsg( String JavaDoc message )
91   {
92     try
93     {
94       init( new InitialContext JavaDoc(), QUEUE);
95       log.debug("Sending a message.." );
96       qsender = qsession.createSender(queue);
97       msg = qsession.createTextMessage();
98       msg.setText(message);
99       qsender.send(msg);
100       close();
101     }
102     catch(Exception JavaDoc ex)
103     {
104     ex.printStackTrace( );
105     }
106   }
107
108   private static InitialContext JavaDoc getInitialContext(String JavaDoc url)
109        throws NamingException JavaDoc
110   {
111       //Hashtable env = new Hashtable();
112
//env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
113
//env.put(Context.PROVIDER_URL, url);
114
//return new InitialContext(env);
115
return new InitialContext JavaDoc();
116   }
117
118 }
119
120
121
122
123
124
125
Popular Tags