Package org.ow2.easybeans.examples.pool

Source Code of org.ow2.easybeans.examples.pool.Client

/**
* EasyBeans
* Copyright (C) 2008 Bull S.A.S.
* Contact: easybeans@ow2.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
* USA
*
* --------------------------------------------------------------------------
* $Id: Client.java 5369 2010-02-24 14:58:19Z benoitf $
* --------------------------------------------------------------------------
*/

package org.ow2.easybeans.examples.pool;

import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
* Access to the bean with a limited Pool size.
* @author Florent BENOIT
*/
public final class Client {

    /**
     * Default InitialContextFactory to use.
     */
    private static final String DEFAULT_INITIAL_CONTEXT_FACTORY = "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory";

    /**
     * Queue connection factory (Available for clients).
     */
    private static final String QUEUE_CONNECTION_FACTORY = "JQCF";

    /**
     * Queue name used by this example.
     */
    private static final String SAMPLE_QUEUE_MDB_XML = "SampleQueueMDBXML";

    /**
     * Queue name used by this example.
     */
    private static final String SAMPLE_QUEUE_MDB_ANNOTATION = "SampleQueueMDB";


    /**
     * Number of threads in // .
     */
    private static final int THREADS_NUMBER = 50;

    /**
     * Waiting time.
     */
    private static final long WAITING_TIME = 5000L;

    /**
     * Queue connection.
     */
    private static QueueConnection queueConnection;

    /**
     * Queue session for creating messages/senders.
     */
    private static QueueSession queueSession;

    /**
     * Sender for the Annotation MDB.
     */
    private static QueueSender annotationQueueSender;

    /**
     * Sender for the XML MDB.
     */
    private static QueueSender xmlQueueSender;


    /**
     * Client class, no public constructor.
     */
    private Client() {

    }

    /**
     * Execute the client code.
     * @param args the client arguments
     */
    public static void main(final String[] args) {

        Context initialContext = null;

        try {
            initialContext = getInitialContext();
        } catch (NamingException e) {
            System.err.println("Cannot get InitialContext: " + e);
            System.exit(2);
        }

        // Get bean configured with annotation
        final BusinessInterface poolBeanAnnotation;
        try {
            poolBeanAnnotation = (BusinessInterface) initialContext.lookup("poolBeanConfiguredByAnnotation");
        } catch (NamingException e) {
            System.err.println("Cannot get bean: " + e);
            System.exit(2);
            return;
        }

        // Get bean configured with XML
        final BusinessInterface poolBeanXML;
        try {
            poolBeanXML = (BusinessInterface) initialContext.lookup("poolBeanConfiguredByXML");
        } catch (NamingException e) {
            System.err.println("Cannot get bean: " + e);
            System.exit(2);
            return;
        }

        // Create runners that will call several times the bean
        Runnable runnerAnnotation = new Runnable() {
            public void run() {
                poolBeanAnnotation.dummyWork();
            }
        };

        Runnable runnerXML = new Runnable() {
            public void run() {
                poolBeanXML.dummyWork();
            }
        };

        System.out.println("Calling bean's methods...");
        // Launch several threads on the beans
        for (int i = 0; i < THREADS_NUMBER; i++) {
            new Thread(runnerAnnotation).start();
            new Thread(runnerXML).start();
        }

        System.out.println("Waiting some time before checking the number of instances...");
        // Wait some times
        try {
            Thread.sleep(WAITING_TIME);
        } catch (InterruptedException e) {
            System.err.println("Error while waiting: " + e);
            System.exit(2);
        }

        // Check the number of instances created for each bean
        System.out.println("Number of instances Annotation Bean = " + poolBeanAnnotation.getInstances());
        if (poolBeanAnnotation.getInstances() <= BusinessInterface.MAX_INSTANCE_ANNOTATION) {
            System.out.println("  --> This value is OK, pool is limited to " + BusinessInterface.MAX_INSTANCE_ANNOTATION);
        } else {
            System.err.println("  --> Invalid value, pool KO");
        }
        System.out.println("Number of instances XML Bean = " + poolBeanXML.getInstances());
        if (poolBeanXML.getInstances() <= BusinessInterface.MAX_INSTANCE_XML) {
            System.out.println("  --> This value is OK, pool is limited to " + BusinessInterface.MAX_INSTANCE_XML);
        } else {
            System.err.println("  --> Invalid value, pool KO");
        }

        // Now, test JMS
        try {
            initJMS();
        } catch (JMSException e) {
            System.err.println("Cannot initialize JMS");
            e.printStackTrace();
            System.exit(1);
        } catch (NamingException e) {
            System.err.println("Cannot initialize JMS");
            e.printStackTrace();
            System.exit(1);
        }


        // Create a runner for annotation queue
        Runnable runnerMDBAnnotation = new Runnable() {
            public void run() {
                try {
                TextMessage message = queueSession.createTextMessage();
                String text = "Message";
                message.setText(text);
                annotationQueueSender.send(message);
            } catch (JMSException e) {
                throw new IllegalStateException("Cannot send messages on annotation MDB", e);
            }

            }
        };

        // Create a runner for annotation queue
        Runnable runnerXMLAnnotation = new Runnable() {
            public void run() {
                try {
                    TextMessage message = queueSession.createTextMessage();
                    String text = "Message";
                    message.setText(text);
                    xmlQueueSender.send(message);
                } catch (JMSException e) {
                    throw new IllegalStateException("Cannot send messages on XML MDB", e);
                }
            }
        };


        System.out.println("Sending messages with multiple threads...");
        // Launch several threads on the beans
        for (int i = 0; i < THREADS_NUMBER; i++) {
            new Thread(runnerMDBAnnotation).start();
            new Thread(runnerXMLAnnotation).start();
        }

        System.out.println("Waiting some time to ensure that all messages have been sent...");
        // Wait some times
        try {
            Thread.sleep(WAITING_TIME);
        } catch (InterruptedException e) {
            System.err.println("Error while waiting: " + e);
            System.exit(2);
        }

        System.out.println("Look at the server side console to check pool values of MDB ...");

        // Close objects
        try {
            closeJMSObjects();
        } catch (JMSException e) {
            System.err.println("Cannot close JMS");
            e.printStackTrace();
            System.exit(1);
        }
    }

    /**
     * @return Returns the InitialContext.
     * @throws NamingException If the Context cannot be created.
     */
    private static Context getInitialContext() throws NamingException {

        // if user don't use jclient/client container
        // we can specify the InitialContextFactory to use
        // But this is *not recommended*.
        Hashtable<String, Object> env = new Hashtable<String, Object>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactory());

        // Usually a simple new InitialContext() without any parameters is
        // sufficent.
        // return new InitialContext();

        return new InitialContext(env);
    }


    /**
     * Initialize the JMS objects.
     * @throws JMSException if JMS objects are not initialized
     * @throws NamingException if context is not found
     */
    public static void initJMS() throws JMSException, NamingException {

        // Get context
        Context initialContext = getInitialContext();

        // Get factory
        QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) initialContext
        .lookup(QUEUE_CONNECTION_FACTORY);

        // Lookup the Queues through their JNDI name
        Queue annotationQueue = (Queue) initialContext.lookup(SAMPLE_QUEUE_MDB_ANNOTATION);
        Queue xmlQueue = (Queue) initialContext.lookup(SAMPLE_QUEUE_MDB_XML);

        // Create connection
        queueConnection = queueConnectionFactory.createQueueConnection();

        // Create session
        queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

        // Create senders
        annotationQueueSender = queueSession.createSender(annotationQueue);
        xmlQueueSender = queueSession.createSender(xmlQueue);


    }

    /**
     * Close the JMS objets that have been used.
     * @throws JMSException if objects can't be closed
     */
    public static void closeJMSObjects() throws JMSException {
        // Close JMS objects
        annotationQueueSender.close();
        xmlQueueSender.close();
        queueSession.close();
        queueConnection.close();
    }


    /**
     * Returns a configurable InitialContextFactory classname.<br/> Can be
     * configured with the <code>easybeans.client.initial-context-factory</code>
     * System property.
     * @return Returns a configurable InitialContextFactory classname.
     */
    private static String getInitialContextFactory() {
        String prop = System.getProperty("easybeans.client.initial-context-factory");
        // If not found, use the default
        if (prop == null) {
            prop = DEFAULT_INITIAL_CONTEXT_FACTORY;
        }
        return prop;
    }

}
TOP

Related Classes of org.ow2.easybeans.examples.pool.Client

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.