List of usage examples for org.springframework.integration.ip.tcp.connection CachingClientConnectionFactory start
@Override public void start()
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();/*from w w w. ja v a 2 s .c om*/ 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 -> {//from w w w. j ava2 s . co m 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(); }
From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java
@Test // INT-3728 public void testEarlyReceive() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final AbstractClientConnectionFactory factory = new TcpNetClientConnectionFactory("", 0) { @Override// www . j a v a 2 s . c o m protected Socket createSocket(String host, int port) throws IOException { Socket mock = mock(Socket.class); when(mock.getInputStream()).thenReturn(new ByteArrayInputStream("foo\r\n".getBytes())); return mock; } @Override public boolean isActive() { return true; } }; factory.setApplicationEventPublisher(mock(ApplicationEventPublisher.class)); final CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(factory, 1); final AtomicReference<Message<?>> received = new AtomicReference<Message<?>>(); cachingFactory.registerListener(message -> { if (!(message instanceof ErrorMessage)) { received.set(message); latch.countDown(); } return false; }); cachingFactory.start(); cachingFactory.getConnection(); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertNotNull(received.get()); assertNotNull(received.get().getHeaders().get(IpHeaders.ACTUAL_CONNECTION_ID)); cachingFactory.stop(); }
From source file:org.springframework.integration.ip.tcp.TcpOutboundGatewayTests.java
@Test public void testGoodNetGWTimeoutCached() throws Exception { final int port = SocketUtils.findAvailableServerSocket(); AbstractClientConnectionFactory ccf = buildCF(port); CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(ccf, 1); cccf.start(); testGoodNetGWTimeoutGuts(port, cccf); }
From source file:org.springframework.integration.ip.tcp.TcpOutboundGatewayTests.java
@Test public void testCachingFailover() throws Exception { final int port = SocketUtils.findAvailableServerSocket(); final CountDownLatch latch = new CountDownLatch(1); final AtomicBoolean done = new AtomicBoolean(); final CountDownLatch serverLatch = new CountDownLatch(1); Executors.newSingleThreadExecutor().execute(new Runnable() { public void run() { try { ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); latch.countDown();/* www.j a v a2 s . com*/ while (!done.get()) { Socket socket = server.accept(); while (!socket.isClosed()) { try { ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); String request = (String) ois.readObject(); logger.debug("Read " + request); ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject("bar"); logger.debug("Replied to " + request); serverLatch.countDown(); } catch (IOException e) { logger.debug("error on write " + e.getClass().getSimpleName()); socket.close(); } } } } catch (Exception e) { if (!done.get()) { e.printStackTrace(); } } } }); assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); // Failover AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class); TcpConnectionSupport mockConn1 = makeMockConnection(); when(factory1.getConnection()).thenReturn(mockConn1); doThrow(new IOException("fail")).when(mockConn1).send(Mockito.any(Message.class)); AbstractClientConnectionFactory factory2 = new TcpNetClientConnectionFactory("localhost", port); factory2.setSerializer(new DefaultSerializer()); factory2.setDeserializer(new DefaultDeserializer()); factory2.setSoTimeout(10000); factory2.setSingleUse(false); List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>(); factories.add(factory1); factories.add(factory2); FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory(factories); failoverFactory.start(); // Cache CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(failoverFactory, 2); cachingFactory.start(); TcpOutboundGateway gateway = new TcpOutboundGateway(); gateway.setConnectionFactory(cachingFactory); PollableChannel outputChannel = new QueueChannel(); gateway.setOutputChannel(outputChannel); gateway.afterPropertiesSet(); gateway.start(); GenericMessage<String> message = new GenericMessage<String>("foo"); gateway.handleMessage(message); Message<?> reply = outputChannel.receive(0); assertNotNull(reply); assertEquals("bar", reply.getPayload()); done.set(true); gateway.stop(); verify(mockConn1).send(Mockito.any(Message.class)); }
From source file:org.springframework.integration.ip.tcp.TcpOutboundGatewayTests.java
@Test public void testCachedGWPropagatesSocketClose() throws Exception { final int port = SocketUtils.findAvailableServerSocket(); AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port); ccf.setSerializer(new DefaultSerializer()); ccf.setDeserializer(new DefaultDeserializer()); ccf.setSoTimeout(10000);/*from w w w . j a v a 2 s .c om*/ ccf.setSingleUse(false); CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(ccf, 1); cccf.start(); testGWPropagatesSocketCloseGuts(port, cccf); }