我在JBoss 7.1上开发了一个应用程序,该应用程序必须监视fileSystem的目录。
为此,我编写了一些代码,类似于我在这篇文章中找到的代码:EJB 3.1 and NIO2: Monitoring the file system。
因此,我编写了一个带有异步方法的会话ejb PollingServiceImpl,用于监视我必须监视的路径以及一个@StartUp Ejb,后者调用会话EJB。
这是我的PollingService代码:
@Stateless
public class PollingServiceImpl {
private Path fichiersXmlPath=Paths.get("/media/sf_DossierPartage/IsidoreJaxb/specXML/fichiersXml");
/**
* Default constructor.
*/
public PollingServiceImpl() {
// TODO Auto-generated constructor stub
}
@Asynchronous
public void pollForXmlMessage() {
WatchService service=null;
// Vérification que le path est un répertoire
try {
Boolean isFolder = (Boolean) Files.getAttribute(fichiersXmlPath,
"basic:isDirectory", LinkOption.NOFOLLOW_LINKS);
if (!isFolder) {
throw new IllegalArgumentException("le path : " + fichiersXmlPath + " n'est pas un répertoire");
}
} catch (IOException ioe) {
System.out.println("le path : " + fichiersXmlPath + " n'est pas un répertoire");
}
System.out.println("Répertoire observé: " + fichiersXmlPath);
// On obtient le système de fichier du chemin
FileSystem fs = fichiersXmlPath.getFileSystem ();
// We create the new WatchService using the new try() block
try{ service = fs.newWatchService();
//On enregistre le chemin dans le watcher
// On surveille les opérations de création
fichiersXmlPath.register(service, StandardWatchEventKinds.ENTRY_CREATE);
// Beginning of the infinite loop
for(;;) {
WatchKey key = service.take();
// Sortir les événements de la queue
Kind<?> kind = null;
for(WatchEvent<?> watchEvent : key.pollEvents()) {
// on teste le type de l'événement
kind = watchEvent.kind();
// If les événements sont perdus
if (StandardWatchEventKinds.OVERFLOW == kind) {
continue; //loop
// Si c'est un événement de création
} else if (StandardWatchEventKinds.ENTRY_CREATE == kind) {
// A new Path was created
Path newPath = ((WatchEvent<Path>) watchEvent).context();
// Output
System.out.println("nouveau chemin : " + newPath +" numKey "+key.toString() );
// buisness process
}
}
if (key != null) {
boolean valid = key.reset();
if (!valid) break; // If the key is no longer valid, the directory is inaccessible so exit the loop.
}
}
} catch(IOException ioe) {
ioe.printStackTrace();
} catch(InterruptedException ie) {
ie.printStackTrace();
} finally{
try {
service.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
这是startp ejb的代码:
@辛格尔顿
@启动
公共类初始化器{
@EJB
private PollingServiceImpl pollingService;
public enum States {BEFORESTARTED, STARTED, PAUSED, SHUTTINGDOWN};
private States state;
@PostConstruct
public void initialize() {
state = States.BEFORESTARTED;
pollingService.pollForXmlMessage();
state = States.STARTED;
System.out.println("ETAT : "+state.toString());
}
@PreDestroy
public void terminate() {
state = States.SHUTTINGDOWN;
System.out.println("Shut down in progress");
}
public States getState() {
return state;
}
public void setState(States state) {
this.state = state;
}
}
一切看起来都很好,观察程序运行良好,但是我有两个问题:
1)控制台上会出现一些警告,如下所示:
[com.arjuna.ats.arjuna](交易收割工人0)ARJUNA012095:中止动作ID
0:ffff7f000101:-51121f7e:53b7f879:9在其中有多个线程处于活动状态时被调用。
15:12:14,522警告[com.arjuna.ats.arjuna](交易收割者0)ARJUNA012108:
CheckedAction :: check-原子动作0:ffff7f000101:-51121f7e:53b7f879:9中止
1个线程处于活动状态!
15:12:14,522警告[com.arjuna.ats.arjuna](交易收割者0)ARJUNA012121:
TransactionReaper :: doCancellations工作线程[Transaction Reaper Worker 0,5,main]
成功取消了TX 0:ffff7f000101:-51121f7e:53b7f879:9
2)服务器拒绝关闭:显然,启动时启动的线程不会停止。我已经读过一些帖子,我必须在Initializer ejb的@PreDestroy方法中停止方法pollForXmlMessage,但是我不知道我该怎么做,或者是否有其他解决方案。
有谁可以帮助我吗?
最佳答案
这是解决方案的模板:
public class Service{
private volotile boolean isCancelled = false;
public void startService()
{ //infinit loop:
while(!isCancelled) {}
}
public void stopService()
{
isCancelled = true;
}
}
@Singleton
@Startup
public class ServiceStarter{
Service service;
@EJB ServiceAsyncWorker asyncWorker;
@PostConstruct
private void init()
{
service = new Service(...);
asyncWorker.startService(service);
}
@PreDestroy
private void destroy()
{
service.stopService();
}
}
@Singleton
public class ServiceAsyncWorker{
@Asynchronous
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void startService(Service service){
service.startService();
}
}