Example usage for org.springframework.util SocketUtils findAvailableTcpPort

List of usage examples for org.springframework.util SocketUtils findAvailableTcpPort

Introduction

In this page you can find the example usage for org.springframework.util SocketUtils findAvailableTcpPort.

Prototype

public static int findAvailableTcpPort() 

Source Link

Document

Find an available TCP port randomly selected from the range [ #PORT_RANGE_MIN , #PORT_RANGE_MAX ].

Usage

From source file:com.hillert.botanic.MainApp.java

/**
 * Main class initializes the Spring Application Context and populates seed
 * data using {@link SeedDataService}./*from  w w w  .ja  v a 2s . c om*/
 *
 * @param args Not used.
 */
public static void main(String[] args) {
    Map<String, Object> props = new HashMap<String, Object>();
    props.put("redisPort", SocketUtils.findAvailableTcpPort());

    SpringApplication app = new SpringApplication(MainApp.class);
    app.setDefaultProperties(props);

    ConfigurableApplicationContext context = app.run(args);

    MapPropertySource propertySource = new MapPropertySource("ports", props);

    context.getEnvironment().getPropertySources().addFirst(propertySource);

    SeedDataService seedDataService = context.getBean(SeedDataService.class);
    seedDataService.populateSeedData();
}

From source file:demo.EmbeddedRedisServerBean.java

public static EmbeddedRedisServerBean createWithAvailbeTcpPort() throws IOException {
    RedisServer redisServer = new RedisServer(SocketUtils.findAvailableTcpPort());
    return new EmbeddedRedisServerBean(redisServer);
}

From source file:playground.itests.AbstractHttpHandlerIntegrationTests.java

@Before
public void setup() throws Exception {
    this.port = SocketUtils.findAvailableTcpPort();
    this.server = new TomcatHttpServer();
    this.server.setPort(this.port);
    this.server.setHandler(createHttpHandler());
    this.server.afterPropertiesSet();
    this.server.start();

    this.webClient = new WebClient(new ReactorClientHttpConnector());
}

From source file:com.kixeye.chassis.bootstrap.SpringConfiguration.java

@Bean(destroyMethod = "close")
public TestingServer zookeeper() throws Exception {
    return new TestingServer(SocketUtils.findAvailableTcpPort());
}

From source file:comsat.sample.tomcat.SampleTomcatTwoConnectorsApplication.java

@Bean
public int port() {
    return SocketUtils.findAvailableTcpPort();
}

From source file:sample.SessionConfig.java

@Bean(destroyMethod = "shutdown")
public HazelcastInstance hazelcastInstance() {
    Config config = new Config();

    int port = SocketUtils.findAvailableTcpPort();

    config.getNetworkConfig().setPort(port);

    System.out.println("Hazelcast port #: " + port);

    SerializerConfig serializer = new SerializerConfig().setImplementation(new ObjectStreamSerializer())
            .setTypeClass(Object.class);

    config.getSerializationConfig().addSerializerConfig(serializer);

    MapAttributeConfig attributeConfig = new MapAttributeConfig()
            .setName(HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE)
            .setExtractor(PrincipalNameExtractor.class.getName());

    config.getMapConfig("spring:session:sessions").addMapAttributeConfig(attributeConfig)
            .addMapIndexConfig(new MapIndexConfig(HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE, false));

    return Hazelcast.newHazelcastInstance(config);
}

From source file:com.kixeye.chassis.bootstrap.DynamicZookeeperConfigurationSourceTest.java

@Before
public void beforeClass() throws Exception {
    zookeeper = new TestingServer(SocketUtils.findAvailableTcpPort());
    curatorFramework = CuratorFrameworkFactory.newClient(zookeeper.getConnectString(), new RetryOneTime(1000));
    curatorFramework.start();/*www .  j  av a  2  s  . c om*/

    curatorFramework.create().forPath(CONFIG_BASE_PATH);

    Map<String, Object> defaults = new HashMap<>();
    defaults.put(DEF_KEY1, DEF_VAL1);
    defaults.put(DEF_KEY2, DEF_VAL2);

    defaultConfiguration = new MapConfiguration(defaults);

    node = UUID.randomUUID().toString();
    config = new ConcurrentCompositeConfiguration();
}

From source file:com.kixeye.chassis.bootstrap.CuratorFrameworkBuilderTest.java

@Test
public void zookeeperStarted() throws Exception {
    testingServer = new TestingServer(SocketUtils.findAvailableTcpPort());
    try (CuratorFramework curatorFramework = new CuratorFrameworkBuilder(true)
            .withZookeeper(testingServer.getConnectString()).build()) {
        Assert.assertEquals(CuratorFrameworkState.STARTED, curatorFramework.getState());
        Assert.assertNull(curatorFramework.checkExists().forPath("/test"));
        curatorFramework.create().forPath("/test");
        Assert.assertNotNull(curatorFramework.checkExists().forPath("/test"));
    }/*from   w  w  w  . java 2  s  .co m*/
}

From source file:net.jadler.JadlerFacadeIntegrationTest.java

@Test
public void portConfiguration() throws IOException {
    initJadlerListeningOn(SocketUtils.findAvailableTcpPort());

    try {/*from   w  w w  . ja  v a  2 s  .c o m*/
        onRequest().respond().withStatus(EXPECTED_STATUS);
        assertExpectedStatus();
    } finally {
        closeJadler();
    }
}

From source file:com.kixeye.chassis.bootstrap.webapp.TestSpringWebApp.java

@Bean(initMethod = "start", destroyMethod = "stop", name = "httpServer")
@Order(0)//from   www  . j a  v  a  2  s. c om
public Server httpServer(ConfigurableWebApplicationContext webApplicationContext) {

    // set up servlets
    ServletHandler servlets = new ServletHandler();
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SECURITY);
    context.setErrorHandler(null);
    context.setWelcomeFiles(new String[] { "/" });

    // set up spring with the servlet context
    setServletContext(context.getServletContext());

    // configure the spring mvc dispatcher
    DispatcherServlet dispatcher = new DispatcherServlet(webApplicationContext);

    // map application servlets
    context.addServlet(new ServletHolder(dispatcher), "/");

    servlets.setHandler(context);

    // create the server
    InetSocketAddress address = new InetSocketAddress(SocketUtils.findAvailableTcpPort());

    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setHost(address.getHostName());
    connector.setPort(address.getPort());
    server.setConnectors(new Connector[] { connector });
    server.setHandler(servlets);
    server.setStopAtShutdown(true);

    return server;
}