Example usage for org.springframework.integration.ip.util TestingUtilities waitListening

List of usage examples for org.springframework.integration.ip.util TestingUtilities waitListening

Introduction

In this page you can find the example usage for org.springframework.integration.ip.util TestingUtilities waitListening.

Prototype

public static void waitListening(AbstractInternetProtocolReceivingChannelAdapter adapter,
        @Nullable Long delayArg) throws IllegalStateException 

Source Link

Document

Wait for a server connection factory to actually start listening before starting a test.

Usage

From source file:org.musa.tcpserver.Main.java

/**
 * Load the Spring Integration Application Context
 *
 * @param args - command line arguments//from  w  ww . j  ava2s .  c  o  m
 */
public static void main(final String... args) {

    System.out.println("preparing to launch the server app 'Eye of Terror'");

    final GenericXmlApplicationContext context = setupContext();
    // context.registerShutdownHook();

    final AbstractServerConnectionFactory crLfServer = context.getBean(AbstractServerConnectionFactory.class);

    TestingUtilities.waitListening(crLfServer, 10000L);

    System.out.println("'Eye of Terror' is up and running at port " + crLfServer.getPort());

}

From source file:com.haythem.integration.Main.java

/**
 * Load the Spring Integration Application Context
 *
 * @param args - command line arguments/*  w  ww  . j  a v a2 s.  com*/
 */
public static void main(final String... args) {

    final Scanner scanner = new Scanner(System.in);

    System.out.println("\n========================================================="
            + "\n                                                         "
            + "\n    Welcome to the Spring Integration                    "
            + "\n          TCP-Client-Server Sample!                      "
            + "\n                                                         "
            + "\n    For more information please visit:                   "
            + "\n    http://www.springintegration.org/                    "
            + "\n                                                         "
            + "\n=========================================================");

    final GenericXmlApplicationContext context = Main.setupContext();
    final SimpleGateway gateway = context.getBean(SimpleGateway.class);
    final AbstractServerConnectionFactory crLfServer = context.getBean(AbstractServerConnectionFactory.class);

    System.out.print("Waiting for server to accept connections...");
    TestingUtilities.waitListening(crLfServer, 10000L);
    System.out.println("running.\n\n");

    System.out.println("Please enter some text and press <enter>: ");
    System.out.println("\tNote:");
    System.out.println("\t- Entering FAIL will create an exception");
    System.out.println("\t- Entering q will quit the application");
    System.out.print("\n");
    System.out.println("\t--> Please also check out the other samples, " + "that are provided as JUnit tests.");
    System.out.println("\t--> You can also connect to the server on port '" + crLfServer.getPort()
            + "' using Telnet.\n\n");

    while (true) {

        final String input = scanner.nextLine();

        if ("q".equals(input.trim())) {
            break;
        } else {
            final String result = gateway.send(input);
            System.out.println(result);
        }
    }

    System.out.println("Exiting application...bye.");
    System.exit(0);

}

From source file:com.richard.memorystore.tcp.TcpServer.java

/**
 * Load the Spring Integration Application Context
 *
 * @param args - command line arguments/*  w w w .  j  a v  a 2s  .  c  o  m*/
 */
public static void main() {

    final Scanner scanner = new Scanner(System.in);

    System.out.println("\n========================================================="
            + "\n                                                         "
            + "\n    Welcome to the Spring Integration                    "
            + "\n          TCP-Client-Server Sample!                      "
            + "\n                                                         "
            + "\n    For more information please visit:                   "
            + "\n    http://www.springintegration.org/                    "
            + "\n                                                         "
            + "\n=========================================================");

    final GenericXmlApplicationContext context = TcpServer.setupContext();
    final SimpleGateway gateway = context.getBean(SimpleGateway.class);
    final AbstractServerConnectionFactory crLfServer = context.getBean(AbstractServerConnectionFactory.class);

    System.out.print("Waiting for server to accept connections...");
    TestingUtilities.waitListening(crLfServer, 10000L);
    System.out.println("running.\n\n");

    System.out.println("Please enter some text and press <enter>: ");
    System.out.println("\tNote:");
    System.out.println("\t- Entering FAIL will create an exception");
    System.out.println("\t- Entering q will quit the application");
    System.out.print("\n");
    System.out.println("\t--> Please also check out the other samples, " + "that are provided as JUnit tests.");
    System.out.println("\t--> You can also connect to the server on port '" + crLfServer.getPort()
            + "' using Telnet.\n\n");

    InputProcessor lineProcessor = new InputProcessor();

    while (true) {

        final String input = scanner.nextLine();

        System.out.println("GOT THIS!!!" + input);

        KeyValueController keyValueController = new KeyValueController();

        KeyValue keyValue = lineProcessor.processLine(input);

        keyValueController.addToMemoryStore(keyValue.getKey(), keyValue.getValue());

        if ("q".equals(input.trim())) {
            break;
        } else {
            final String result = gateway.send(input);
            System.out.println(result);
        }
    }

    System.out.println("Exiting application...bye.");
    System.exit(0);

}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test
public void integrationTest() throws Exception {
    TestingUtilities.waitListening(serverCf, null);
    new DirectFieldAccessor(this.clientAdapterCf).setPropertyValue("port", this.serverCf.getPort());

    this.outbound.send(new GenericMessage<>("Hello, world!"));
    Message<?> m = inbound.receive(10000);
    assertNotNull(m);//  w w  w .ja  va  2s  .  c o m
    String connectionId = m.getHeaders().get(IpHeaders.CONNECTION_ID, String.class);

    // assert we use the same connection from the pool
    outbound.send(new GenericMessage<String>("Hello, world!"));
    m = inbound.receive(10000);
    assertNotNull(m);
    assertEquals(connectionId, m.getHeaders().get(IpHeaders.CONNECTION_ID, String.class));
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test
//   @Repeat(1000) // INT-3722
public void gatewayIntegrationTest() throws Exception {
    final List<String> connectionIds = new ArrayList<String>();
    final AtomicBoolean okToRun = new AtomicBoolean(true);
    Executors.newSingleThreadExecutor().execute(() -> {
        while (okToRun.get()) {
            Message<?> m = inbound.receive(1000);
            if (m != null) {
                connectionIds.add((String) m.getHeaders().get(IpHeaders.CONNECTION_ID));
                replies.send(MessageBuilder.withPayload("foo:" + new String((byte[]) m.getPayload()))
                        .copyHeaders(m.getHeaders()).build());
            }//from www .  ja v  a  2 s.  co m
        }
    });
    TestingUtilities.waitListening(serverCf, null);
    new DirectFieldAccessor(this.clientGatewayCf).setPropertyValue("port", this.serverCf.getPort());

    this.toGateway.send(new GenericMessage<>("Hello, world!"));
    Message<?> m = fromGateway.receive(1000);
    assertNotNull(m);
    assertEquals("foo:" + "Hello, world!", new String((byte[]) m.getPayload()));

    BlockingQueue<?> connections = TestUtils.getPropertyValue(this.gatewayCF, "pool.available",
            BlockingQueue.class);
    // wait until the connection is returned to the pool
    int n = 0;
    while (n++ < 100 && connections.size() == 0) {
        Thread.sleep(100);
    }

    // assert we use the same connection from the pool
    toGateway.send(new GenericMessage<String>("Hello, world2!"));
    m = fromGateway.receive(1000);
    assertNotNull(m);
    assertEquals("foo:" + "Hello, world2!", new String((byte[]) m.getPayload()));

    assertEquals(2, connectionIds.size());
    assertEquals(connectionIds.get(0), connectionIds.get(1));

    okToRun.set(false);
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test
public void testCloseOnTimeoutNet() throws Exception {
    TestingUtilities.waitListening(serverCf, null);
    testCloseOnTimeoutGuts(new TcpNetClientConnectionFactory("localhost", serverCf.getPort()));
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test
public void testCloseOnTimeoutNio() throws Exception {
    TestingUtilities.waitListening(serverCf, null);
    testCloseOnTimeoutGuts(new TcpNioClientConnectionFactory("localhost", serverCf.getPort()));
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test
public void testCachedFailoverRealClose() throws Exception {
    TcpNetServerConnectionFactory server1 = new TcpNetServerConnectionFactory(0);
    server1.setBeanName("server1");
    final CountDownLatch latch1 = new CountDownLatch(3);
    server1.registerListener(message -> {
        latch1.countDown();//from  ww  w.j a va  2s . c o  m
        return false;
    });
    server1.start();
    TestingUtilities.waitListening(server1, 10000L);
    int port1 = server1.getPort();
    TcpNetServerConnectionFactory server2 = new TcpNetServerConnectionFactory(0);
    server1.setBeanName("server2");
    final CountDownLatch latch2 = new CountDownLatch(2);
    server2.registerListener(message -> {
        latch2.countDown();
        return false;
    });
    server2.start();
    TestingUtilities.waitListening(server2, 10000L);
    int port2 = server2.getPort();
    // Failover
    AbstractClientConnectionFactory factory1 = new TcpNetClientConnectionFactory("localhost", port1);
    factory1.setBeanName("client1");
    factory1.registerListener(message -> false);
    AbstractClientConnectionFactory factory2 = new TcpNetClientConnectionFactory("localhost", port2);
    factory2.setBeanName("client2");
    factory2.registerListener(message -> false);
    List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
    factories.add(factory1);
    factories.add(factory2);
    FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory(factories);

    // Cache
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(failoverFactory, 2);
    cachingFactory.start();
    TcpConnection conn1 = cachingFactory.getConnection();
    GenericMessage<String> message = new GenericMessage<String>("foo");
    conn1.send(message);
    conn1.close();
    TcpConnection conn2 = cachingFactory.getConnection();
    assertSame(((TcpConnectionInterceptorSupport) conn1).getTheConnection(),
            ((TcpConnectionInterceptorSupport) conn2).getTheConnection());
    conn2.send(message);
    conn1 = cachingFactory.getConnection();
    assertNotSame(((TcpConnectionInterceptorSupport) conn1).getTheConnection(),
            ((TcpConnectionInterceptorSupport) conn2).getTheConnection());
    conn1.send(message);
    conn1.close();
    conn2.close();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    server1.stop();
    TestingUtilities.waitStopListening(server1, 10000L);
    TestingUtilities.waitUntilFactoryHasThisNumberOfConnections(factory1, 0);
    conn1 = cachingFactory.getConnection();
    conn2 = cachingFactory.getConnection();
    conn1.send(message);
    conn2.send(message);
    conn1.close();
    conn2.close();
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    SimplePool<?> pool = TestUtils.getPropertyValue(cachingFactory, "pool", SimplePool.class);
    assertEquals(2, pool.getIdleCount());
    server2.stop();
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test
public void testCachedFailoverRealBadHost() throws Exception {
    TcpNetServerConnectionFactory server1 = new TcpNetServerConnectionFactory(0);
    server1.setBeanName("server1");
    final CountDownLatch latch1 = new CountDownLatch(3);
    server1.registerListener(message -> {
        latch1.countDown();//ww  w  . j  a v  a2 s  .co  m
        return false;
    });
    server1.start();
    TestingUtilities.waitListening(server1, 10000L);
    int port1 = server1.getPort();
    TcpNetServerConnectionFactory server2 = new TcpNetServerConnectionFactory(0);
    server1.setBeanName("server2");
    final CountDownLatch latch2 = new CountDownLatch(2);
    server2.registerListener(message -> {
        latch2.countDown();
        return false;
    });
    server2.start();
    TestingUtilities.waitListening(server2, 10000L);
    int port2 = server2.getPort();
    // Failover
    AbstractClientConnectionFactory factory1 = new TcpNetClientConnectionFactory("junkjunk", port1);
    factory1.setBeanName("client1");
    factory1.registerListener(message -> false);
    AbstractClientConnectionFactory factory2 = new TcpNetClientConnectionFactory("localhost", port2);
    factory2.setBeanName("client2");
    factory2.registerListener(message -> false);
    List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
    factories.add(factory1);
    factories.add(factory2);
    FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory(factories);

    // Cache
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(failoverFactory, 2);
    cachingFactory.start();
    TcpConnection conn1 = cachingFactory.getConnection();
    GenericMessage<String> message = new GenericMessage<String>("foo");
    conn1.send(message);
    conn1.close();
    TcpConnection conn2 = cachingFactory.getConnection();
    assertSame(((TcpConnectionInterceptorSupport) conn1).getTheConnection(),
            ((TcpConnectionInterceptorSupport) conn2).getTheConnection());
    conn2.send(message);
    conn1 = cachingFactory.getConnection();
    assertNotSame(((TcpConnectionInterceptorSupport) conn1).getTheConnection(),
            ((TcpConnectionInterceptorSupport) conn2).getTheConnection());
    conn1.send(message);
    conn1.close();
    conn2.close();
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertEquals(3, latch1.getCount());
    server1.stop();
    server2.stop();
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test //INT-3650
public void testRealConnection() throws Exception {
    TcpNetServerConnectionFactory in = new TcpNetServerConnectionFactory(0);
    final CountDownLatch latch1 = new CountDownLatch(2);
    final CountDownLatch latch2 = new CountDownLatch(102);
    final List<String> connectionIds = new ArrayList<String>();
    in.registerListener(message -> {/*  w  w  w  .  j  av  a  2s .c om*/
        connectionIds.add((String) message.getHeaders().get(IpHeaders.CONNECTION_ID));
        latch1.countDown();
        latch2.countDown();
        return false;
    });
    in.start();
    TestingUtilities.waitListening(in, null);
    int port = in.getPort();
    TcpNetClientConnectionFactory out = new TcpNetClientConnectionFactory("localhost", port);
    CachingClientConnectionFactory cache = new CachingClientConnectionFactory(out, 1);
    cache.setSingleUse(false);
    cache.setConnectionWaitTimeout(100);
    cache.start();
    TcpConnectionSupport connection1 = cache.getConnection();
    connection1.send(new GenericMessage<String>("foo"));
    connection1.close();
    TcpConnectionSupport connection2 = cache.getConnection();
    connection2.send(new GenericMessage<String>("foo"));
    connection2.close();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    assertSame(connectionIds.get(0), connectionIds.get(1));
    for (int i = 0; i < 100; i++) {
        TcpConnectionSupport connection = cache.getConnection();
        connection.send(new GenericMessage<String>("foo"));
        connection.close();
    }
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertSame(connectionIds.get(0), connectionIds.get(101));
    in.stop();
    cache.stop();
}