แสดงบทความที่มีป้ายกำกับ Spring Boot แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ Spring Boot แสดงบทความทั้งหมด

วันพฤหัสบดี, มิถุนายน 11, 2558

ทำ Spring Boot ให้สร้าง PID file ตอนรัน executable jar/war (Another way to config ApplicationPidFileWriter in Spring Boot)

เมื่อเราทำ executable jar หรือ executable war เวลารันไฟล์เหล่านี้ มันก็จะไม่ได้เปิดหรือแสดง console การที่จะหยุดการทำงานมัน เราจะต้องรู้ process id ของมัน
วิธีหา process id ของ executable jar/war ก็มีหลายวิธี
  • ใช้ command line `ps -ef|grep {ชื่อ file ของเรา}
  • เปิด log file ดู (ถ้า config พวก log4j เอาไว้) เวลา SpringApplication เริ่มทำงานมันก็จะบอกว่า process id ที่มันได้คือเลขอะไร i.e.: ...Starting FooApplication v0.1.0 on dahoba-laptop with PID 15840 ...
  • ใช้ Spring Boot Actuator ให้มันสร้าง pid ไฟล์ให้เรา
อ้างอิงตาม Spring Boot 1.2.3.RELEASE หัวข้อ Process monitoring เค้าบอกว่า ApplicationPidFileWriter สามารถจะเขียน application.pid ซึ่งมีเลข PID (process id) ของ application ที่รันอยู่ให้เราได้
วิธีการทำ
  • ใส่ dependency Spring Boot Actuator ก่อน
    reference #39
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>
Note: วิธี extend META-INF/spring.factories ยังทำให้ผมสับสน หรือใช้ SpringApplication.addListeners(…​) มันไม่ work แฮะ (ผมยังใช้ไม่เป็น :-( )
ผมทำแบบนี้ ใช้ได้เหมือนกัน
  • สร้างไฟล์ application.properties ขึ้นมาใน classpath i.e.: src/main/resources/application.properties
  • เพิ่ม line context.listener.classes นี้เข้าไป
  • ทางขวา value เป็น comma separate ใส่ class อื่นๆ ได้อีก
... 
context.listener.classes = org.springframework.boot.actuate.system.ApplicationPidFileWriter
... 
ที่นี้พอ execute jar/war ที่ package ใหม่หลังจาก ใส่ properties กับ dependency เข้าไปแล้ว มันก็จะสร้าง application.pid ให้เราแล้ว
มันจะถูกสร้างข้างๆไฟล์ jar/war ของเราเลย
$ cat applicaion.pid
19489%
ถ้าใช้ OS เป็น OSX หรือ linux ก็สามารถใช้คำสั่งแบบนี้เพื่อหยุด app ของเราได้เลย
$ kill `cat application.pid| awk '{print;}'`
หรือ
$ kill $(cat application.pid| awk '{print;}')

Happy Coding ^^
Written with StackEdit.

วันอังคาร, พฤษภาคม 26, 2558

เซตอัพ Maven, Spring Boot (spring-boot:run) กับ JRebel

   มีโปรเจคหนึ่งของผมเป็น java web application คุมด้วย maven แล้วผมอยากจะเรียกคำสั่ง mvn spring-boot:run พร้อมใช้งาน jrebel ไปด้วย

1. สร้างไฟล์ rebel.xml ก่อน เอาไว้ที่ src/main/resources
2. อ้างอิงตามเอกสารของ zeroturnaround เราใส่ content ของไฟล์ตามนี้
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zeroturnaround.com" xsi:schemaLocation="http://www.zeroturnaround.com http://www.zeroturnaround.com/alderaan/rebel-2_0.xsd">
  <classpath>
    <dir name="[absolute path to]/target/classes/"/>
  </classpath>
</application>

4. หากมี jar file ที่อยากให้ jrebel reload ให้ด้วย (jar library ที่อยากให้มัน reload คือ jar ที่ทำขึ้นมาใช้จากอีกโปรเจคหนึ่ง)
ก้อเพิ่มแบบนี้

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zeroturnaround.com" xsi:schemaLocation="http://www.zeroturnaround.com http://www.zeroturnaround.com/alderaan/rebel-2_0.xsd">
  <classpath>
    <dir name="[absolute path to]/src/main/resources/"/>
    <dir name="[absolute path to]/target/classes/"/>
    <jar name="[absolute path to a jar file].jar"></jar>
  </classpath>
</application>

path ไปยัง jar library ผมใช้วิธีชี้ไปที่ .m2/repository ,ที่ maven โหลดมา cache ในเครื่องเรา
เช่น /Users/dahoba/.m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar

5. เวลา execute mvn spring-boot:run เพิ่ม option แบบนี้

$ mvn spring-boot:run -Drun.agent=[/Users/dahoba/app/jrebel-nightly/jrebel]/jrebel.jar

- เพิ่ม -Drun.agent เข้าไป
- path ใน [ ] คือ absolute path ไปยัง jrebel.jar ที่ติดตั้งอยู่ในเครื่อง

เท่านี้เอง ใช้งาน jrebel ให้มันช่วย reload โปรเจค ที่เป็น spring-boot + maven ได้แล้ว