List of usage examples for org.springframework.integration.ip.tcp.connection TcpNioServerConnectionFactory TcpNioServerConnectionFactory
public TcpNioServerConnectionFactory(int port)
From source file:ru.jts_dev.gameserver.config.GameIntegrationConfig.java
@Bean public TcpNioServerConnectionFactory gameConnectionFactory() { TcpNioServerConnectionFactory serverConnectionFactory = new TcpNioServerConnectionFactory(port); serverConnectionFactory.setDeserializer(new ProtocolByteArrayLengthHeaderSerializer()); serverConnectionFactory.setSerializer(new ProtocolByteArrayLengthHeaderSerializer()); return serverConnectionFactory; }
From source file:ru.jts_dev.authserver.config.AuthIntegrationConfig.java
/** * Server connection factory, for game client connections. * Set length serializer/deserializer for packets. * * @return - server factory bean/*w ww . j a v a 2 s .co m*/ */ @Bean public TcpNioServerConnectionFactory connectionFactory() { TcpNioServerConnectionFactory serverConnectionFactory = new TcpNioServerConnectionFactory(authserverPort); serverConnectionFactory.setDeserializer(new ProtocolByteArrayLengthHeaderSerializer()); serverConnectionFactory.setSerializer(new ProtocolByteArrayLengthHeaderSerializer()); return serverConnectionFactory; }
From source file:org.springframework.integration.ip.tcp.connection.ConnectionEventTests.java
@Test public void testNioServerExceptionEvent() throws Exception { int port = SocketUtils.findAvailableTcpPort(); AbstractServerConnectionFactory factory = new TcpNioServerConnectionFactory(port); testServerExceptionGuts(port, factory); }
From source file:org.springframework.integration.ip.tcp.connection.ConnectionFactoryTests.java
@Test public void testObtainConnectionIdsNio() throws Exception { TcpNioServerConnectionFactory serverFactory = new TcpNioServerConnectionFactory(0); testObtainConnectionIds(serverFactory); }
From source file:org.springframework.integration.ip.tcp.connection.ConnectionFactoryTests.java
@Test public void testEarlyCloseNio() throws Exception { AbstractServerConnectionFactory factory = new TcpNioServerConnectionFactory(0); testEarlyClose(factory, "serverChannel", " stopped before registering the server channel"); }
From source file:org.springframework.integration.ip.tcp.connection.TcpNioConnectionTests.java
@Test public void testAssemblerUsesSecondaryExecutor() throws Exception { TcpNioServerConnectionFactory factory = new TcpNioServerConnectionFactory(0); factory.setApplicationEventPublisher(nullPublisher); CompositeExecutor compositeExec = compositeExecutor(); factory.setSoTimeout(1000);/*ww w .ja v a 2s .c o m*/ factory.setTaskExecutor(compositeExec); final AtomicReference<String> threadName = new AtomicReference<String>(); final CountDownLatch latch = new CountDownLatch(1); factory.registerListener(new TcpListener() { @Override public boolean onMessage(Message<?> message) { if (!(message instanceof ErrorMessage)) { threadName.set(Thread.currentThread().getName()); latch.countDown(); } return false; } }); factory.start(); TestingUtilities.waitListening(factory, null); int port = factory.getPort(); Socket socket = null; int n = 0; while (n++ < 100) { try { socket = SocketFactory.getDefault().createSocket("localhost", port); break; } catch (ConnectException e) { } Thread.sleep(100); } assertTrue("Could not open socket to localhost:" + port, n < 100); socket.getOutputStream().write("foo\r\n".getBytes()); socket.close(); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertThat(threadName.get(), containsString("assembler")); factory.stop(); }
From source file:org.springframework.integration.ip.tcp.connection.TcpNioConnectionTests.java
@Test public void testAllMessagesDelivered() throws Exception { final int numberOfSockets = 100; TcpNioServerConnectionFactory factory = new TcpNioServerConnectionFactory(0); factory.setApplicationEventPublisher(nullPublisher); CompositeExecutor compositeExec = compositeExecutor(); factory.setTaskExecutor(compositeExec); final CountDownLatch latch = new CountDownLatch(numberOfSockets * 4); factory.registerListener(new TcpListener() { @Override//from w ww .ja v a2 s.c om public boolean onMessage(Message<?> message) { if (!(message instanceof ErrorMessage)) { latch.countDown(); } return false; } }); factory.start(); TestingUtilities.waitListening(factory, null); int port = factory.getPort(); Socket[] sockets = new Socket[numberOfSockets]; for (int i = 0; i < numberOfSockets; i++) { Socket socket = null; int n = 0; while (n++ < 100) { try { socket = SocketFactory.getDefault().createSocket("localhost", port); break; } catch (ConnectException e) { } Thread.sleep(100); } assertTrue("Could not open socket to localhost:" + port, n < 100); sockets[i] = socket; } for (int i = 0; i < numberOfSockets; i++) { sockets[i].getOutputStream().write("foo1 and...".getBytes()); sockets[i].getOutputStream().flush(); } Thread.sleep(100); for (int i = 0; i < numberOfSockets; i++) { sockets[i].getOutputStream().write(("...foo2\r\nbar1 and...").getBytes()); sockets[i].getOutputStream().flush(); } for (int i = 0; i < numberOfSockets; i++) { sockets[i].getOutputStream().write(("...bar2\r\n").getBytes()); sockets[i].getOutputStream().flush(); } for (int i = 0; i < numberOfSockets; i++) { sockets[i].getOutputStream().write("foo3 and...".getBytes()); sockets[i].getOutputStream().flush(); } Thread.sleep(100); for (int i = 0; i < numberOfSockets; i++) { sockets[i].getOutputStream().write(("...foo4\r\nbar3 and...").getBytes()); sockets[i].getOutputStream().flush(); } for (int i = 0; i < numberOfSockets; i++) { sockets[i].getOutputStream().write(("...bar4\r\n").getBytes()); sockets[i].close(); } assertTrue("latch is still " + latch.getCount(), latch.await(60, TimeUnit.SECONDS)); factory.stop(); }
From source file:org.springframework.integration.ip.tcp.connection.TcpNioConnectionTests.java
@Test public void int3453RaceTest() throws Exception { TcpNioServerConnectionFactory factory = new TcpNioServerConnectionFactory(0); final CountDownLatch connectionLatch = new CountDownLatch(1); factory.setApplicationEventPublisher(new ApplicationEventPublisher() { @Override//from w w w .j a va2 s . c om public void publishEvent(ApplicationEvent event) { if (event instanceof TcpConnectionOpenEvent) { connectionLatch.countDown(); } } @Override public void publishEvent(Object event) { } }); final CountDownLatch assemblerLatch = new CountDownLatch(1); final AtomicReference<Thread> assembler = new AtomicReference<Thread>(); factory.registerListener(new TcpListener() { @Override public boolean onMessage(Message<?> message) { if (!(message instanceof ErrorMessage)) { assembler.set(Thread.currentThread()); assemblerLatch.countDown(); } return false; } }); ThreadPoolTaskExecutor te = new ThreadPoolTaskExecutor(); te.setCorePoolSize(3); // selector, reader, assembler te.setMaxPoolSize(3); te.setQueueCapacity(0); te.initialize(); factory.setTaskExecutor(te); factory.start(); TestingUtilities.waitListening(factory, 10000L); int port = factory.getPort(); Socket socket = SocketFactory.getDefault().createSocket("localhost", port); assertTrue(connectionLatch.await(10, TimeUnit.SECONDS)); TcpNioConnection connection = (TcpNioConnection) TestUtils .getPropertyValue(factory, "connections", Map.class).values().iterator().next(); Log logger = spy(TestUtils.getPropertyValue(connection, "logger", Log.class)); DirectFieldAccessor dfa = new DirectFieldAccessor(connection); dfa.setPropertyValue("logger", logger); ChannelInputStream cis = spy( TestUtils.getPropertyValue(connection, "channelInputStream", ChannelInputStream.class)); dfa.setPropertyValue("channelInputStream", cis); final CountDownLatch readerLatch = new CountDownLatch(4); // 3 dataAvailable, 1 continuing final CountDownLatch readerFinishedLatch = new CountDownLatch(1); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { invocation.callRealMethod(); // delay the reader thread resetting writingToPipe readerLatch.await(10, TimeUnit.SECONDS); Thread.sleep(100); readerFinishedLatch.countDown(); return null; } }).when(cis).write(any(ByteBuffer.class)); doReturn(true).when(logger).isTraceEnabled(); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { invocation.callRealMethod(); readerLatch.countDown(); return null; } }).when(logger).trace(contains("checking data avail")); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { invocation.callRealMethod(); readerLatch.countDown(); return null; } }).when(logger).trace(contains("Nio assembler continuing")); socket.getOutputStream().write("foo\r\n".getBytes()); assertTrue(assemblerLatch.await(10, TimeUnit.SECONDS)); assertTrue(readerFinishedLatch.await(10, TimeUnit.SECONDS)); StackTraceElement[] stackTrace = assembler.get().getStackTrace(); assertThat(Arrays.asList(stackTrace).toString(), not(containsString("ChannelInputStream.getNextBuffer"))); socket.close(); factory.stop(); }
From source file:org.springframework.integration.syslog.inbound.SyslogReceivingChannelAdapterTests.java
@Test public void testTcpRFC5424() throws Exception { SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean( SyslogReceivingChannelAdapterFactoryBean.Protocol.tcp); int port = SocketUtils.findAvailableServerSocket(1514); PollableChannel outputChannel = new QueueChannel(); factory.setOutputChannel(outputChannel); ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class); final CountDownLatch latch = new CountDownLatch(2); doAnswer(invocation -> {/*from ww w . j a v a2s . co m*/ latch.countDown(); return null; }).when(publisher).publishEvent(any(ApplicationEvent.class)); factory.setBeanFactory(mock(BeanFactory.class)); AbstractServerConnectionFactory connectionFactory = new TcpNioServerConnectionFactory(port); connectionFactory.setDeserializer(new RFC6587SyslogDeserializer()); connectionFactory.setApplicationEventPublisher(publisher); factory.setConnectionFactory(connectionFactory); factory.setConverter(new RFC5424MessageConverter()); factory.afterPropertiesSet(); factory.start(); TcpSyslogReceivingChannelAdapter adapter = (TcpSyslogReceivingChannelAdapter) factory.getObject(); Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class)); doReturn(true).when(logger).isDebugEnabled(); final CountDownLatch sawLog = new CountDownLatch(1); doAnswer(invocation -> { if (((String) invocation.getArgument(0)).contains("Error on syslog socket")) { sawLog.countDown(); } invocation.callRealMethod(); return null; }).when(logger).debug(anyString()); new DirectFieldAccessor(adapter).setPropertyValue("logger", logger); Thread.sleep(1000); byte[] buf = ("253 <14>1 2014-06-20T09:14:07+00:00 loggregator d0602076-b14a-4c55-852a-981e7afeed38 DEA - " + "[exampleSDID@32473 iut=\\\"3\\\" eventSource=\\\"Application\\\" eventID=\\\"1011\\\"]" + "[exampleSDID@32473 iut=\\\"3\\\" eventSource=\\\"Application\\\" eventID=\\\"1011\\\"] Removing instance") .getBytes("UTF-8"); Socket socket = SocketFactory.getDefault().createSocket("localhost", port); socket.getOutputStream().write(buf); socket.close(); assertTrue(sawLog.await(10, TimeUnit.SECONDS)); @SuppressWarnings("unchecked") Message<Map<String, ?>> message = (Message<Map<String, ?>>) outputChannel.receive(10000); assertNotNull(message); assertEquals("loggregator", message.getPayload().get("syslog_HOST")); adapter.stop(); assertTrue(latch.await(10, TimeUnit.SECONDS)); }