Package org.ow2.easybeans.examples.timerservice

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

/**
* 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: MessageDrivenBean.java 5369 2010-02-24 14:58:19Z benoitf $
* --------------------------------------------------------------------------
*/

package org.ow2.easybeans.examples.timerservice;

import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
import javax.jms.Message;
import javax.jms.MessageListener;

/**
* Example of Message Driven Bean.<br />
* This is an JMS MDB, implementing the MessageListener interface.
* @author Florent Benoit
*/
@MessageDriven(activationConfig = {
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "SampleTimerQueue"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
        }
)
public class MessageDrivenBean implements MessageListener {

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


    /**
     * MDB context used to access to the timer service. (timer service can also be injected).
     */
    @Resource
    private MessageDrivenContext messageDrivenContext = null;


    /**
     * Passes a message to the listener.
     * @param message - the message passed to the listener
     */
    public void onMessage(final Message message) {
        // Get the timer service
        TimerService timerService = messageDrivenContext.getTimerService();

        // Calling the timer service
        timerService.createTimer(FIRING_TIME, "Timer started by the onMessage() method");
    }

    /**
     * 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 theMDBtimerMethod(final Timer timer) {
        System.out.println("  MDB -> Timer method called by the Timer Service.");
        System.out.println("  MDB -> Timer received = '" + timer + "'.");
        System.out.println("  MDB -> Info object inside the timer object is '" + timer.getInfo() + "'.");
    }

}
TOP

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

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.