Wednesday, 25 September 2013

Issue invoking WatchService inside a singleton bean

Issue invoking WatchService inside a singleton bean

we wanted to watch a file periodically for changes, we are using jboss 7 .
Following is my code snippet. I initialized the watcher in the
postconstruct method of singleton bean and scheduled a method to poll
watch events. I could observe the changes when i modify the file very
first time, however the subsequent modifications to the file are not
recieved . Can anyone please let me know what could be the issue
@Startup
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
@Interceptors(NonThrowingPostConstructInterceptor.class)
@Singleton
@Service
@LocalBinding(jndiBinding=IConciergeHeartBeatProducerService.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(ConciergeHeartBeatProducerService.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();
}
}
}
There is already a link explaining the same with @Asynchronous tag (EJB
3.1 and NIO2: Monitoring the file system) . however I need to know what
could be wrong in this approach.

No comments:

Post a Comment