0

我们想定期观察文件的变化,我们使用的是 jboss 7 。以下是我的代码片段。我在单例bean的postconstruct方法中初始化了watcher,并安排了一个方法来轮询watch事件。当我第一次修改文件时,我可以观察到更改,但是没有收到对文件的后续修改。谁能让我知道可能是什么问题

  @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 和 NIO2:监控文件系统)。但是我需要知道这种方法可能有什么问题。

4

1 回答 1

0

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

     try {           
       key = watcher.take();
     }

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

     for(;;) {

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

于 2013-11-20T13:27:48.727 回答