Package org.ow2.easybeans.examples.messagedrivenbean

Source Code of org.ow2.easybeans.examples.messagedrivenbean.ClientMessageDriven

/**
* EasyBeans
* Copyright (C) 2006 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: ClientMessageDriven.java 5644 2010-10-19 12:40:11Z benoitf $
* --------------------------------------------------------------------------
*/

package org.ow2.easybeans.examples.messagedrivenbean;

import java.util.Hashtable;

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;

/**
* Client of the Message Driven Bean.
* @author Florent Benoit
*/
public final class ClientMessageDriven {

    /**
     * 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 = "SampleQueue";

    /**
     * Rollback Queue name used by this example.
     */
    private static final String SAMPLE_QUEUE_CONTEXT = "SampleRollbackQueue";

    /**
     * Number of messages to send.
     */
    private static final int NUMBER_MESSAGES = 5;

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

    /**
     * Utility class.
     */
    private ClientMessageDriven() {

    }

    /**
     * Main method.
     * @param args the arguments (not required)
     * @throws Exception if exception is found.
     */
    public static void main(final String[] args) throws Exception {

        // Build Context
        Context initialContext = getInitialContext();

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

        // Lookup the Queue through its JNDI name
        Queue queue = (Queue) initialContext.lookup(SAMPLE_QUEUE);
        Queue queueContext = (QueueinitialContext.lookup(SAMPLE_QUEUE_CONTEXT);


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

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

        // Create sender
        QueueSender queueSender = queueSession.createSender(queue);

        // Create sender for context MDB
        QueueSender queueSenderContext = queueSession.createSender(queueContext);

        // Send messages
        TextMessage message = null;
        for (int i = 0; i < NUMBER_MESSAGES; i++) {
            message = queueSession.createTextMessage();
            String text = "Message_" + i;
            message.setText(text);
            queueSender.send(message);
            System.out.println("Message [" + message.getJMSMessageID() + ", text:" + text + "] sent");
        }

        message = queueSession.createTextMessage();
        message.setText("Message that should be accepted after several attempts");
        queueSenderContext.send(message);

        // Close JMS objects
        queueSender.close();
        queueSenderContext.close();
        queueSession.close();
        queueConnection.close();

    }

    /**
     * @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);
    }

    /**
     * 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.messagedrivenbean.ClientMessageDriven

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.