/*
 * Copyright (c) 1998-2000, Fiorano Software, Inc.
 * All Rights Reserved
 *
 * JMS-EJB Integration Sample
 * 
 * FileName   : Publisher.java
 *
 * class Publishergenerates JMS Messages (Events) every 2 secs, 
 * simulating event generation.
 *
 * These Publisher trigger either ADDITION/DELETION/UPDATION Events.
 *
 * Each Event, corresponds to an EJB. The properties of the EJB to invoke are
 * contained in the message as JMS Message Properties.
 *
 * The JMSMessage can optionally contain details of data needed to perform 
 * database operations
 *
 *  Any JMS Messages can be used to trigger these events. Header properties 
 *  used
 * 
 *  EJB_NAME      - name of the Ejb to invoke
 *
 *  CONST        - constructor name (Create)
 *  CONST_N_ARGS - number of args to the constructor
 *  CONST_ARGS0  - Argument 0
 *  CONST_TYPE0  - Argument type
 *  .....
 *
 *  METHOD_NAME    - method to invoke on the bean
 *  METHOD_N_ARGS  - Number of args to the method
 *  METHOD_ARGS0   - Argument 0
 *  METHOD_TYPE0   - Argument 0 type
 *  .....
 * 
 * Questions/comments/suggestions?
 * Please visit: http://www.fiorano.com
 * or e-mail: support@fiorano.com
 *
 * @since     4.4, May 2000
 */

import javax.jms.*;
import java.io.*;
import java.net.*;

import fiorano.jms.rtl.FioranoInitialContext;

class Publisher implements ExceptionListener
{
    public static void main (String args[])
    {
        Publisher publisher = new Publisher ();
        try
        {
            //  1. Create the InitialContext Object used for looking up
            //     JMS administered objects on the FioranoMQ
            //     located on the default host.
            //
            FioranoInitialContext ic = null;

            ic = new FioranoInitialContext ();
            ic.bind ();

            // 1.1  Lookup Connection Factory and Queue names
            //
            QueueConnectionFactory qcf =
                        (QueueConnectionFactory) ic.lookup ("primaryQCF");
            Queue queue = (Queue)ic.lookup("primaryQueue");

            // 1.2  Dispose the InitialContext resources
            //
            ic.dispose();

            // 2. Create and start a queue connection
            System.out.println("Creating queue connection");
            QueueConnection qc = qcf.createQueueConnection();
            
	        // Set the ExceptionListener for this QueueConnection
	        qc.setExceptionListener (publisher);
            qc.start ();

            // 3. Create a Queue session on this connection
            System.out.println("Creating Queue session: not transacted, auto ack");
            QueueSession qs = qc.createQueueSession(false,1);

            // 4. Create a publisher for this queue
            System.out.println("Creating Queue ,publisher");
            QueueSender sender = qs.createSender(queue);

            System.out.println ("Ready to publish messages : Enter Q to Quit...");

            // 6. Read in data from standard input and publish it in a loop
            int i = 0;
            while (true)
            {
                // Create a text message for use in the while loop
                // Any JMS Message can be used, as the header properties
                // can be set on all JMS Messages.
                TextMessage textmsg1 = qs.createTextMessage();
                textmsg1.setJMSType(JMSConstants.FIORANO_EJB_MESSAGE);
                i++;
                
                // Publish an Add or Delete or Update Event.
                // Each of this Event results in invoking a method
                // on an EJB
                if (i%3 == 0)
                {
                    System.out.println ("Publishing Add Event");
                    // Set appropriate message properties
                    // to invoke the ADD Bean
                    textmsg1.setStringProperty(JMSConstants.EJB_NAME , "Add");
                    textmsg1.setStringProperty(JMSConstants.CONST, "create");
                    textmsg1.setIntProperty(JMSConstants.CONST_N_ARGS, 0);
                    
                    textmsg1.setStringProperty(JMSConstants.METHOD_NAME, "add");
                    textmsg1.setIntProperty(JMSConstants.METHOD_N_ARGS, 0);
                }
                else if (i%3 == 1)
                {
                    System.out.println ("Publishing Delete Event");
                    // Set appropriate message properties
                    // to invoke the DELETE Bean
                    textmsg1.setStringProperty(JMSConstants.EJB_NAME ,"Delete" );
                    textmsg1.setStringProperty(JMSConstants.CONST, "create");
                    textmsg1.setIntProperty(JMSConstants.CONST_N_ARGS, 0);
                    
                    textmsg1.setStringProperty(JMSConstants.METHOD_NAME, "delete");
                    textmsg1.setIntProperty(JMSConstants.METHOD_N_ARGS, 0);
                }
                else if (i%3 == 2)
                {
                    System.out.println ("Publishing Update Event");
                    // Set appropriate message properties
                    // to invoke the UPDATE Bean
                    textmsg1.setStringProperty(JMSConstants.EJB_NAME ,"Update" );
                    textmsg1.setStringProperty(JMSConstants.CONST, "create");
                    textmsg1.setIntProperty(JMSConstants.CONST_N_ARGS, 0);
                    
                    textmsg1.setStringProperty(JMSConstants.METHOD_NAME, "update");
                    textmsg1.setIntProperty(JMSConstants.METHOD_N_ARGS, 0);
                }
                
                // Set any other property to the message, that can be used by 
                // the Bean ....
                
                // Publish the message.
                sender.send(textmsg1);
                
                // Sleep for 2 secs before sending in the next Event
                Thread.sleep (2000);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace ();
        }
    } // main

    /** 
     * If a JMS provider detects a serious problem with a 
     * Connection this method is invoked passing it a JMSException 
     * describing the problem. 
     * @param JMSException e
     */
    public void onException (JMSException e)
    {
	    //Report the Error and take necessary Error handling measures
        String error = e.getErrorCode ();
	    System.out.println (error);
    }
}