Fixed possible race condition in the metadata listener  by adding read
write locks
junit tests pass