001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cts.jms;
023:
024: import javax.jms.QueueConnectionFactory;
025: import javax.jms.QueueConnection;
026: import javax.jms.QueueSession;
027: import javax.jms.QueueSender;
028: import javax.jms.TextMessage;
029: import javax.jms.Queue;
030: import javax.jms.JMSException;
031: import javax.jms.Session;
032: import javax.naming.Context;
033: import javax.naming.NamingException;
034: import javax.naming.InitialContext;
035:
036: import org.apache.log4j.Logger;
037:
038: public class MsgSender {
039: Logger log = Logger.getLogger(getClass());
040:
041: public final static String JMS_FACTORY = "ConnectionFactory";
042: public final static String QUEUE = "queue/testQueue";
043:
044: private QueueConnectionFactory qconFactory;
045: private QueueConnection qcon;
046: private QueueSession qsession;
047: private QueueSender qsender;
048: private TextMessage msg;
049: private Queue queue;
050:
051: public MsgSender() {
052: }
053:
054: /**
055: * Create all the necessary objects for receiving messages from a JMS queue.
056: */
057: public void init(Context ctx, String queueName)
058: throws NamingException, JMSException {
059: qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
060: qcon = qconFactory.createQueueConnection();
061: qsession = qcon.createQueueSession(false,
062: Session.AUTO_ACKNOWLEDGE);
063: try {
064: queue = (Queue) ctx.lookup(queueName);
065: } catch (NamingException ne) {
066: queue = qsession.createQueue(queueName);
067: ctx.bind(queueName, queue);
068: }
069: qcon.start();
070: }
071:
072: /**
073: * Close JMS objects.
074: */
075: public void close() throws JMSException {
076: if (qcon != null) {
077: qsender.close();
078: qsession.close();
079: qcon.close();
080: qcon = null;
081: }
082: }
083:
084: public void sendMsg(String message) {
085: try {
086: init(new InitialContext(), QUEUE);
087: log.debug("Sending a message..");
088: qsender = qsession.createSender(queue);
089: msg = qsession.createTextMessage();
090: msg.setText(message);
091: qsender.send(msg);
092: close();
093: } catch (Exception ex) {
094: ex.printStackTrace();
095: }
096: }
097:
098: private static InitialContext getInitialContext(String url)
099: throws NamingException {
100: //Hashtable env = new Hashtable();
101: //env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
102: //env.put(Context.PROVIDER_URL, url);
103: //return new InitialContext(env);
104: return new InitialContext();
105: }
106:
107: }
|