Update 64.13. Enable Multiple Listeners with Undertow.md

master
qibaoguang 2015-03-25 22:39:26 +08:00
parent 10ec84b5f9
commit e5f896258b
1 changed files with 12 additions and 16 deletions

View File

@ -1,22 +1,18 @@
### 64.13. 启用Undertow的多监听器Multiple Listeners
### 64.13. 发现运行时的HTTP端口 往`UndertowEmbeddedServletContainerFactory`添加一个`UndertowBuilderCustomizer`,然后添加一个监听者到`Builder`
你可以通过日志输出或它的EmbeddedServletContainer的EmbeddedWebApplicationContext获取服务器正在运行的端口。获取和确认服务器已经初始化的最好方式是添加一个`ApplicationListener<EmbeddedServletContainerInitializedEvent>`类型的`@Bean`然后当事件发布时将容器pull出来。
使用`@WebIntegrationTests`的一个有用实践是设置`server.port=0`,然后使用`@Value`注入实际的('local')端口。例如:
```java ```java
@RunWith(SpringJUnit4ClassRunner.class) @Bean
@SpringApplicationConfiguration(classes = SampleDataJpaApplication.class) public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
@WebIntegrationTest("server.port:0") UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
public class CityRepositoryIntegrationTests { factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
@Autowired @Override
EmbeddedWebApplicationContext server; public void customize(Builder builder) {
builder.addHttpListener(8080, "0.0.0.0");
@Value("${local.server.port}") }
int port;
// ...
});
return factory;
} }
``` ```