Spring Bootでは組み込みコンテナをTomcat、Jetty、Undertowから選択できます。今まではTomcatを使ってたのですが、Jettyへの切り替えも簡単に出来そうだったのでちょっと使ってみようかと思います。
参考にしたサイト
pom.xmlの設定
spring-boot-starter-webでは、デフォルトコンテナとしてTomcatを利用します。なので、まずはTomcatを除外してそのうえでJettyを追加します。以下のようになります。
※Gradleの記述も公式にあります。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Tomcatを除外 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Jettyを追加 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
これでJettyが使えるようになりました。
Jettyを起動してみる
Tomcatの時と同じようにJettyを起動してコンソールログを確認してみます。
2015-12-20 00:04:06.792 INFO 5840 --- [ main] com.example.SpringBootJettyApplication : Starting SpringBootJettyApplication on N-PC with PID 5840 (C:\Development\sts-bundle\workspace\spring-boot-jetty\target\classes started by N in C:\Development\sts-bundle\workspace\spring-boot-jetty)
~略~
2015-12-20 00:04:18.721 INFO 5840 --- [ main] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 8080 (http/1.1)
2015-12-20 00:04:18.746 INFO 5840 --- [ main] com.example.SpringBootJettyApplication : Started SpringBootJettyApplication in 14.129 seconds (JVM running for 16.169)
Jetty started on port(s) 8080 (http/1.1) 8080ポートでJettyが起動していることが分かります。
ついでなので起動までの時間をTomcatと比較します。以下はTomcat起動時のコンソールログです。
2015-12-19 23:58:52.347 INFO 9896 --- [ main] com.example.SpringBootJettyApplication : Starting SpringBootJettyApplication on N-PC with PID 9896 (C:\Development\sts-bundle\workspace\spring-boot-jetty\target\classes started by N in C:\Development\sts-bundle\workspace\spring-boot-jetty)
~略~
2015-12-19 23:59:09.642 INFO 9896 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2015-12-19 23:59:09.671 INFO 9896 --- [ main] com.example.SpringBootJettyApplication : Started SpringBootJettyApplication in 18.602 seconds (JVM running for 21.014)
Jetty:14.129 seconds
Tomcat:18.602 seconds
やはりJettyの方が早いですね。
Jettyの設定を変更してみる
Jettyの設定変更もapplication.properties(or YML)で可能ですが、JettyEmbeddedServletContainerFactoryやJettyServerCustomizerを利用したコードベースでの柔軟な変更も可能となっています。
- JettyEmbeddedServletContainerFactory
- JettyServerCustomizer
以下のような感じで試してみました。
JettyServerCustomizerの実装クラスを作りcustomizeメソッドをオーバーライドします。
org.eclipse.jetty.server.Serverが取得できます。このクラスはConnectorやRequestHandlerの集合クラスでサーバーの基本的なところの設定を行います。
package com.example.config;
import org.eclipse.jetty.server.Server;
import org.springframework.boot.context.embedded.jetty.JettyServerCustomizer;
import org.springframework.stereotype.Component;
@Component
public class JettyServerCustomizerImpl implements JettyServerCustomizer {
@Override
public void customize(Server server) {
System.out.println("JettyServer Customize!!");
server.setStopTimeout(100);
}
}
次にEmbeddedServletContainerCustomizerの実装クラスを作りcustomizeクラスをオーバーライドします。
org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainerが取得できます。実態はJettyEmbeddedServletContainerFactoryとなっており、このクラスを利用してコンテナの設定を行います。最後に上記のJettyServerCustomizerImplをaddServerCustomizersに渡すことで、サーバ全体のカスタマイズ設定が可能です。
package com.example.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.stereotype.Component;
@Component
public class JettyConfig implements EmbeddedServletContainerCustomizer {
@Autowired
JettyServerCustomizerImpl jettyServerCustomizerImpl;
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
System.out.println("JettyContainer Configuration!!");
container.setPort(8888);
((JettyEmbeddedServletContainerFactory) container).addServerCustomizers(jettyServerCustomizerImpl);
}
}
これらのクラスはJettyの起動時に実行され、コンテナの設定として反映されます。
以下は起動時のログです。コンソール出力から上記2つのクラスが起動時に読み込まれていることがわかります。
2015-12-20 20:45:04.504 INFO 9308 --- [ main] com.example.SpringBootJettyApplication : Starting SpringBootJettyApplication on N-PC with PID 9308 (C:\Development\sts-bundle\workspace\spring-boot-jetty\target\classes started by N in C:\Development\sts-bundle\workspace\spring-boot-jetty)
2015-12-20 20:45:04.507 INFO 9308 --- [ main] com.example.SpringBootJettyApplication : No active profile set, falling back to default profiles: default
2015-12-20 20:45:04.578 INFO 9308 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@52e677af: startup date [Sun Dec 20 20:45:04 JST 2015]; root of context hierarchy
2015-12-20 20:45:05.766 INFO 9308 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
JettyContainer Configuration!!
2015-12-20 20:45:06.829 INFO 9308 --- [ main] e.j.JettyEmbeddedServletContainerFactory : Server initialized with port: 8888
JettyServer Customize!!
2015-12-20 20:45:06.832 INFO 9308 --- [ main] org.eclipse.jetty.server.Server : jetty-9.2.14.v20151106
2015-12-20 20:45:06.975 INFO 9308 --- [ main] application : Initializing Spring embedded WebApplicationContext
2015-12-20 20:45:06.975 INFO 9308 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2400 ms
2015-12-20 20:45:07.626 INFO 9308 --- [ main] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2015-12-20 20:45:07.629 INFO 9308 --- [ main] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-12-20 20:45:07.629 INFO 9308 --- [ main] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2015-12-20 20:45:07.629 INFO 9308 --- [ main] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2015-12-20 20:45:07.630 INFO 9308 --- [ main] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2015-12-20 20:45:07.902 INFO 9308 --- [ main] o.e.jetty.server.handler.ContextHandler : Started o.s.b.c.e.j.JettyEmbeddedWebAppContext@6bb7cce7{/,file:/C:/Users/N/AppData/Local/Temp/jetty-docbase.186998930257237945.8888/,AVAILABLE}
2015-12-20 20:45:07.903 INFO 9308 --- [ main] org.eclipse.jetty.server.Server : Started @5155ms
2015-12-20 20:45:08.201 INFO 9308 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@52e677af: startup date [Sun Dec 20 20:45:04 JST 2015]; root of context hierarchy
2015-12-20 20:45:08.315 INFO 9308 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2015-12-20 20:45:08.317 INFO 9308 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2015-12-20 20:45:08.594 INFO 9308 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-12-20 20:45:08.595 INFO 9308 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-12-20 20:45:08.643 INFO 9308 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-12-20 20:45:08.854 INFO 9308 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2015-12-20 20:45:08.879 INFO 9308 --- [ main] application : Initializing Spring FrameworkServlet 'dispatcherServlet'
2015-12-20 20:45:08.880 INFO 9308 --- [ main] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2015-12-20 20:45:08.898 INFO 9308 --- [ main] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 18 ms
2015-12-20 20:45:08.926 INFO 9308 --- [ main] o.eclipse.jetty.server.ServerConnector : Started ServerConnector@7906578e{HTTP/1.1}{0.0.0.0:8888}
2015-12-20 20:45:08.930 INFO 9308 --- [ main] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 8888 (http/1.1)
2015-12-20 20:45:08.936 INFO 9308 --- [ main] com.example.SpringBootJettyApplication : Started SpringBootJettyApplication in 5.441 seconds (JVM running for 6.189)
ちょっと説明がイマイチなところもありますが、こんな感じで設定することが出来ました。