跳至主要内容

博文

目前显示的是标签为“Java”的博文

Requests Pipelining and Size Estimation

这两周刚好在看和 requests pipelining 有关的 Netty 代码,找到了挺有意思的文章: 1. [Pipelining requests](https://aphyr.com/posts/267-pipelining-requests) 2. [Estimate size of objects in OrderedMemoryAwareThreadPoolExecutor](https://aphyr.com/posts/269-reaching-200k-events-sec) 这里提到的 [Riemann](https://riemann.io/) 是一个分布式监控软件,用 Clojure 语言开发的(GitHub 代码库[地址](https://github.com/riemann))。

"The Java Version Almanac"

看了看从 Java 8 到 17 的 changes。 从 [这篇文章](https://ondro.inginea.eu/index.php/new-features-in-java-versions-since-java-8/) 发现了个有意思的 [Java 年鉴](https://javaalmanac.io/),非常用心地将历代 API 的变化都标注了出来。 仔细看了看,该网站的作者之一 Cay Horstmann 就是当年俺初学 Java 时读的 Core Java 的作者。

Dev's Ranting - Hibernate batch processing

Recently I have been working on a performance issue of a small application. It utilizes Hibernate to delete a lot of entities from database. Unfortunately the application died throwing a `OutOfMemoryError`. After discussing with my colleagues and reading some posts about how Hibernate works in batch mode, it appears to me that Hibernate's batch mode (`order_inserts` and/or `order_updates`) may played some role in this issue. Ref: * [Why is Hibernate batching / order_inserts / order_updates disabled by default](https://stackoverflow.com/questions/27755461/why-is-hibernate-batching-order-inserts-order-updates-disabled-by-default) * [Hibernate Batch Processing - Why you may not be using it. (Even if you think you are](https://abramsm.wordpress.com/2008/04/23/hibernate-batch-processing-why-you-may-not-be-using-it-even-if-you-think-you-are/)

Dev's Ranting - Map rendering in Freemarker

I’ve got a `Map ` and set it as model attribute in MVC Controller. Then I'd like to render this map in a loop in Freemarker view, e.g. ${key.prop} = ${myMap[key].fooProp} </#list> I have to loop through keys as they need to be displayed in a order. And surprisingly, it does NOT work!!! After searching dozens of stack overflow as well as Freemarker docs, I've learnt that: 1. If the map's key type is not `String`, you cannot utilise the sugar syntax like `map[key]`, actually you can but it would return null value... 2. It seems there is some way to re-configure the Freemarker to enable syntax like `map?api.get(key)`, but it’s not trivial. I tried a few changes but did not work (see the TLDR below). *TL;DR* 1. Need to set `api_builtin_enabled` to true, this can be done easily in our CustomFreemarkerConfiguration 2. After that it’s complaining "The value doesn't support ?api." and suggests to change another configuration on `object_wrapper` ...

Dev's Ranting - Collectors.toMap() in Java Stream

If you have a map and wants to convert into a different map, say mapping the value to something different, it would be sensible to utilise Java 8 Lambda, right? Map newMap = oldMap.entrySet() .stream() .collect(Map.Entry::getKey, entry -> someMapperFunc(entry.getValue())); Unfortunately the lambda does not ALWAYS Work, depends on whether there is any NULL value in the original map. As documented in JavaDoc, the intention of the `toMap()` is to "Returns a Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements." So why does it not want to accept null value? You'll have to be thorough and patient enough to read all the three overloaded versions of `toMap()` to be able to figure out that, `Collector.toMap()` relies on `Map.merge()`, which is designed to throw NPE when a NULL value is passed in. Who would expect that? Idiot library implementation ... Refer to: ...

Dev's Ranting - Hibernate Mapping Collections of Subtypes

Hibernate 作为ORM框架的中流砥柱被广泛地应用于企业应用程序开发中。从早期的XML配置到现在的Java Configuration,开发者早已习惯了使用各种 annotation 来配置 entity mapping。不过基础概念都是一样的。 为了展示用于一对多关系的多表映射,DZone上有一个[挺简单的例子](https://dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-one-to-many-using-annotations-1.html)。 @Entity @Table(name = "STUDENT") public class Student { private long studentId; private String studentName; private Set studentPhoneNumbers = new HashSet (0); @OneToMany(cascade = CascadeType.ALL) @JoinTable(name = "STUDENT_PHONE", joinColumns = { @JoinColumn(name = "STUDENT_ID") }, inverseJoinColumns = { @JoinColumn(name = "PHONE_ID") }) public Set getStudentPhoneNumbers() { return this.studentPhoneNumbers; } } 这两天遇到了一个定义映射关系的问题,与上面这个映射的区别在于,除了 Entity A 和 B 之间有 join table之外,Entity B 实际上是共享一个table的sub type,使用了 `@DiscriminatorColumn`...

Java 开发不可错过的 IntelliJ IDEA 2016.1 来了

手头在用的 IDE 更新到了 [IntelliJ 2016.1](https://blog.jetbrains.com/idea/2016/03/intellij-idea-2016-1-is-here/),发现多了不少好东东,包括: 1. 更好用的 VCS 合并界面 2. 可以直接 Alt + Enter 导入 static constants 3. 尊重 Gradle 的 SourceSets,直接导入为 IDEA Modules 4. 可以提示将 Guava API 转换为对应的 Java 8 API 5. 更丰富的语言支持,包括 Scala、JavaScript 和 TypeScript 等 6. 更高级的 Spring Framework 支持 7. 更漂亮的启动画面 …… ![Start splash of IDEA2016 Community Edition](http://blog.jetbrains.com/idea/files/2016/03/IntelliJIDEA_20161_blog@2x.png =600x400) 更多精彩请参考以上链接所指向的 INTELLIJ IDEA BLOG。

My CentOS in VirtualBox

1. Install JDK8 on CentOS - follow [this post](http://www.if-not-true-then-false.com/2014/install-oracle-java-8-on-fedora-centos-rhel/) 2. Download Scala from [Scala-lang.org](http://www.scala-lang.org/download/), and run the following commands tar xvf scala-2.11.5.tgz sudo mv ~/Downloads/scala-2.11.5 /usr/scala/ sudo ln -s /usr/scala/scala-2.11.5 /usr/scala/latest sudo alternatives --install /usr/bin/scala scala /usr/scala/latest/bin/scala 100 sudo alternatives --install /usr/bin/scalac scalac /usr/scala/latest/bin/scalac 100 sudo alternatives --install /usr/bin/scalap scalap /usr/scala/latest/bin/scalap 100

Learning Scala (Ref)

接触 Scala 有一段时间了,一开始的时候是因为 Scala 支持 strong type 的缘故感兴趣的,后来慢慢的看了 traits, pattern matching, generics 等语法特性,觉得这个语言夹杂了很多其它语言的特征。不过,就像 C++ 从 C 发源后加入了 OO、 Meta-Programming 等不同性质的语法特性之后整个语言的学习曲线陡增,Scala 对于 Java developers 来说可能也不算是容易上手的 JVM Language。 最近拜读了一篇讨论 Scala 优缺点的文章,颇有想法—— Scala: Sink or Swim Part 1 , Part 2 , Part 3 。之前玩了一阵子的 sbt,最近项目上在用 Gradle (这是用 Groovy 语言编写的一个具有 Ant 和 Maven 二者之长的构件系统),很能体会作者谈到的 sbt 的不爽……

Learning Scala

Scala 是一种多范式的编程语言,设计初衷是要整合面向对象编程和函数式编程的各种特性。Scala 运行于 Java 平台(Java 虚拟机),并兼容现有的 Java 程序。它也能运行于 Java ME, CLDC 上。 Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages, enabling Java and other programmers to be more productive. Code sizes are typically reduced by a factor of two to three when compared to an equivalent Java application. Scala 解释器类似于 Unix Shell,它允许用户以交互的方式输入表达式。在命令行中执行 `scala` 命令可以进入 Scala 解释器。 Welcome to Scala version 2.9.2 (Java HotSpot(TM) Client VM). Type in expressions to have them evaluated. Type :help for more information. 如果想要在解释器中跨行输入语句,只要一行行键入。如果输入到一行的结尾还没有结束,解释器将会在下一行自动回显一个 `|` 符号提示等待输入。用户如果发现一些错误而希望终止输入,可以连续键入两次回车取消当前的输入。如果需要离开解释器,只需要输入 `:quit` 或是 `:q` 即可。 有的时候,如果需要在命令行交互中得到更为详尽的提示信息,例如对过期函数调用的提示,或是对于未检查警告的解释,可以在启动 scala 的时候传入参数。 scala -unchecked -deprecation tag: scala

Java Shift Operation

在java中,位移符号 ( <<、 >> 和 >>> ) 是使用频率相对不太高的运算符号。不过,在一些特定的程序语境中,这些符号相当的有用,例如在JavaME程序中做数据的乘除运算,很多时候就以 << 和 >> 来书写代码。 Lucio童鞋发了几行关于处理RGB色值转换的代码和我讨论,大致的问题是将int类型左移64位和左移128位应该得到什么结果。结果我俩在这上头遇到了不解的谜,大意如下:     int a = (1 << 3), b = (1 << 8), c = (1 << 31);     int d = (1 << 32), e = (1 << 64), f = (1 << 128); 问这些数的值到底几何,很显然,第一行的a = 2^3 = 8,b = 2^8 = 256, c = 2^31 = 2147483648。但是第二行怎么算呢?java中的int类型是32bit,第二行中左移的位数都超过了这个范围,我俩都直接觉得 d == e == f == 0。However, 在运行过实际程序之后,结果出乎意料之外…… 实际执行的时候,d == e == f == 1。这究竟是为什么呢?这时候就要靠猛击JLS来寻找神谕了…… (还用不着直接骚扰Gosling大神) 。在 圣经 JLS的第十五节第十九条 中给出了明确的解释: If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance . It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x1f. The shift distance actually used is there...

64bit的Java

TSS上推荐了一篇名为 Java Performance in 64bit land 的博文。作者Roo大概和IBM J9有些什么渊源,讨论的话题很明了——在买车买性能,车主们赤裸裸地冲着多气缸奔去的年代,计算机的性能提升却成了一个“不好说”的问题。 按照『常理』来说,三核应该比双核来的强悍,64位系统应该较32位的小弟弟生猛,无奈软件和硬件之间的纠结让现实变得不那么美好……这问题搅活得像是奶粉奶牛鸡蛋饲料一样错综复杂…… 早在几年前C++社区的牛人Herb Sutter就已经发了 一篇振聋发聩的檄文 质问免费的性能提升还能撑多久,大意就是单个微处理器的处理速度增长地山河日下频率的提升已经逐渐接近物理极限,以往程序员们躺着也能依赖摩尔定律飙升程序运行速度的good old time眼看就要say goodbye了,往后的日子处理器大户们肯定会在多核超线程上动脑筋,而软件开发人员也必须要开发多线程的程序才能充分发挥硬件的功能了。 毫无疑问,首先经历变革的阵痛的肯定是C、C++程序员——部分原因当然归功于永远的指针。Java程序员们在面对64bit运算平台的时候,该认真考虑一下以往因为不需要担心所以不曾注意过的问题了……