List of usage examples for org.springframework.integration.ip.tcp.connection AbstractServerConnectionFactory registerListener
public void registerListener(TcpListener listenerToRegister)
From source file:org.springframework.integration.ip.tcp.connection.ConnectionEventTests.java
private void testServerExceptionGuts(int port, AbstractServerConnectionFactory factory) throws Exception { ServerSocket ss = null;/*from ww w . j a v a 2s. com*/ try { ss = ServerSocketFactory.getDefault().createServerSocket(port); } catch (Exception e) { return; // skip this test, someone grabbed the port } final AtomicReference<TcpConnectionServerExceptionEvent> theEvent = new AtomicReference<TcpConnectionServerExceptionEvent>(); final CountDownLatch latch = new CountDownLatch(1); factory.setApplicationEventPublisher(new ApplicationEventPublisher() { @Override public void publishEvent(ApplicationEvent event) { theEvent.set((TcpConnectionServerExceptionEvent) event); latch.countDown(); } @Override public void publishEvent(Object event) { } }); factory.setBeanName("sf"); factory.registerListener(message -> false); Log logger = spy(TestUtils.getPropertyValue(factory, "logger", Log.class)); doAnswer(new DoesNothing()).when(logger).error(anyString(), any(Throwable.class)); new DirectFieldAccessor(factory).setPropertyValue("logger", logger); factory.start(); assertTrue(latch.await(10, TimeUnit.SECONDS)); String actual = theEvent.toString(); assertThat(actual, containsString("cause=java.net.BindException")); assertThat(actual, containsString("source=" + "sf, port=" + port)); ArgumentCaptor<String> reasonCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<Throwable> throwableCaptor = ArgumentCaptor.forClass(Throwable.class); verify(logger).error(reasonCaptor.capture(), throwableCaptor.capture()); assertThat(reasonCaptor.getValue(), startsWith("Error on Server")); assertThat(reasonCaptor.getValue(), endsWith("; port = " + port)); assertThat(throwableCaptor.getValue(), instanceOf(BindException.class)); ss.close(); }
From source file:org.springframework.integration.ip.tcp.connection.ConnectionFactoryTests.java
private void testEarlyClose(final AbstractServerConnectionFactory factory, String property, String message) throws Exception { factory.setApplicationEventPublisher(mock(ApplicationEventPublisher.class)); factory.setBeanName("foo"); factory.registerListener(mock(TcpListener.class)); factory.afterPropertiesSet();//from w w w . j a va2 s . c o m Log logger = spy(TestUtils.getPropertyValue(factory, "logger", Log.class)); new DirectFieldAccessor(factory).setPropertyValue("logger", logger); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); final CountDownLatch latch3 = new CountDownLatch(1); when(logger.isInfoEnabled()).thenReturn(true); when(logger.isDebugEnabled()).thenReturn(true); doAnswer(invocation -> { latch1.countDown(); // wait until the stop nulls the channel latch2.await(10, TimeUnit.SECONDS); return null; }).when(logger).info(contains("Listening")); doAnswer(invocation -> { latch3.countDown(); return null; }).when(logger).debug(contains(message)); factory.start(); assertTrue("missing info log", latch1.await(10, TimeUnit.SECONDS)); // stop on a different thread because it waits for the executor Executors.newSingleThreadExecutor().execute(() -> factory.stop()); int n = 0; DirectFieldAccessor accessor = new DirectFieldAccessor(factory); while (n++ < 200 && accessor.getPropertyValue(property) != null) { Thread.sleep(100); } assertTrue("Stop was not invoked in time", n < 200); latch2.countDown(); assertTrue("missing debug log", latch3.await(10, TimeUnit.SECONDS)); String expected = "foo, port=" + factory.getPort() + message; ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(logger, atLeast(1)).debug(captor.capture()); assertThat(captor.getAllValues(), hasItem(expected)); factory.stop(); }