/*
 * Enhydra Java Application Server
 * The Initial Developer of the Original Code is Lutris Technologies Inc.
 * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
 * Inc.
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Enhydra Public License Version
 * 1.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://www.enhydra.org/software/license/epl.html
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under the
 * License.
 *-----------------------------------------------------------------------------
 * $Id: SimpleSender.java,v 1.2 2002/10/24 14:05:31 jmesnil Exp $
 *-----------------------------------------------------------------------------
 */

import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.transaction.UserTransaction;

/**
 * Created on Mar 4, 2002
 * @author  Christophe Ney - Lutris Technologies cney@batisseurs.com
 */
public class SimpleSender extends Thread {

    /**
     * Send a few messages on a given queue
     */
    void sendMessages(String msg[], QueueConnectionFactory factory, Queue queue) throws JMSException {
        // create a connection, session, sender, and the message QueueConnection conn;
        QueueConnection connection = factory.createQueueConnection();
        QueueSession session = connection.createQueueSession (false, Session.AUTO_ACKNOWLEDGE);
        QueueSender sender = session.createSender(queue);

        // start up the connection, send the message
        connection.start();
        for (int i=0; i<msg.length; i++) {
            TextMessage message = session.createTextMessage(msg[i]);
            sender.send(message);
            System.out.println("[SimpleSender] send : "+message.getText());
        }

        // now close all resources
        connection.stop();
        sender.close();
        session.close();
        connection.close();
    }

    public void run() {
        try {
            // Use JNDI to find the connection factory and the destination
            Context ctx = new InitialContext();
            QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("theQueueConnectionFactory");
            Queue queue = (Queue) ctx.lookup("theQueue");
            UserTransaction utx = (UserTransaction)ctx.lookup("UserTransaction");

            sendMessages(new String[]{"non transactional message"},factory,queue);

            utx.begin();
            sendMessages(new String[]{"transactional message with commit"},factory,queue);
            utx.commit();

            utx.begin();
            sendMessages(new String[]{"transactional message with rollback"},factory,queue);
            utx.rollback();

            sendMessages(new String[]{"LAST message"},factory,queue);

        } catch (Exception e) {
            System.err.println("Exception thrown from SimpleSender "+e.getMessage());
            e.printStackTrace();
        }
    }


}
