EJB Timer 使用

Java 定时器之EJB Timer:

Interface:
package test.timersession;

import javax.ejb.Remote;
import javax.ejb.Timer;


@Remote
public interface TimerSession {
    public void setTimer(long intervalDuration, String timerName);

    public void timeout(Timer timer);
}


EJB:
package test.timersession;

import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.logging.Logger;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;

@Stateless (name="TimerSession", mappedName="TimerSession")
public class TimerSessionBean implements TimerSession, Serializable {
    /**
	 * 
	 */
	private static final long serialVersionUID = 7349339140991323648L;

	private static final Logger logger = Logger.getLogger("test.timersession.TimerSessionBean");
    
	@Resource
	TimerService timerService;
	
	public void setTimer(long intervalDuration, String timerName) {
		System.out.println("In TimerSessionBean -> setTimer method");
	Timer timer = timerService.createTimer(
	        new Date(System.currentTimeMillis()),intervalDuration,
	        timerName);
	System.out.println("Exit TimerSessionBean -> setTimer method");
	}
	
	@Timeout
	public void timeout(Timer timer) {
		String info = (String) timer.getInfo();
		logger.info(info + " - Timeout occurred");
		System.out.println("In TimerSessionBean -> timeout method");
	}
	
	@PreDestroy
	public void clearTimer(){
		System.out.println("In TimerSessionBean -> clearTimer method");
		if (null != timerService.getTimers()) {
			Iterator<?> it = timerService.getTimers().iterator();
			while (it.hasNext()) {
				((Timer)it.next()).cancel();
			}
		}
		System.out.println("Exit TimerSessionBean -> clearTimer method");
	}

}



Client:
package test.timersession;

import java.util.Properties;

import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;

public class TimerSessionClient {
    @EJB
    private static TimerSession timer;
    private static TimerSession timer2;
    private static TimerSession timer3;

    public TimerSessionClient(String[] args) {
    }

    public static void main(String[] args) {
        TimerSessionClient client = new TimerSessionClient(args);
        client.doClient();
    }

    public void doClient() {
        try {
            long intervalDuration = 5000;
            System.out.println(
                    "Creating a timer with an interval duration of "
                    + intervalDuration + " ms.");
    		Properties prop = new Properties();
    		prop.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
    		prop.put(Context.PROVIDER_URL, "t3://192.168.81.143:8889,192.168.81.219:8888");
    		
    		Context ctx = new InitialContext(prop);
    		timer = (TimerSession) ctx.lookup("TimerSession#test.timersession.TimerSession");
    		timer2 = (TimerSession) ctx.lookup("TimerSession#test.timersession.TimerSession");
    		timer3 = (TimerSession) ctx.lookup("TimerSession#test.timersession.TimerSession");

            timer.setTimer(intervalDuration, "Timer 1");
            timer2.setTimer(intervalDuration+1000, "Timer 2");
            timer3.setTimer(intervalDuration+2000, "Timer 3");

            //System.exit(0);
        } catch (Exception ex) {
            System.err.println("Caught an unexpected exception.");
            ex.printStackTrace();
            //System.exit(1);
        }
    }
}

你可能感兴趣的:(java,weblogic,ejb)