2021年2月24日 星期三

COMPILING YOUR QUARKUS APPLICATIONS TO NATIVE EXECUTABLES

https://access.redhat.com/documentation/en-us/red_hat_build_of_quarkus/1.7/html-single/compiling_your_quarkus_applications_to_native_executables/index

Redhat官方的quarkus demo code

https://github.com/redhat-developer-demos/quarkus-tutorial

(1)
sudo yum install maven

備註:需要3.5.3以上版本
可用mvn --version指令查看目前版本

RHEL更新Maven方式:
https://computingforgeeks.com/installing-latest-apache-maven-on-centos-fedora/
需先sudo yum remove maven


(2)
git clone https://github.com/redhat-developer-demos/quarkus-tutorial.git

(3)
cd quarkus-tutorial

(4)
export TUTORIAL_HOME='pwd'
export QUARKUS_VERSION=0.21.2

(5)
mvn "io.quarkus:quarkus-maven-plugin:$QUARKUS_VERSION:create" \
  -DprojectGroupId="com.example" \
  -DprojectArtifactId="fruits-app" \
  -DprojectVersion="1.0-SNAPSHOT" \
  -DclassName="FruitResource" \
  -Dpath="fruit"

(6)
確認檔案已產生
cd fruits-app
ls -al

(7)
已開發模式啟動(具備Hot Reload功能)
./mvnw compile quarkus:dev

(8)
啟動後會看到以下訊息
2021-02-24 21:10:49,700 WARN  [io.qua.dep.QuarkusAugmentor] (main) Using Java versions older than 11 to build Quarkus applications is deprecated and will be disallowed in a future release!
2021-02-24 21:10:52,875 INFO  [io.quarkus] (Quarkus Main Thread) fruits-app 1.0-SNAPSHOT on JVM (powered by Quarkus 1.12.0.Final) started in 0.700s. Listening on: http://localhost:8080
2021-02-24 21:10:52,882 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2021-02-24 21:10:52,882 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy]


=======以下是改用Native模式編譯
(9)
oc new-project quarkustutorial

(10)
編譯成native executable(需安裝GraalVM)
./mvnw package -DskipTests -Pnative

Windows平台編譯問題請參考以下文章
https://quarkus.io/guides/building-native-image#producing-a-native-executable

如果沒安裝GraalVM,使用container runtime(預設是使用docker,可用下方指令指定)
./mvnw package -DskipTests -Pnative -Dquarkus.native.container-build=true
https://quarkus.io/guides/building-native-image#quarkus-native-pkg-native-config_quarkus.native.container-build


https://quarkus.io/guides/building-native-image
By default Quarkus automatically detects the container runtime. If you want to explicitely select the container runtime, you can do it with:
# Docker
./mvnw package -Pnative -Dquarkus.native.container-build=true -Dquarkus.native.container-runtime=docker

# Podman
./mvnw package -Pnative -Dquarkus.native.container-build=true -Dquarkus.native.container-runtime=podman

2021年2月23日 星期二

Quarkus開發模式的Hot reload

指令
./mvnw compile quarkus:dev

log檔
2021-02-23 01:52:03,239 INFO  [io.qua.dep.dev.RuntimeUpdatesProcessor] (vert.x-worker-thread-5) Changed source files detected, recompiling [/home/javadev/fruits-app/src/main/java/com/example/FruitResource.java]
2021-02-23 01:52:04,184 INFO  [io.qua.dep.dev.RuntimeUpdatesProcessor] (vert.x-worker-thread-5) Application restart not required, replacing classes via instrumentation
2021-02-23 01:52:04,258 INFO  [io.qua.dep.dev.RuntimeUpdatesProcessor] (vert.x-worker-thread-5) Hot replace performed via instrumentation, no restart needed, total time: 1.020s

2021年2月5日 星期五

OWASP Encode forHtml和forJava的差異

Encode.forHtml:
可防範XSS(Cross-Site Scripting),把「<」、「&」、「>」、「"」、「""」這類的特殊字元取代成「&lt;」、「&amp;」、「&gt;」、「&#34;」、「&#34;&#34;」。
但是不會取代「換行字元」,無法防範Log Forging (日誌偽造),防範Log Forging一定要取代「換行字元」

Encode.forJava:
可防範Log Forging (日誌偽造),把「換行字元」取代

Source Code:
https://github.com/OWASP/owasp-java-encoder/blob/main/core/src/main/java/org/owasp/encoder/Encode.java

官網:
https://owasp.org/owasp-java-encoder/
目前最新版是1.2.3,可用在Java 5以上的專案。

encoder-jsp-1.2.3.jar是JSP tag方式使用,jsp檔需要聲明taglib如下
<%@taglib prefix="e" uri="https://www.owasp.org/index.php/OWASP_Java_Encoder_Project" %>

encoder-1.2.3.jar是直接透過Method方式呼叫使用,可以用於一般java程式。

REF:
https://owasp-top-10-proactive-controls-2018.readthedocs.io/en/latest/c4-encode-escape-data.html


2021年1月24日 星期日

HikariCP Example

Java 6
https://github.com/imrexhuang/HikariCPClientExample/tree/master/ForJava6

Java 8+
https://github.com/imrexhuang/HikariCPClientExample/tree/master/ForJava8%2B

HikariCP
https://github.com/brettwooldridge/HikariCP#configuration-knobs-baby

HikariCP Maven:
https://search.maven.org/search?q=com.zaxxer.hikaricp

作者:Brett Wooldridge
https://github.com/brettwooldridge

作者為何要開發HikariCP的專訪
https://dzone.com/articles/jooq-tuesdays-what-it-takes-to-write-the-fastest-java-connection-pool

【追光者系列】HikariCP默认配置


利用SQL Server的sp_who指令查看
沒有設定setMinimumIdle和setMaximumPoolSize時,的確預設是建立10個Connection


超過預設10個Connection會出現以下錯誤訊息,無法再用HikariDataSource去getConnection導致timeout:

java.sql.SQLTimeoutException: Timeout after 30004ms of waiting for a connection.
    at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:233)
    at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:183)
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:93)
    at HikariCPDataSource.getConnection(HikariCPDataSource.java:33)
    at SQLServerPOC.main(SQLServerPOC.java:25)