在单例bean中调用WatchService的问题

时间:2013-09-26 06:04:53

标签: java jboss7.x watchservice

我们想定期查看文件以进行更改,我们正在使用jboss 7。以下是我的代码段。我在singleton bean的postconstruct方法中初始化了观察者,并安排了一个轮询监视事件的方法。我第一次修改文件时可以观察到更改,但是后续修改文件没有收到。任何人都可以让我知道可能是什么问题

  @Startup
  @ConcurrencyManagement(ConcurrencyManagementType.BEAN)
  @Interceptors(NonThrowingPostConstructInterceptor.class)
  @Singleton
  @Service
  @LocalBinding(jndiBinding=IHeartBeatProducerService.JNDI_LOCAL_BINDING)
 public class HeartBeatProducerService extends EMSingletonService implements IHeartBeatProducerService{

@EJB(mappedName=IMessageService.JNDI_LOCAL_BINDING)
public IMessageService messageService;

@EJB(mappedName=ICommandExecutionService.JNDI_LOCAL_BINDING)
public ICommandExecutionService commandService;

private final static String LAST_OPERATION_COMPLETED="Last Operation Completed";

private final static String STATUS="Status"; 

private WatchService watcher;

private Path dir;

private String concServer; 

public static final String TOPIC="foo";

private IMLogger logger = new IMLogger("foo");

private String content=null; 

@PostConstruct
@Override
public void init() {
    // TODO Auto-generated method stub
    super.init();
    try {


        watcher = FileSystems.getDefault().newWatchService();
        dir=Paths.get("/shared/foo");
        dir.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
        logger.entering(0, IHeartBeatProducerService.class.getSimpleName(), "Initializing Heart Beat", new String[]{"Entered"});

    } catch (IOException e) {

        e.printStackTrace();
    }
}

@Schedule(second="*/10", minute = "*", hour="*")
private void checkStatus()
{
    logger.entering(0, IHeartBeatProducerService.class.getSimpleName(), "Checking Status", new String[]{"Entered"});
    final String[] command={"pidof","server"};
    commandService.run(command, null, false);
    concServer=(commandService.getExitCode()==0)?"UP":"DOWN";
    if(concServer.equals("UP"))
    {
        watch();
    }
    else
    {
        content="foo:Failed";
    }
    produce();
}


public void watch()
{       
        logger.entering(0, IHeartBeatProducerService.class.getSimpleName(), "Entering watch()", new String[]{"Entered"});
        WatchKey key = null;

        try 
        {           
         key = watcher.take();
        }
        catch (InterruptedException e)
        {               
            logger.error(HeartBeatProducerService.class.getSimpleName(),"Interupted Exception " +  e.getMessage());
        }

        for ( WatchEvent<?> event: key.pollEvents())
        {
            WatchEvent.Kind kind = event.kind();

            logger.info(HeartBeatProducerService.class.getSimpleName(),"Watch Event :" + kind.name()); 

            if(kind.name().equals("OVERFLOW"))
            {
                continue;
            }
            if(kind.name().equals("ENTRY_MODIFY"))
            {
                Path concLog = (Path) event.context();

                logger.info(HeartBeatProducerService.class.getSimpleName(),"Modified File Name:" + concLog.getFileName()); 

                if(concLog.endsWith("current_status.txt"))
                {
                    logger.info(HeartBeatProducerService.class.getSimpleName(), "Reading Status");                  
                    readStatus();
                }
            }

        }

        boolean valid = key.reset();

        if ( !valid)
        {               
            logger.error(HeartBeatProducerService.class.getSimpleName(),"Key Unregistered"); 
        }
}





private void parse(String output)
{       
    // parse file contents
}

private void readStatus() {

    //read status and parse()

}
private void produce() 
{

    try {

        messageService.publish(TOPIC, content, PublishType.ASync);

    } catch (MessageException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

已经有一个链接向@Asynchronous标记(EJB 3.1 and NIO2: Monitoring the file system)解释相同的内容。但是我需要知道这种方法可能有什么问题。

1 个答案:

答案 0 :(得分:0)

您的监视方法需要在无限循环中运行。现在发生的事情是在

之后
     try {           
       key = watcher.take();
     }

您处理事件,然后watch()方法完成。尝试

的效果
     for(;;) {

在上述行之前,在有效性检查后结束for块。您在The Java Tutorials看到了示例吗?