EJB定时器

版权声明:The beautiful thing about learning is nobody can take it away from you. https://blog.csdn.net/xiuye2015/article/details/50532861
package remote;

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

@Remote
public interface TimerEJB {

	void setTime(java.util.Date init,long interval);
	
	void check(Timer timer);
	
}
package stateless;

import java.util.Date;

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

import remote.TimerEJB;

@Stateless(mappedName="timerEJBBean")
public class TimerEJBBean implements TimerEJB{

	@Resource
	private TimerService timerService;
	
	@Resource
	private SessionContext context;
	
	

	@Override
	public void setTime(Date init, long interval) {
		
		System.out.println(timerService);
		
		timerService.createTimer(init, interval,"新的定时器");
		//this.context.getTimerService().createTimer(init, interval,"新的定时器");
		
	}

	@Timeout
	@Override
	public void check(Timer timer) {
		
		
		System.out.println("定时器消息:"+timer.getInfo());
		
		System.out.println("模拟系统检查");
                timer.cancel();
	}

	
	
}
package ejb.client.test;

import java.util.Date;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

import remote.TimerEJB;
import stateless.TimerEJBBean;

public class TimerejbClient {

	public static void main(String[] args) {
		
		
		try{
			
			Properties props = new Properties();
			
			props.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
			
			InitialContext ctx = new InitialContext(props);
			
			TimerEJB timerejb = (TimerEJB) ctx.lookup("ejb:"
					+ "/timerejb/TimerEJBBean!remote.TimerEJB");
			
			//timerejb.setTime(new Date()	, 1000);
			timerejb.setTime(new Date(), 10000);
			
			ctx.close();
			
		}
		catch(Exception e){
			
			e.printStackTrace();
			
			
		}
		
		
	}
	
}






阅读更多
换一批

没有更多推荐了,返回首页