Package org.ow2.easybeans.examples.timerservice

Source Code of org.ow2.easybeans.examples.timerservice.TimerBean

/**
* EasyBeans
* Copyright (C) 2007 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: TimerBean.java 5369 2010-02-24 14:58:19Z benoitf $
* --------------------------------------------------------------------------
*/

package org.ow2.easybeans.examples.timerservice;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
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;

/**
* Stateless bean.
* @author Florent Benoit
*/
@Stateless(mappedName="timerBean")
public class TimerBean implements TimerBeanRemote {

    /**
     * Time before firing a new Timer.
     */
    private static final long FIRING_TIME = 3000;

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

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

    /**
     * Link to the local bean.
     */
    @EJB
    private TimedLocal timedBean;

    /**
     * Queue factory for queue sessions.
     */
    @Resource(mappedName=QUEUE_CONNECTION_FACTORY)
    private QueueConnectionFactory queueConnectionFactory;

    /**
     * Queue for sending the messages.
     */
    @Resource(mappedName=SAMPLE_TIMER_QUEUE)
    private Queue timerQueue;

    /**
     * Timer service (injected).
     */
    @Resource
    private TimerService timerService;


    /**
     * Business init method that will fire a Timer.
     */
    public void init() {
        // Calling the timer service
        timerService.createTimer(FIRING_TIME, "Simple Serializable object");
    }


    /**
     * Timeout method that will be called by the timer service when a timer is expiring.
     * @param timer the timer object containing some data.
     */
    @Timeout
    public void timerMethod(final Timer timer) {
        System.out.println(" SLSB -> Timer method called by the Timer Service.");
        System.out.println(" SLSB -> Timer received = '" + timer + "'.");
        System.out.println(" SLSB -> Info object inside the timer object is '" + timer.getInfo() + "'.");

        // Send a message on an MDB (that start a timer)
        System.out.println(" SLSB -> Sending a message to a MDB which will start a timer.");
        try {
            sendMessage();
        } catch (Exception e) {
            throw new EJBException("Cannot send a message to an MDB", e);
        }

        // Start a timer on another local bean that implements javax.ejb.TimedObject
        System.out.println(" SLSB -> Call a local bean in order to start a new timer.");
        timedBean.startTimer();
    }


    /**
     * Send a message to a queue.
     * @throws Exception if there is failure while sending a message
     */
    private void sendMessage() throws Exception {
        // Create connection
        QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();

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

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

        // Send message
        TextMessage message = queueSession.createTextMessage();
        message.setText("Sending a message");
        queueSender.send(message);
        System.out.println(" SLSB -> Message sent");

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

    }

}
TOP

Related Classes of org.ow2.easybeans.examples.timerservice.TimerBean

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.