かんがるーさんの日記

最近自分が興味をもったものを調べた時の手順等を書いています。今は Spring Boot をいじっています。

Spring Boot 1.2.x の Web アプリを 1.3.x へバージョンアップする ( その6 )( JRebel と devtools をどちらも入れた時の動作を確認する )

概要

Spring Boot 1.2.x の Web アプリを 1.3.x へバージョンアップする ( その5 )( 1.2 → 1.3 で実装方法が変更された箇所を修正する ) の続きです。

  • 今回の手順で確認できるのは以下の内容です。
    • JRebel と devtools をどちらも入れた時の動作を確認する

参照したサイト・書籍

  1. 20. Developer tools
    http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html

  2. DevTools in Spring Boot 1.3
    https://spring.io/blog/2015/06/17/devtools-in-spring-boot-1-3

  3. spring-boot-devtoolsで開発効率上げようぜ、的な。 (Spring Boot 1.3)
    http://qiita.com/IsaoTakahashi/items/f99d5f761d1d4190860d

  4. DevToolsPropertyDefaultsPostProcessor.java
    https://github.com/spring-projects/spring-boot/blob/v1.3.5.RELEASE/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsPropertyDefaultsPostProcessor.java

目次

  1. build.gradle を変更する
  2. application-develop.properties, application-unittest.properties, application-product.properties を修正する
  3. JRebel と devtools を併用している状態でそれぞれが正常に動作することを確認する
  4. spring.thymeleaf.cache が IDE で起動した時には false に、java -jar で起動した時には true になることを確認する
  5. devtools を外して java -jar で起動すると spring.thymeleaf.cache が true になるか確認する
  6. 次回は。。。

手順

build.gradle を変更する

build.gradle を以下のように変更します。

dependencies {
    def jdbcDriver = "org.postgresql:postgresql:9.4.1208"

    // dependency-management-plugin によりバージョン番号が自動で設定されるもの
    // Appendix A. Dependency versions ( http://docs.spring.io/platform/docs/current/reference/htmlsingle/#appendix-dependency-versions ) 参照
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity4")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-velocity")
    compile("org.springframework.boot:spring-boot-starter-mail")
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("org.springframework.boot:spring-boot-starter-redis")
    compile("org.springframework.boot:spring-boot-starter-amqp")
    compile("org.springframework.boot:spring-boot-devtools")
    compile("org.springframework.session:spring-session")
    compile("org.codehaus.janino:janino")
    compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
    compile("com.fasterxml.jackson.dataformat:jackson-dataformat-xml")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    testCompile("org.springframework.security:spring-security-test")
    testCompile("org.yaml:snakeyaml")
    testCompile("org.spockframework:spock-core") {
        exclude module: "groovy-all"
    }
    testCompile("org.spockframework:spock-spring") {
        exclude module: "groovy-all"
    }

    // dependency-management-plugin によりバージョン番号が自動で設定されないもの、あるいは最新バージョンを指定したいもの
    runtime("${jdbcDriver}")
    compile("org.seasar.doma:doma:2.8.0")
    compile("org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16")
    compile("org.apache.commons:commons-lang3:3.4")
    compile("org.projectlombok:lombok:1.16.8")
    compile("com.google.guava:guava:19.0")
    compile("org.simpleframework:simple-xml:2.7.1")
    compile("com.univocity:univocity-parsers:2.1.1")
    compile("org.thymeleaf.extras:thymeleaf-extras-java8time:2.1.0.RELEASE")
    testCompile("org.dbunit:dbunit:2.5.1")
    testCompile("com.icegreen:greenmail:1.5.0")
    testCompile("org.assertj:assertj-core:3.4.1")
    testCompile("com.jayway.jsonpath:json-path:2.2.0")
    testCompile("org.jmockit:jmockit:1.23")

    // for Doma-Gen
    domaGenRuntime("org.seasar.doma:doma-gen:2.8.0")
    domaGenRuntime("${jdbcDriver}")
}
  • compile("org.springframework.boot:spring-boot-devtools") を追加します。

変更後、Gradle projects View の左上にある「Refresh all Gradle projects」ボタンをクリックして更新します。

application-develop.properties, application-unittest.properties, application-product.properties を修正する

cache の ON/OFF を設定している部分をコメントアウトします。

■application-develop.properties

spring.datasource.url=jdbc:log4jdbc:postgresql://localhost/ksbylending
spring.datasource.username=ksbylending_user
spring.datasource.password=xxxxxxxx
spring.datasource.driverClassName=net.sf.log4jdbc.sql.jdbcapi.DriverSpy

spring.mail.host=localhost
spring.mail.port=25

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672

spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=localhost:6381,localhost:6382,localhost:6383

#spring.messages.cache-seconds=0
#spring.thymeleaf.cache=false
#spring.velocity.cache=false
  • 以下の3項目をコメントアウトします。
    • spring.messages.cache-seconds=0
    • spring.thymeleaf.cache=false
    • spring.velocity.cache=false

■application-unittest.properties

spring.datasource.url=jdbc:postgresql://localhost/ksbylending
spring.datasource.username=ksbylending_user
spring.datasource.password=xxxxxxxx
spring.datasource.driverClassName=org.postgresql.Driver

spring.mail.host=localhost
spring.mail.port=25

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672

spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=localhost:6381,localhost:6382,localhost:6383

#spring.thymeleaf.cache=true

■application-product.properties

server.tomcat.basedir=C:/webapps/ksbysample-webapp-lending
logging.file=${server.tomcat.basedir}/logs/ksbysample-webapp-lending.log

spring.datasource.url=jdbc:postgresql://localhost/ksbylending
spring.datasource.username=ksbylending_user
spring.datasource.password=xxxxxxxx
spring.datasource.driverClassName=org.postgresql.Driver

spring.mail.host=localhost
spring.mail.port=25

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672

spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=localhost:6381,localhost:6382,localhost:6383

#spring.thymeleaf.cache=true

JRebel と devtools を併用している状態でそれぞれが正常に動作することを確認する

まずは devtools で Automatic Restart が動作することを確認します。

最初に IntelliJ IDEA の画面右上の「Select Run/Debug Configuration」で「Application」を選択した後、「Run」ボタンをクリックして Tomcat を起動します ( JRebel を使用せずに Tomcat が起動します )。

f:id:ksby:20160526004941p:plain

f:id:ksby:20160526010915p:plain

src/main/java/ksbysample/webapp/lending の下に以下の SampleController.java を作成します。

package ksbysample.webapp.lending;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sample")
public class SampleController {

    @RequestMapping
    @ResponseBody
    public String index() {
        return "sample";
    }

}

Ctrl+F9 を押して Compile します。Compile が完了すると devtools の Automatic Restart が実行されました。通常起動時は 18秒ほどかかりましたが、Automatic Restart だと 7秒程度で再起動が完了します。

f:id:ksby:20160526011327p:plain

f:id:ksby:20160526011141p:plain

http://localhost:8080/sample/ にアクセスすると "sample" の文字が表示されることが確認できます。JRebel を使用していないのに新規に 作成した Controller クラスが反映されるとは便利ですね。

f:id:ksby:20160526011810p:plain

Tomcat を停止します。

次に JRebel が動作することを確認します。

IntelliJ IDEA の画面右上の「Select Run/Debug Configuration」で「Application」を選択した後、「Run with JRebel」ボタンをクリックして Tomcat を起動します。

f:id:ksby:20160526012218p:plain

f:id:ksby:20160526013027p:plain

src/main/java/ksbysample/webapp/lending の下の SampleController.java を以下のように変更します。

package ksbysample.webapp.lending;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sample")
public class SampleController {

    @RequestMapping
    @ResponseBody
    public String index() {
        return "test";
    }

}
  • return "sample";return "test"; へ変更します。

Ctrl+F9 を押して Compile します。Compile が完了すると JRebel: Reloading class 'ksbysample.webapp.lending.SampleController'. のメッセージが出力されて JRebel が class をリロードしました。また devtools の Automatic Restart が実行されている時のようなログは出力されませんでした。JRebel は変更のあったクラスだけをリロードしているようなので、リロードにかかる時間はこちらの方が速いですね。

f:id:ksby:20160526013208p:plain

http://localhost:8080/sample/ にアクセスすると "test" の文字が表示されることが確認できます。

f:id:ksby:20160526013635p:plain

devtools が入っていても JRebel の動作には影響ないようです。

spring.thymeleaf.cache が IDE で起動した時には false に、java -jar で起動した時には true になることを確認する

20. Developer tools を見ると java -jar で起動したら devtools は自動的に disable になるらしいので、spring.thymeleaf.cache が true になるのか確認したいと思います。

spring.thymeleaf.cache の値を確認できるよう、src/main/java/ksbysample/webapp/lending の下の SampleController.java を以下のように変更します。

package ksbysample.webapp.lending;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sample")
public class SampleController {

    @Value("${spring.thymeleaf.cache:}")
    private String springThymeleafCache;

    @RequestMapping
    @ResponseBody
    public String index() {
        return "spring.thymeleaf.cache = " + springThymeleafCache;
    }

}

まずは IntelliJ IDEA から Tomcat を起動した場合には false と表示されることを確認します。

IntelliJ IDEA の画面右上の「Select Run/Debug Configuration」で「Application」を選択した後、「Run」ボタンをクリックして Tomcat を起動します ( JRebel を使用せずに Tomcat が起動します )。

http://localhost:8080/sample/ にアクセスすると spring.thymeleaf.cache = false と表示されることが確認できます。

f:id:ksby:20160528100207p:plain

Tomcat を停止します。

JRebel で Tomcat を起動した時も false と表示されるのか確認してみます。

IntelliJ IDEA の画面右上の「Select Run/Debug Configuration」で「Application」を選択した後、「Run with JRebel」ボタンをクリックして Tomcat を起動します。

http://localhost:8080/sample/ にアクセスすると同じように spring.thymeleaf.cache = false と表示されることが確認できます。

f:id:ksby:20160528101027p:plain

Tomcat を停止します。

今度は jar ファイルを作成して java -jar で起動した時に true と表示されることを確認します。

Gradle projects View から clean タスクの実行→「Rebuild Project」メニューの実行→build タスクの実行を行い、ksbysample-webapp-lending-1.1.0-RELEASE.jar を作成します。

f:id:ksby:20160528101703p:plain

ksbysample-webapp-lending-1.1.0-RELEASE.jar を C:\webapps\ksbysample-webapp-lending\lib の下へコピーします。

C:\webapps\ksbysample-webapp-lending\bat の下の webapp_startup.bat を以下のように変更します。

@echo on

setlocal
set JAVA_HOME=C:\Java\jdk1.8.0_92
set PATH=%JAVA_HOME%\bin;%PATH%
set WEBAPP_HOME=C:\webapps\ksbysample-webapp-lending
set WEBAPP_JAR=ksbysample-webapp-lending-1.1.0-RELEASE.jar

cd /d %WEBAPP_HOME%
java -server ^
     -Xms1024m -Xmx1024m ^
     -XX:MaxMetaspaceSize=384m ^
     -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled ^
     -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=75 ^
     -XX:+ScavengeBeforeFullGC -XX:+CMSScavengeBeforeRemark ^
     -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps ^
     -Xloggc:%WEBAPP_HOME%/logs/gc.log ^
     -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=10M ^
     -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=%WEBAPP_HOME%/logs/`date`.hprof ^
     -XX:ErrorFile=%WEBAPP_HOME%/logs/hs_err_pid_%p.log ^
     -Dsun.net.inetaddr.ttl=100 ^
     -Dcom.sun.management.jmxremote ^
     -Dcom.sun.management.jmxremote.port=7900 ^
     -Dcom.sun.management.jmxremote.ssl=false ^
     -Dcom.sun.management.jmxremote.authenticate=false ^
     -Dspring.profiles.active=product ^
     -jar lib\%WEBAPP_JAR%
  • set JAVA_HOME=C:\Java\jdk1.8.0_77set JAVA_HOME=C:\Java\jdk1.8.0_92 へ変更します。
  • set WEBAPP_JAR=ksbysample-webapp-lending-1.0.0-RELEASE.jarset WEBAPP_JAR=ksbysample-webapp-lending-1.1.0-RELEASE.jar へ変更します。

webapp_startup.bat を実行します。

f:id:ksby:20160528191241p:plain

http://localhost:8080/sample/ にアクセスすると spring.thymeleaf.cache = false と表示されました。javar -jar で起動したのに true になりませんね。。。

f:id:ksby:20160528191508p:plain

webapp_startup.bat 実行時に開いたコマンドプロンプトを閉じます。

devtools を外して java -jar で起動すると spring.thymeleaf.cache が true になるか確認する

devtools を外したら spring.thymeleaf.cache がどう表示されるのか確認してみます。

build.gradle の compile("org.springframework.boot:spring-boot-devtools") の行をコメントアウトします。変更後、Gradle projects View の左上にある「Refresh all Gradle projects」ボタンをクリックして更新します。

Gradle projects View から clean タスクの実行→「Rebuild Project」メニューの実行→build タスクの実行を行い、ksbysample-webapp-lending-1.1.0-RELEASE.jar を作成し直します。

ksbysample-webapp-lending-1.1.0-RELEASE.jar を C:\webapps\ksbysample-webapp-lending\lib の下へコピーした後、webapp_startup.bat を実行します。

http://localhost:8080/sample/ にアクセスすると今度は spring.thymeleaf.cache = と表示されました。Appendix A. Common application properties を見ると spring.thymeleaf.cache=true # Enable template caching. と記載されていますので、未設定時は true になっていると考えてよいでしょう。

f:id:ksby:20160528195250p:plain

webapp_startup.bat 実行時に開いたコマンドプロンプトを閉じます。

build.gradle の compile("org.springframework.boot:spring-boot-devtools")コメントアウトも元に戻して、変更後、Gradle projects View の左上にある「Refresh all Gradle projects」ボタンをクリックして更新しておきます。

次回は。。。

java -jar で起動しても spring.thymeleaf.cache = false になる原因を調査します。DevToolsPropertyDefaultsPostProcessor.java を見ると remoteUrl が null だと spring.thymeleaf.cache = false が設定されるようなのですが、remoteUrl とは何でしょうか?

ソースコード

履歴

2016/05/28
初版発行。