Friday, March 15, 2013

Java EE 6 - Part 8 - Transactions

Project: EJB31-ejb


package helloworld.beans;

import helloworld.interceptors.PMInterceptor;
import helloworld.vo.GreetingRequest;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.LocalBean;
import javax.ejb.Schedule;
import javax.ejb.Startup;
import javax.ejb.Timer;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.interceptor.Interceptors;

/**
 * This is a stateless bean using EJB timer service.
 *
 * @author rwatsh
 */
@Singleton
@LocalBean
@Startup
public class AutomaticSayHelloBean {

    @EJB
    private SingletonHelloWorldBean singletonHelloWorldBean;
    private int timerNotifications;
    private boolean cancelTimer = false;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    /**
     * Using schedule will cause a timer notification to occur every 5 seconds.
     */
    @Interceptors(PMInterceptor.class)
    @Schedule(second = "*/5", minute = "*", hour = "*")
    public void sayHello(Timer timer) {
        if (!cancelTimer) {
            GreetingRequest request = singletonHelloWorldBean.sayHello();
            System.out.println("AutomaticSayHelloBean.sayHello:- " + request);
            timerNotifications++;
        } else {
            timer.cancel();
            System.out.println("AutomaticSayHelloBean.sayHello: canceled timer");
        }
    }

    public int getNotificationCount() {
        return timerNotifications;
    }

    @TransactionAttribute(TransactionAttributeType.MANDATORY)
    public void cancelTimer() {
        this.cancelTimer = true;
    }
}

---------------------

package helloworld.beans;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.transaction.UserTransaction;

/**
 * Bean implementation for controlling the timer service EJB.
 *
 * @author rwatsh
 */
@TransactionManagement(TransactionManagementType.BEAN)
@Stateless
public class AutomaticManagerBean implements AutomaticManagerBeanRemote {
    @EJB
    private AutomaticSayHelloBean automaticSayHelloBean;
    @Resource
    private TimerService timerService;
    private int threshold;
    @Resource SessionContext context;
   
    @Override
    public void manageTimer(int interval, int threshold) {
        this.threshold= threshold;
        int currentCount = automaticSayHelloBean.getNotificationCount();
        if (currentCount >= threshold){
            try {
                //automaticSayHelloBean.cancelTimer();
                cancelSayHelloTimer();
            } catch (Exception ex) {
                Logger.getLogger(AutomaticManagerBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            timerService.createIntervalTimer(interval, interval, null);
        }
    }

    @Override
    @Timeout
    public void monitorAutomaticTimer(Timer timer) {      
        int currentCount = automaticSayHelloBean.getNotificationCount();
        if (currentCount >= threshold) {
            try {
                //automaticSayHelloBean.cancelTimer();
                cancelSayHelloTimer();
            } catch (Exception ex) {
                Logger.getLogger(AutomaticManagerBean.class.getName()).log(Level.SEVERE, null, ex);
            }
            timer.cancel();
        }
    }
   
    private void cancelSayHelloTimer() throws Exception {
        UserTransaction ut = context.getUserTransaction();
        ut.begin();
        automaticSayHelloBean.cancelTimer();
        ut.commit();
    }

}

Project: EJB31-app-client
package ejb31;

import helloworld.beans.AutomaticManagerBeanRemote;
import javax.ejb.EJB;

/**
 * Test the automatic timer bean through its session facade
 * AutomaticManagerBean.
 *
 * @author rwatsh
 */
public class MaMApp {

    @EJB
    private static AutomaticManagerBeanRemote automaticManagerBean;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        automaticManagerBean.manageTimer(1000 * 60, 50);
        System.out.println("MaMApp.main: started management process");
    }
}

No comments: