List of usage examples for org.springframework.integration.ip.tcp.serializer ByteArrayStxEtxSerializer ByteArrayStxEtxSerializer
ByteArrayStxEtxSerializer
From source file:org.springframework.integration.ip.tcp.connection.TcpNetConnectionTests.java
@Test public void testErrorLog() throws Exception { Socket socket = mock(Socket.class); InputStream stream = mock(InputStream.class); when(socket.getInputStream()).thenReturn(stream); when(stream.read()).thenReturn((int) 'x'); TcpNetConnection connection = new TcpNetConnection(socket, true, false, nullPublisher, null); connection.setDeserializer(new ByteArrayStxEtxSerializer()); final AtomicReference<Object> log = new AtomicReference<Object>(); Log logger = mock(Log.class); doAnswer(new Answer<Object>() { public Object answer(InvocationOnMock invocation) throws Throwable { log.set(invocation.getArguments()[0]); return null; }/*from w w w .j av a 2 s. com*/ }).when(logger).error(Mockito.anyString()); DirectFieldAccessor accessor = new DirectFieldAccessor(connection); accessor.setPropertyValue("logger", logger); connection.registerListener(mock(TcpListener.class)); connection.setMapper(new TcpMessageMapper()); connection.run(); assertNotNull(log.get()); assertEquals("Read exception " + connection.getConnectionId() + " MessageMappingException:Expected STX to begin message", log.get()); }
From source file:org.springframework.integration.ip.tcp.TcpSendingMessageHandlerTests.java
@Test public void testNetStxEtx() throws Exception { final int port = SocketUtils.findAvailableServerSocket(); final CountDownLatch latch = new CountDownLatch(1); final AtomicBoolean done = new AtomicBoolean(); Executors.newSingleThreadExecutor().execute(new Runnable() { public void run() { try { ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); latch.countDown();//ww w . j a v a2 s.co m Socket socket = server.accept(); int i = 0; while (true) { byte[] b = new byte[6]; readFully(socket.getInputStream(), b); b = ("\u0002Reply" + (++i) + "\u0003").getBytes(); socket.getOutputStream().write(b); } } catch (Exception e) { if (!done.get()) { e.printStackTrace(); } } } }); AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port); ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer(); ccf.setSerializer(serializer); ccf.setDeserializer(serializer); ccf.setSoTimeout(10000); ccf.start(); TcpSendingMessageHandler handler = new TcpSendingMessageHandler(); handler.setConnectionFactory(ccf); TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter(); adapter.setConnectionFactory(ccf); QueueChannel channel = new QueueChannel(); adapter.setOutputChannel(channel); assertTrue(latch.await(10, TimeUnit.SECONDS)); handler.handleMessage(MessageBuilder.withPayload("Test").build()); handler.handleMessage(MessageBuilder.withPayload("Test").build()); Message<?> mOut = channel.receive(10000); assertNotNull(mOut); assertEquals("Reply1", new String((byte[]) mOut.getPayload())); mOut = channel.receive(10000); assertNotNull(mOut); assertEquals("Reply2", new String((byte[]) mOut.getPayload())); done.set(true); ccf.stop(); }
From source file:org.springframework.integration.ip.tcp.TcpSendingMessageHandlerTests.java
@Test public void testNioStxEtx() throws Exception { final int port = SocketUtils.findAvailableServerSocket(); final CountDownLatch latch = new CountDownLatch(1); final AtomicBoolean done = new AtomicBoolean(); Executors.newSingleThreadExecutor().execute(new Runnable() { public void run() { try { ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); latch.countDown();//from www . j a v a2s . co m Socket socket = server.accept(); int i = 0; while (true) { byte[] b = new byte[6]; readFully(socket.getInputStream(), b); b = ("\u0002Reply" + (++i) + "\u0003").getBytes(); socket.getOutputStream().write(b); } } catch (Exception e) { if (!done.get()) { e.printStackTrace(); } } } }); AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port); ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer(); ccf.setSerializer(serializer); ccf.setDeserializer(serializer); ccf.setSoTimeout(10000); ccf.start(); TcpSendingMessageHandler handler = new TcpSendingMessageHandler(); handler.setConnectionFactory(ccf); TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter(); adapter.setConnectionFactory(ccf); QueueChannel channel = new QueueChannel(); adapter.setOutputChannel(channel); assertTrue(latch.await(10, TimeUnit.SECONDS)); handler.handleMessage(MessageBuilder.withPayload("Test").build()); handler.handleMessage(MessageBuilder.withPayload("Test").build()); Set<String> results = new HashSet<String>(); Message<?> mOut = channel.receive(10000); assertNotNull(mOut); results.add(new String((byte[]) mOut.getPayload())); mOut = channel.receive(10000); assertNotNull(mOut); results.add(new String((byte[]) mOut.getPayload())); assertTrue(results.remove("Reply1")); assertTrue(results.remove("Reply2")); done.set(true); ccf.stop(); }