|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package smoketest.jetty; |
| 18 | + |
| 19 | +import com.linecorp.armeria.server.ServerBuilder; |
| 20 | +import com.linecorp.armeria.server.healthcheck.HealthChecker; |
| 21 | +import com.linecorp.armeria.server.jetty.JettyService; |
| 22 | +import com.linecorp.armeria.spring.ArmeriaServerConfigurator; |
| 23 | +import org.eclipse.jetty.server.Server; |
| 24 | + |
| 25 | +import org.springframework.boot.web.embedded.jetty.JettyWebServer; |
| 26 | +import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.context.annotation.Configuration; |
| 29 | + |
| 30 | +/** |
| 31 | + * Configures an Armeria server to redirect the incoming requests to the Jetty instance provided by |
| 32 | + * Spring Boot. It also sets up a {@link HealthChecker} so that it works well with a load balancer. |
| 33 | + */ |
| 34 | +@Configuration |
| 35 | +public class ArmeriaConfiguration { |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns a new {@link HealthChecker} that marks the server as unhealthy when Tomcat becomes unavailable. |
| 39 | + */ |
| 40 | + @Bean |
| 41 | + public HealthChecker jettyHealthChecker(ServletWebServerApplicationContext applicationContext) { |
| 42 | + final Server server = jettyServer(applicationContext).getServer(); |
| 43 | + return server::isRunning; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns a new {@link JettyService} that redirects the incoming requests to the Jetty instance |
| 48 | + * provided by Spring Boot. |
| 49 | + */ |
| 50 | + @Bean |
| 51 | + public JettyService jettyService(ServletWebServerApplicationContext applicationContext) { |
| 52 | + final JettyWebServer jettyWebServer = jettyServer(applicationContext); |
| 53 | + return JettyService.of(jettyWebServer.getServer(), null); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Returns a new {@link ArmeriaServerConfigurator} that is responsible for configuring a {@link Server} |
| 58 | + * using the given {@link ServerBuilder}. |
| 59 | + */ |
| 60 | + @Bean |
| 61 | + public ArmeriaServerConfigurator armeriaServiceInitializer(JettyService jettyService) { |
| 62 | + return sb -> sb.serviceUnder("/", jettyService); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Extracts a Jetty {@link Server} from Spring webapp context. |
| 67 | + */ |
| 68 | + private static JettyWebServer jettyServer(ServletWebServerApplicationContext applicationContext) { |
| 69 | + return (JettyWebServer) applicationContext.getWebServer(); |
| 70 | + } |
| 71 | +} |
0 commit comments