温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用源码分析Play Framework hotswap

发布时间:2021-10-29 10:38:55 来源:亿速云 阅读:116 作者:柒染 栏目:编程语言

这期内容当中小编将会给大家带来有关怎么用源码分析Play Framework hotswap,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

Play Framework hotswap的卖点就在于 hot swap,正如它自己宣称的:

reach your maximum productivity。play! 允许开发人员修改java文件,保存,然后刷新浏览器,立马可以看到效果。不需要编译,也不需要重启服务器

Java 要想实现动态更新 class 文件,不外乎两种手段:替换 classloader、替换 JVM。因为替换 JVM 引起的开销更大,需要维护 JVM 的堆、栈等运行信息,所以 hot swap 通常是选择替换 classloader。比如 grails 里面就是选择替换 classloader,它会自己维护一个线程,定期轮询源文件是否发生修改,以替换原来的 classloader。那么 play! 宣称的 hot swap 又是怎么实现的呢?

让我们来看看play! 的内部流程:

1. play! 使用了 Apache Mina 作为底层的 http server,然后使用了自己关于 Mina IoHandler 接口的实现—— HttpHandler

2. 当浏览器发起一个 request:

2.1 Mina Server 生成一个 Mina Request,转发给 HttpHandler 的 messageReceived 方法

2.2 play! 解析 Mina Request 和 Mina Session,包装成自己的 Request 对象

Request request = parseRequest(minaRequest, session);

2.3 play! 检测 Route 文件修改情况,根据 Route 配置信息将 Route/Action 的信息赋给 Request 对象

Router.detectChanges();  Router.route(request);

2.4 play! 根据当前配置的开发模式来采用不同的策略调用 Action 来理 Request

if (Play.mode == Play.Mode.DEV) {  Invoker.invokeInThread(new MinaInvocation(session, minaRequest, minaResponse, request, response));  } else {  Invoker.invoke(new MinaInvocation(session, minaRequest, minaResponse, request, response));  }

2.5 如果 play! 当前是 DEV 模式,invokeInThread方法会让 invocation 对象代理 run() 方法

public void run() {  try {  before();  execute();  after();  } catch (Throwable e) {  onException(e);  } finally {  _finally();  }  }

咱们来看看 before() 方法:

public static void before() {  Thread.currentThread().setContextClassLoader(Play.classloader);  if(!Play.id.equals("test")) {  Play.detectChanges();  if (!Play.started) {  Play.start();  }  }  //  }

在 Play 类的 detectChanges() 方法里面,有这么一句:

classloader.detectChanges();

哈哈,play! 修改源文件后,刷新浏览器即见效的奥秘就在这里了。再进去看看 play! 自定义 classloader 的 detectChanges() 方法:

public void detectChanges() {  // Now check for file modification  List<ApplicationClass> modifieds = new ArrayList<ApplicationClass>();  for (ApplicationClass applicationClass : Play.classes.all()) {  if (applicationClass.timestamp < applicationClass.javaFile.lastModified()) {  applicationClass.refresh();  modifieds.add(applicationClass);  }  }  List<ClassDefinition> newDefinitions = new ArrayList<ClassDefinition>();  Map<Class, Integer> annotationsHashes = new HashMap<Class, Integer>();  for (ApplicationClass applicationClass : modifieds) {  annotationsHashes.put(applicationClass.javaClass, computeAnnotationsHash(applicationClass.javaClass));  if (applicationClass.compile() == null) {  Play.classes.classes.remove(applicationClass.name);  } else {  applicationClass.enhance();  BytecodeCache.cacheBytecode(applicationClass.enhancedByteCode, applicationClass.name, applicationClass.javaSource);  newDefinitions.add(new ClassDefinition(applicationClass.javaClass, applicationClass.enhancedByteCode));  }  }  try {  HotswapAgent.reload(newDefinitions.toArray(new ClassDefinition[newDefinitions.size()]));  } catch (ClassNotFoundException e) {  throw new UnexpectedException(e);  } catch (UnmodifiableClassException e) {  throw new UnexpectedException(e);  }  // Check new annotations  for (Class clazz : annotationsHashes.keySet()) {  if (annotationsHashes.get(clazz) != computeAnnotationsHash(clazz)) {  throw new RuntimeException("Annotations change !");  }  }  // Now check if there is new classes or removed classes  int hash = computePathHash();  if (hash != this.pathHash) {  // Remove class for deleted files !!  for (ApplicationClass applicationClass : Play.classes.all()) {  if (!applicationClass.javaFile.exists()) {  Play.classes.classes.remove(applicationClass.name);  }  if(applicationClass.name.contains("$")) {  Play.classes.classes.remove(applicationClass.name);  }  }  throw new RuntimeException("Path has changed");  }  }

HotswapAgent类的 reload 方法如下:

public static void reload(ClassDefinition definitions) throws UnmodifiableClassException, ClassNotFoundException {  instrumentation.redefineClasses(definitions);  }

读到这里,也就弄清楚了 play! 怎么实现 hot swap 的原理了,还是调用java.lang.instrument目录下的类和方法来实现的 hot swap。不存在魔法,play! 还是选择了替换 classloader,只不过这个替换动作发生在处理 http request 的时候,于是开发人员用起来就是“刷新浏览器就可以看见效果了”。

上述就是小编为大家分享的怎么用源码分析Play Framework hotswap了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI