在一个方法中,我可能需要生成一个下载bean来获取文件。当我并行接收这些调用时,我只想下载一次文件。我不知道用JEE/CDI表达以下伪代码的正确方式:
if (download_required) {
monitor.enter();
if (!map.ContainsKey(downloadPath))
{
//CDI inject new instance of downloader here.
downloader.File = downloadPath;
downloader.startDownload();
map.put(downloadPath, downloader);
monitor.exit();
downloader.waitForDownload();
} else {
monitor.exit();
map.get(downloadPath).waitForDownload();
}
}
我想使用CDI,因为我需要从JNDI变量设置下载器的属性。每个下载者负责下载一个文件并留下一些状态。请注意,每个下载器将被赋予不同的状态,因此必须是bean的新实例。
就在我放弃CDI之前,将变量注入父类,然后使用带有一些静态的标准java来实例化上面的内容,有没有一种“正确”的方法来实现CDI?