Java Code Examples for org.springframework.util.SocketUtils#findAvailableTcpPort()

The following examples show how to use org.springframework.util.SocketUtils#findAvailableTcpPort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SshdShellAutoConfigurationTest.java    From sshd-shell-spring-boot with Apache License 2.0 6 votes vote down vote up
@Test
public void testHealthInfoCommand() {
    int smtpPort = SocketUtils.findAvailableTcpPort();
    ServerSetup setup = new ServerSetup(smtpPort, null, ServerSetup.PROTOCOL_SMTP);
    setup.setServerStartupTimeout(5000);
    GreenMail mailServer = new GreenMail(setup);
    JavaMailSenderImpl jmsi = (JavaMailSenderImpl) mailSender;
    mailServer.setUser(jmsi.getUsername(), jmsi.getPassword());
    mailServer.start();
    jmsi.setPort(smtpPort);
    sshCallShell((is, os) -> {
        write(os, "health info");
        verifyResponseContains(is, "{\r\n  \"status\" : \"UP\"");
        mailServer.stop();
    });
}
 
Example 2
Source File: ReconnectTest.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Test
public void scheduledConnect() throws IOException, InterruptedException {
    final PinpointClientFactory clientFactory = new DefaultPinpointClientFactory();
    clientFactory.setReconnectDelay(200);
    PinpointClient client = null;

    TestPinpointServerAcceptor testPinpointServerAcceptor = null;
    try {
        int availableTcpPort = SocketUtils.findAvailableTcpPort(47000);
        client = clientFactory.scheduledConnect("localhost", availableTcpPort);

        testPinpointServerAcceptor = new TestPinpointServerAcceptor(testServerMessageListenerFactory);
        testPinpointServerAcceptor.bind(availableTcpPort);
        assertClientConnected(client);

        logger.debug("request server");
        byte[] randomByte = TestByteUtils.createRandomByte(10);
        byte[] response = PinpointRPCTestUtils.request(client, randomByte);

        Assert.assertArrayEquals(randomByte, response);
    } finally {
        PinpointRPCTestUtils.close(client);
        clientFactory.release();
        TestPinpointServerAcceptor.staticClose(testPinpointServerAcceptor);
    }
}
 
Example 3
Source File: TomcatWebSocketTestServer.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void setup() {
	this.port = SocketUtils.findAvailableTcpPort();

	Connector connector = new Connector(Http11NioProtocol.class.getName());
	connector.setPort(this.port);

	File baseDir = createTempDir("tomcat");
	String baseDirPath = baseDir.getAbsolutePath();

	this.tomcatServer = new Tomcat();
	this.tomcatServer.setBaseDir(baseDirPath);
	this.tomcatServer.setPort(this.port);
	this.tomcatServer.getService().addConnector(connector);
	this.tomcatServer.setConnector(connector);
}
 
Example 4
Source File: SampleApplicationTests.java    From spring-cloud-zookeeper with Apache License 2.0 6 votes vote down vote up
@Test
public void contextLoads() throws Exception {
	int zkPort = SocketUtils.findAvailableTcpPort();
	TestingServer server = new TestingServer(zkPort);

	int port = SocketUtils.findAvailableTcpPort(zkPort + 1);

	ConfigurableApplicationContext context = new SpringApplicationBuilder(
			SampleZookeeperApplication.class).run("--server.port=" + port,
					"--management.endpoints.web.exposure.include=*",
					"--spring.cloud.zookeeper.connect-string=localhost:" + zkPort);

	ResponseEntity<String> response = new TestRestTemplate()
			.getForEntity("http://localhost:" + port + "/hi", String.class);
	assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);

	context.close();
	server.close();
}
 
Example 5
Source File: Reactor2TcpStompClientTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {

	logger.debug("Setting up before '" + this.testName.getMethodName() + "'");

	int port = SocketUtils.findAvailableTcpPort(61613);

	this.activeMQBroker = new BrokerService();
	this.activeMQBroker.addConnector("stomp://127.0.0.1:" + port);
	this.activeMQBroker.setStartAsync(false);
	this.activeMQBroker.setPersistent(false);
	this.activeMQBroker.setUseJmx(false);
	this.activeMQBroker.getSystemUsage().getMemoryUsage().setLimit(1024 * 1024 * 5);
	this.activeMQBroker.getSystemUsage().getTempUsage().setLimit(1024 * 1024 * 5);
	this.activeMQBroker.start();

	ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
	taskScheduler.afterPropertiesSet();

	this.client = new Reactor2TcpStompClient("127.0.0.1", port);
	this.client.setMessageConverter(new StringMessageConverter());
	this.client.setTaskScheduler(taskScheduler);
}
 
Example 6
Source File: StompBrokerRelayMessageHandlerIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	logger.debug("Setting up before '" + this.testName.getMethodName() + "'");
	this.port = SocketUtils.findAvailableTcpPort(61613);
	this.responseChannel = new ExecutorSubscribableChannel();
	this.responseHandler = new TestMessageHandler();
	this.responseChannel.subscribe(this.responseHandler);
	this.eventPublisher = new TestEventPublisher();
	startActiveMqBroker();
	createAndStartRelay();
}
 
Example 7
Source File: PinpointClientFactoryTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Test
public void connectFail() {
    try {
        int availableTcpPort = SocketUtils.findAvailableTcpPort(47000);
        clientFactory.connect("127.0.0.1", availableTcpPort);
        Assert.fail();
    } catch (PinpointSocketException e) {
        Assert.assertTrue(ConnectException.class.isInstance(e.getCause()));
    } 
}
 
Example 8
Source File: CauchoRemotingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void simpleHessianServiceExporter() throws IOException {
	final int port = SocketUtils.findAvailableTcpPort();

	TestBean tb = new TestBean("tb");
	SimpleHessianServiceExporter exporter = new SimpleHessianServiceExporter();
	exporter.setService(tb);
	exporter.setServiceInterface(ITestBean.class);
	exporter.setDebug(true);
	exporter.prepare();

	HttpServer server = HttpServer.create(new InetSocketAddress(port), -1);
	server.createContext("/hessian", exporter);
	server.start();
	try {
		HessianClientInterceptor client = new HessianClientInterceptor();
		client.setServiceUrl("http://localhost:" + port + "/hessian");
		client.setServiceInterface(ITestBean.class);
		//client.setHessian2(true);
		client.prepare();
		ITestBean proxy = ProxyFactory.getProxy(ITestBean.class, client);
		assertEquals("tb", proxy.getName());
		proxy.setName("test");
		assertEquals("test", proxy.getName());
	}
	finally {
		server.stop(Integer.MAX_VALUE);
	}
}
 
Example 9
Source File: EmbeddedZooKeeper.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Construct an EmbeddedZooKeeper with a random port.
 */
public EmbeddedZooKeeper() {
    clientPort = SocketUtils.findAvailableTcpPort();
}
 
Example 10
Source File: PortUtils.java    From spring-boot-email-tools with Apache License 2.0 4 votes vote down vote up
public static int randomFreePort() throws IOException {
    return SocketUtils.findAvailableTcpPort();
}
 
Example 11
Source File: DefinedPortTests.java    From spring-cloud-square with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void init() {
	int port = SocketUtils.findAvailableTcpPort();
	System.setProperty("server.port", String.valueOf(port));
	System.setProperty("retrofit.client.url.tests.url", "http://localhost:"+port);
}
 
Example 12
Source File: EmbeddedZooKeeper.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Construct an EmbeddedZooKeeper with a random port.
 */
public EmbeddedZooKeeper() {
    clientPort = SocketUtils.findAvailableTcpPort();
}
 
Example 13
Source File: TestPinpointServerAcceptor.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
public int bind() {
    int port = SocketUtils.findAvailableTcpPort(47000);
    return bind(port);
}
 
Example 14
Source File: EmbeddedZooKeeper.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Construct an EmbeddedZooKeeper with a random port.
 */
public EmbeddedZooKeeper() {
    clientPort = SocketUtils.findAvailableTcpPort();
}
 
Example 15
Source File: EmbeddedZooKeeper.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Construct an EmbeddedZooKeeper with a random port.
 */
public EmbeddedZooKeeper() {
    clientPort = SocketUtils.findAvailableTcpPort();
}
 
Example 16
Source File: EmbeddedZooKeeper.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Construct an EmbeddedZooKeeper with a random port.
 */
public EmbeddedZooKeeper() {
    clientPort = SocketUtils.findAvailableTcpPort();
}
 
Example 17
Source File: EmbeddedZooKeeper.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Construct an EmbeddedZooKeeper with a random port.
 */
public EmbeddedZooKeeper() {
    clientPort = SocketUtils.findAvailableTcpPort();
}
 
Example 18
Source File: WebClientRetrofitTests.java    From spring-cloud-square with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void init() {
    int port = SocketUtils.findAvailableTcpPort();
    System.setProperty("server.port", String.valueOf(port));
    System.setProperty("retrofit.client.url.tests.url", "http://localhost:"+port);
}
 
Example 19
Source File: EmbeddedZooKeeper.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Construct an EmbeddedZooKeeper with a random port.
 */
public EmbeddedZooKeeper() {
    clientPort = SocketUtils.findAvailableTcpPort();
}
 
Example 20
Source File: EmbeddedZooKeeper.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Construct an EmbeddedZooKeeper with a random port.
 */
public EmbeddedZooKeeper() {
    clientPort = SocketUtils.findAvailableTcpPort();
}