List of usage examples for org.springframework.integration.ip.tcp.connection AbstractClientConnectionFactory registerListener
public void registerListener(TcpListener listenerToRegister)
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();//w ww.ja v a2s . 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("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 ww .ja va 2 s.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("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.FailoverClientConnectionFactory.java
/** * Delegate TCP Client Connection factories that are used to receive * data need a Listener to send the messages to. * This applies to client factories used for outbound gateways * or for a pair of collaborating channel adapters. * <p/>// w w w . jav a 2 s .c om * During initialization, if a factory detects it has no listener * it's listening logic (active thread) is terminated. * <p/> * The listener registered with a factory is provided to each * connection it creates so it can call the onMessage() method. * <p/> * This code satisfies the first requirement in that this * listener signals to the factory that it needs to run * its listening logic. * <p/> * When we wrap actual connections with FailoverTcpConnections, * the connection is given the wrapper as a listener, so it * can enhance the headers in onMessage(); the wrapper then invokes * the real listener supplied here, with the modified message. */ @Override public void registerListener(TcpListener listener) { super.registerListener(listener); for (AbstractClientConnectionFactory factory : this.factories) { factory.registerListener(new TcpListener() { public boolean onMessage(Message<?> message) { throw new UnsupportedOperationException("This should never be called"); } }); } }