在linux里面怎么监控一个java程序的运行

在linux里面怎么监控一个java程序的运行,第1张

可以写一个脚本进行监控

可以用  ps   top  jstack 等命令来查看和监控进程。

参考1

参考2

我是海腾数据中心的技术小哥,很高兴为您解答问题。

java 的WatchService 类提供了一种方式可以检查

try

{

WatchService watchService = FileSystems.getDefault()

.newWatchService()

Path path = Paths.get(pathName)

// 注册监听器

path.register(watchService,

StandardWatchEventKinds.ENTRY_CREATE,

StandardWatchEventKinds.ENTRY_DELETE)

while (true)

{

// 阻塞方式,消费文件更改事件

List<WatchEvent<?>>watchEvents = watchService.take()

.pollEvents()

for (WatchEvent<?>watchEvent : watchEvents)

{

System.out.printf("[%s]文件发生了[%s]事件。%n", watchEvent

.context(), watchEvent.kind())

}

}

}

catch (Exception e)

{

}

JDK 7 的nio2 WatchService可以监听文件系统。

Oracle官方教程链接 http://docs.oracle.com/javase/tutorial/essential/io/notification.html

样例代码:

import static java.nio.file.StandardWatchEventKinds.*

Path path = Paths.get("/home")

WatchService watchService = FileSystems.getDefault().newWatchService()

WatchKey watchKey = path.register(watchService,ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY)

/*

private boolean notDone = true

while(notDone){

    try{

         WatchKey watchKey = watchService.poll(60,TimeUnit.SECONDS)

         List<WatchEvent.Kind<?>> events = watchKey.pollEvents()

         for(WatchEvent event : events){

            //WatchKey watchable returns the calling Path object of Path.register

             Path watchedPath = (Path) watchKey.watchable()

             //returns the event type

             StandardWatchEventKinds eventKind = event.kind()

             //returns the context of the event

             Path target = (Path)event.context()

         }

         if(!watchKey.reset()){

            ...handle situation no longer valid

         }

     }catch(InterruptedException e){

            Thread.currentThread().interrupt()

     }

}

*/


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7224753.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-03
下一篇2023-04-03

发表评论

登录后才能评论

评论列表(0条)

    保存