分类:
2008-11-17 22:32:46
NewsTimer.java package com.kuaff.ejb3.schedule; import javax.ejb.Remote; @Remote public interface NewsTimer { public void fiveNews(); } |
NewsTimerBean.java package com.kuaff.ejb3.schedule; import java.util.Date; import javax.ejb.Inject; import javax.ejb.SessionContext; import javax.ejb.Stateless; import javax.ejb.Timer; @Stateless public class NewsTimerBean implements NewsTimer { private @Inject SessionContext ctx; public void fiveNews() { ctx.getTimerService().createTimer(new Date(new Date().getTime() + 300000), "子虚乌有电视台5分钟新闻栏目:现在过5分钟,又到即时新闻节目的时间了。"); } public void ejbTimeout(Timer timer) { System.out.printf("时间到:%n%s%n" , timer.getInfo()); timer.cancel(); } } Client.java package com.kuaff.ejb3.schedule; import javax.naming.InitialContext; import javax.naming.NamingException; public class Client { public static void main(String[] args) throws NamingException { InitialContext ctx = new InitialContext(); NewsTimer timer = (NewsTimer) ctx.lookup(NewsTimer.class.getName()); timer.fiveNews(); } } |
ctx = new InitialContext(); |