@POST @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) @Path("/start") public Response startMemoryMonitor() { timeMonitor = null; timeMonitor = new Timer(); TimerTask scanTask = new TimerTask() { IMemoryDAO mdao = DAOFactory.getInstance().getMemoryDAO(); MemoryPO memDetails = new MemoryPO(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); @Override public void run() { /*Runtime rt=Runtime.getRuntime(); long freeVMemory = rt.freeMemory()/1024; long totalVMemory = rt.totalMemory()/1024; long usedVMemory = totalVMemory-freeVMemory;*/ OperatingSystemMXBean bean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); long maxMemory = bean.getTotalPhysicalMemorySize() / 1024; long freeVMemory = bean.getFreePhysicalMemorySize() / 1024; long usedVMemory = maxMemory - freeVMemory; long freeSpace = 0; long usedSpace = 0; for (java.nio.file.Path root : FileSystems.getDefault().getRootDirectories()) { try { FileStore store = Files.getFileStore(root); freeSpace = store.getUnallocatedSpace() / 1024; usedSpace = ((store.getTotalSpace() / 1024) - (store.getUnallocatedSpace() / 1024)); } catch (FileSystemException e) { Log.trace(e.toString()); } catch (IOException e) { Log.trace(e.toString()); } } memDetails.setUsedVolatile(usedVMemory); memDetails.setRemainingVolatile(freeVMemory); memDetails.setUsedPersistent(usedSpace); memDetails.setRemainingPersistent(freeSpace); Calendar calendar = Calendar.getInstance(); memDetails.setCreatedAt(df.format(calendar.getTime())); mdao.insertMemoryStats(memDetails); } }; timeMonitor.schedule(scanTask, 0, 60000); return ok(); }
@POST @Path("/stop") public void stopMemoryMonitor() { timeMonitor.cancel(); Log.trace("Memory Monitor Stopped"); }