简体   繁体   English

EJB 3 Timer问题

[英]EJB 3 Timer problem

I am using JBoss 4.2.3 with JDK 1.5. 我正在将JBoss 4.2.3和JDK 1.5一起使用。 I have created a stateless EJB whose purpose is to delete a file after a specified period of time (in milliseconds). 我创建了一个无状态EJB,其目的是在指定的时间段(以毫秒为单位)之后删除文件。

The EJB code is: EJB代码是:

import java.io.File;

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

import org.jboss.annotation.ejb.LocalBinding;

@Stateless
@LocalBinding(jndiBinding = "TimedFileDeletion")
public class TimedFileDeletionBean implements TimedFileDeletionBeanLocal {

    @Resource
    TimerService timerService;

    File fileToDelete;

    public void setRequiredInfo(long intervalDuration, File fileToDelete) {
        timerService.createTimer(intervalDuration, "Created new timer");
        this.fileToDelete = fileToDelete;
    }

    @Timeout
    public void timeout(Timer timer) {
        System.out.println("Timeout occurred");

        if(fileToDelete.exists()) {
            fileToDelete.delete();
        }
    }
}

The local interface is: 本地接口为:

import java.io.File;

public interface TimedFileDeletionBeanLocal {

    public void setRequiredInfo(long intervalDuration, File fileToDelete);
}

When I call the bean through the web container (I use the Stripes framework) the timeout method is called after the specified time but it only prints "Timeout occurred", it does not delete the file and it throws an exception. 当我通过Web容器调用Bean(我使用Stripes框架)时,在指定的时间后调用了timeout方法,但是它仅显示“ Timeout created”,它不会删除文件,并且会引发异常。 This is the console output: 这是控制台输出:

INFO  [STDOUT] Timeout occurred
ERROR [TimerImpl] Error invoking ejbTimeout: javax.ejb.EJBException: java.lang.NullPointerException

Any advice would be appreciated. 任何意见,将不胜感激。

One thing that appears that might be a problem is that you're passing into the setRequiredInfo method a reference to a File. 可能出现的问题是,您正在将对文件的引用传递给setRequiredInfo方法。 That reference is then stored locally, using a debugger I would verify that the reference value is the same when the timer fires. 然后使用调试器将该引用存储在本地,我将在计时器触发时验证该引用值是否相同。 I suspect that it may no longer be the same file referenced or that the File object may be transient. 我怀疑它可能不再是引用的同一文件,或者File对象可能是瞬时的。

Also, just a little warning with EJBTimers and JBoss. 另外,对EJBTimers和JBoss只需一点警告。 This version of JBoss spins a thread for every EJB with a timer. 此版本的JBoss使用计时器为每个EJB旋转线程。 So, if you have 500 files to delete with these EJBs, JBoss will spin up 500 threads. 因此,如果您要使用这些EJB删除500个文件,那么JBoss将启动500个线程。 This behavior, while undesirable, does comply with the EJB spec (which is ambiguous on implementation). 这种行为虽然不受欢迎,但确实符合EJB规范(在实现上是模棱两可的)。 These threads will be recreated if the container restarts and the timers are still waiting to fire. 如果容器重新启动并且计时器仍在等待触发,则将重新创建这些线程。

In stateless session bean, conversational state is not maintained. 在无状态会话Bean中,不维护会话状态。 Stateless bean instance variables are shared between invocations, so they may overlap. 无状态Bean实例变量在调用之间共享,因此它们可能会重叠。

Therefore even if you set file using setRequiredInfo(), on timeout it gets the fileToDelete null. 因此,即使您使用setRequiredInfo()设置文件,在超时时它也会得到fileToDelete null。

Try checking null before doing operation. 尝试在操作前检查null。 Below is some code snippet might help you. 以下是一些可能会帮助您的代码段。

class FileUtility {

// Make singleton class to store list of files to delete

public static List<File> files;

//-- get/set accessing methods

}

//--------------------- // ---------------------

public void setRequiredInfo(long intervalDuration, File fileToDelete) {
        timerService.createTimer(intervalDuration, fileToDelete.getName()+Math.random());
        FileUtility.files.add(fileToDelete);
}

//--------------------- // ---------------------

@Timeout
    public void timeout(Timer timer) {
        System.out.println("Timeout occurred");

    for(File fileToDelete : Fileutility.files){

        if(fileToDelete.exists()) {
            fileToDelete.delete();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM