EJB定时器服务

//TimerServiceDAO.java

package com.wdf.ejb3;

public interface TimerServiceDAO {
public void scheduleTimer(long milliseconds);
}

//TimerServiceBean.java
package com.wdf.ejb3.impl;

import java.util.Date;

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

import com.wdf.ejb3.TimerServiceDAO;
@Stateless
@Remote(TimerServiceDAO.class)
public class TimerServiceBean implements TimerServiceDAO{
   private static int count=0;
   @Resource private TimerService timerService;
public void scheduleTimer(long milliseconds) {
  // TODO Auto-generated method stub
  if(count==0){
   count=1;
   timerService.createTimer(new Date(new Date().getTime()+milliseconds), milliseconds, "大家好。这是我的第一个定时器!");
  }
}
@Timeout
public void timeoutHandler(Timer timer){
  System.out.println("count="+count);
  System.out.println("timer="+timer.getInfo());
  if(count>=5){
   timer.cancel();
   count=0;
  }else{
   count++;
  }
}

}

//JSP

<%@ page language="java" import="com.wdf.ejb3.*,javax.naming.*" pageEncoding="gb2312"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
  <%
                InitialContext ctx=new InitialContext();
                TimerServiceDAO remote=(TimerServiceDAO)ctx.lookup("TimerServiceBean/remote");
                remote.scheduleTimer((long)3000);
                out.println("定时器调用!");                          
   %>
  </body>
</html>

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