javaAgent初探

agent 初探 premain 命令式 在main方法之前执行 agentmain 以Attach的方式载入,在Java程序启动后执行,1.6提供 public interface Instrumentation { /** * 添加Transformer(转换器) * ClassFileTransformer类是一个接口,通常用户只需实现这个接口的 byte[] transform()方法即可; * transform这个方法会返回一个已经转换过的对象的byte[]数组 * @param transformer 拦截器 * @return canRetransform 是否能重新转换 */ void addTransformer(ClassFileTransformer transformer, boolean canRetransform); /** * 重新触发类加载, * 该方法可以修改方法体、常量池和属性值,但不能新增、删除、重命名属性或方法,也不能修改方法的签名 * @param classes Class对象 * @throws UnmodifiableClassException 异常 */ void retransformClasses(Class<?>... classes) throws UnmodifiableClassException; /** * 直接替换类的定义 * 重新转换某个对象,并已一个新的class格式,进行转化。 * 该方法可以修改方法体、常量池和属性值,但不能新增、删除、重命名属性或方法,也不能修改方法的签名 * @param definitions ClassDefinition对象[Class定义对象] * @throws ClassNotFoundException,UnmodifiableClassException 异常 */ void redefineClasses(ClassDefinition... definitions)throws ClassNotFoundException, UnmodifiableClassException; /** * 获取当前被JVM加载的所有类对象 * @return Class[] class数组 */ Class[] getAllLoadedClasses(); } premain方法介绍 public class AgentWithTransFormer { public static void premain(String agentOps, Instrumentation instrumentation) { System.out.println("premian start 。。"); System.out.println("agentOps = " + agentOps); instrumentation.addTransformer(new DefineTransformer(),true); } /** * */ static class DefineTransformer implements ClassFileTransformer { @Override public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { System.out.println("premain load class :" + className); return classfileBuffer; } } } premain方法会拦截大部分类(不包含有些系统类),实现类DefineTransformer需要实现transform方法,比如我们可以拦截某个类,将某个类的class文件修改,可以整个文件替换或者利用javassist自定义添加或者删除方法。 ...

September 28, 2022 · 4 min

reactor模型了解

reactor模型 特点 事件驱动 可以处理一个或多个输入源 通过多路复用将请求的事件分发给对应的处理器处理 根据大神Doug Lea 在 《Scalable IO in Java 》中的介绍,Reacotr模型主要分为三个角色 ...

February 10, 2022 · 3 min

netty利用websocket协议开发服务端

介绍websocket协议 WebSocket是HTML5开始提供的一种浏览器与服务器间进行全双工通信的网络技术,WebSocket通信协议于2011年被IETF定为 标准RFC6455,WebSocket API被W3C定为标准。 ...

February 9, 2022 · 4 min

使用netty作为http文件服务

代码 HttpFileServer package http; import echo.server.EchoServer; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.http.*; import io.netty.handler.stream.ChunkedWriteHandler; /** * @author ggBall * @version 1.0.0 * @ClassName HttpFileServer.java * @Description TODO * @createTime 2022年02月08日 09:41:00 */ public class HttpFileServer { private static final String default_url = "/src/main/java/http/file/netty/"; public void run(final int port , final String url) throws InterruptedException { EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(boss,worker) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { /** * This method will be called once the {@link Channel} was registered. After the method returns this instance * will be removed from the {@link ChannelPipeline} of the {@link Channel}. * * @param ch the {@link Channel} which was registered. * @throws Exception is thrown if an error occurs. In that case the {@link Channel} will be closed. */ protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); // 请求消息解码器 ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));// 目的是将多个消息转换为单一的request或者response对象 ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());//响应解码器 ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());//目的是支持异步大文件传输() ch.pipeline().addLast("fileServerHandler", new HttpFileServerHandler(url));// 业务逻辑 } }); ChannelFuture future = bootstrap.bind("127.0.0.1", port).sync(); System.out.println("http文件服务器,地址:http://127.0.0.1:" + port + url); future.channel().closeFuture().sync(); } finally { boss.shutdownGracefully(); worker.shutdownGracefully(); } } public static void main(String[] args) throws Exception { int port = 8080; if (args.length > 0) { try { port = Integer.parseInt(args[0]); } catch (NumberFormatException e) { e.printStackTrace(); } } String url = default_url; if (args.length > 1) url = args[1]; new HttpFileServer().run(port, url); } } HttpFileServerHandler ...

February 8, 2022 · 3 min

io入门了解

Java1.4之前的早期版本,Java对I/O的支持并不完善,开发人员在开发高性能I/O程序的时候,会面临一些巨大的挑战和困难,主要问题如下: ...

January 15, 2022 · 1 min
总访问量 总访客数