「java监视」Java监视器怎么连接增加数据给数据库
今天给各位分享java监视的知识,其中也会对Java监视器怎么连接增加数据给数据库进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
Java 如何监控文件目录的变化
JavaSE 1.7提供了相关的API,去监视文件或者文件夹的变动,主要的API都在java.nio.file下面,其大概流程如下:
package org.xdemo.superutil.j2se.filewatch;
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;
/**
* 文件夹监控
*
* @author Goofy a href="";/a
* @Date 2015年7月3日 上午9:21:33
*/
public class WatchDir {
private final WatchService watcher;
private final MapWatchKey, Path keys;
private final boolean subDir;
/**
* 构造方法
*
* @param file
* 文件目录,不可以是文件
* @param subDir
* @throws Exception
*/
public WatchDir(File file, boolean subDir, FileActionCallback callback) throws Exception {
if (!file.isDirectory())
throw new Exception(file.getAbsolutePath() + "is not a directory!");
this.watcher = FileSystems.getDefault().newWatchService();
this.keys = new HashMapWatchKey, Path();
this.subDir = subDir;
Path dir = Paths.get(file.getAbsolutePath());
if (subDir) {
registerAll(dir);
} else {
register(dir);
}
processEvents(callback);
}
@SuppressWarnings("unchecked")
static T WatchEventT cast(WatchEvent? event) {
return (WatchEventT) event;
}
/**
* 观察指定的目录
*
* @param dir
* @throws IOException
*/
private void register(Path dir) throws IOException {
WatchKey key = dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
keys.put(key, dir);
}
/**
* 观察指定的目录,并且包括子目录
*/
private void registerAll(final Path start) throws IOException {
Files.walkFileTree(start, new SimpleFileVisitorPath() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
register(dir);
return FileVisitResult.CONTINUE;
}
});
}
/**
* 发生文件变化的回调函数
*/
@SuppressWarnings("rawtypes")
void processEvents(FileActionCallback callback) {
for (;;) {
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
Path dir = keys.get(key);
if (dir == null) {
System.err.println("操作未识别");
continue;
}
for (WatchEvent? event : key.pollEvents()) {
Kind kind = event.kind();
// 事件可能丢失或遗弃
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
// 目录内的变化可能是文件或者目录
WatchEventPath ev = cast(event);
Path name = ev.context();
Path child = dir.resolve(name);
File file = child.toFile();
if (kind.name().equals(FileAction.DELETE.getValue())) {
callback.delete(file);
} else if (kind.name().equals(FileAction.CREATE.getValue())) {
callback.create(file);
} else if (kind.name().equals(FileAction.MODIFY.getValue())) {
callback.modify(file);
} else {
continue;
}
// if directory is created, and watching recursively, then
// register it and its sub-directories
if (subDir (kind == StandardWatchEventKinds.ENTRY_CREATE)) {
try {
if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
registerAll(child);
}
} catch (IOException x) {
// ignore to keep sample readbale
}
}
}
boolean valid = key.reset();
if (!valid) {
// 移除不可访问的目录
// 因为有可能目录被移除,就会无法访问
keys.remove(key);
// 如果待监控的目录都不存在了,就中断执行
if (keys.isEmpty()) {
break;
}
}
}
}
}
求助!!!java的按钮监视器怎么写啊啊啊???
这个 真的没啥意思 给按钮添加一个监听 监听调用 button的另一个调用图片的构造方法 很方便\
package com.test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ButtonDemo {
URL url = this.getClass().getResource("poto.jpg");
public ButtonDemo() {
}
public void showMe() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
final JButton button;
button = new JButton("点击我!");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
button.setIcon(new ImageIcon(url));
}
});
frame.add(panel);
panel.add(button);
frame.setSize(600, 500);
frame.setVisible(true);
}
public static void main(String[] args) {
new ButtonDemo().showMe();
}
}
这是基本的 你看看吧
怎么监视java中函数被执行
请求响应一个线程中N个方法时,每隔5s,向前台推送当前执行哪个方法(未执行方法覆盖掉已执行方法名称存在一个变量中)。
package com.synnex.just.test;
public class SomeMethod {
private String currentMethodName;
public void m1(){
setCurrentMethodName("m1");
System.out.println("m1 executing .....");
}
public void m2(){
setCurrentMethodName("m2");
System.out.println("m2 executing .....");
}
public void m3(){
setCurrentMethodName("m3");
System.out.println("m3 executing .....");
}
public void m4(){
setCurrentMethodName("m4");
System.out.println("m4 executing .....");
}
public String getCurrentMethodName() {
return currentMethodName;
}
public void setCurrentMethodName(String currentMethodName) {
this.currentMethodName = currentMethodName;
}
}
--------------------------------------------------------------------------------------------------------------------------------
package com.synnex.just.test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class SomeThread extends Thread {
private SomeMethod m ;
public SomeThread(SomeMethod m){
this.m=m;
}
@Override
public void run() {
Class? classz=null;
try {
classz = Class.forName("com.synnex.just.test.SomeMethod");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method[] declaredMethods = classz.getDeclaredMethods();
while(true){
for(Method method : declaredMethods){
if(!method.getName().startsWith("m")){
continue;
}
try {
Thread.currentThread().sleep(2000);
method.invoke(m, null);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
-----------------------------------------------------------------------------------------------------------------------------
package com.synnex.just.test;
public class Main {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
SomeMethod m = new SomeMethod();
SomeThread t = new SomeThread(m);
t.start();
while(true){
Thread.sleep(5000);
System.out.println("Current method"+m.getCurrentMethodName());
}
}
}
Java类应用监控应该监控哪些
当有问题出现时,许多开发人员可能会比较盲目的用这些工具来试探性定位问题,而大多数情况下,这种试探会无功而返。因为这些分析工具主要是侧重Java单方面的分析,比如该系统调用第三方API,如果第三方API有问题,是无法监控到的。还有像文件、DB资源的访问也是是无法监控到的。
除了JAVA自带的监控工具外,我们尝试了第三方的监控工具透视宝,功能相对全面,且易操作。
在功能方面,透视宝都包括:查看执行最慢的10个元素,包括元素执行次数、持续时长和占用时长百分比;查看HTTP请求参数,包括请求的响应状态、链接页面、具体的请求参数及返回结果;查看代码执行堆栈的详细树状信息,包括每个方法的计算时间、总耗时和被调用的次数,您能直接看到特殊标识的最慢方法;查看涉及SQL语句的总耗时排序,包括SQL执行总耗时、执行次数和具体的查询语句;第三方API调用。
Java监控一个进程的执行状态
应用程序很高兴对所有这些活动一无所知。它只知道自己的虚拟地址空间。但是,如果当前在主存中的页面集(称为 驻留集)少于实际要使用的页面集(称为 工作集),应用程序的性能很快就会显著降低。(不幸的是,本文中您将看到,我们要讨论的工具常常交换使用这两个术语,尽管它们指的是完全不同的事物。) Task Manager 和 PerfMon 我们首先考察两种最常见的工具:Task Manager 和 PerfMon。这两个工具都随 Windows 一起提供,因此由此起步比较容易。 Task Manager Task Manager 是一种非常见的 Windows 进程监控程序。您可以通过熟悉的 Ctrl-Alt-Delete 组合键来启动它,或者右击任务栏。Processes 选项卡显示了最详细的信息,如图 2 所示。 图 2. Task Manager 进程选项卡 图 2 中显示的列已经通过选择 View -- Select Columns 作了调整。有些列标题非常含糊,但可以在 Task Manager 帮助中找到各列的定义。和进程内存使用情况关系最密切的计数器包括: Mem Usage(内存使用):在线帮助将其称为进程的工作集(尽管很多人称之为驻留集)——当前在主存中的页面集。但是这个数值包含能够和其他进程共享的页面,因此要注意避免重复计算。比方说,如果要计算共享同一个 DLL 的两个进程的总内存占用情况,不能简单地把“内存使用”值相加。 Peak Mem Usage(内存使用高峰值):进程启动以来 Mem Usage(内存使用)字段的最大值。 Page Faults(页面错误):进程启动以来要访问的页面不在主存中的总次数。 VM Size(虚拟内存大小):联机帮助将其称为“分配给进程私有虚拟内存总数。”更确切地说,这是进程所 提交的内存。如果进程保留内存而没有提交,那么该值就与总地址空间的大小有很大的差别。 虽然 Windows 文档将 Mem Usage(内存使用)称为工作集,但在该上下文中,它实际上指的是很多人所说的驻留集(resident set),明白这一点很重要。您可以在 Memory Management Reference 术语表(请参阅 参考资料)中找到这些术语的定义。 工作集 更通常的含义指的是一个逻辑概念,即在某一点上为了避免分页操作,进程需要驻留在内存中的那些页面。 PerfMon 随 Windows 一起提供的另一种 Microsoft 工具是 PerfMon,它监控各种各样的计数器,从打印队列到电话。PerfMon 通常在系统路径中,因此可以在命令行中输入 perfmon 来启动它。这个工具的优点是以图形化的方式显示计数器,很容易看到计数器随时间的变化情况。 请在 PerfMon 窗口上方的工具栏中单击 + 按钮,这样会打开一个对话框让您选择要监控的计数器,如图 3a 所示。计数器按照 性能对象分成不同的类别。与内存使用关系最密切的两个类是 Memory 和 Process。选中计数器然后单击 Explain 按钮,就可以看到计数器的定义。说明出现在主对话框下方弹出的单独的窗口中。
java监视的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java监视器怎么连接增加数据给数据库、java监视的信息别忘了在本站进行查找喔。
发布于:2022-11-22,除非注明,否则均为
原创文章,转载请注明出处。