List of usage examples for org.springframework.integration.ip.tcp.connection TcpNetConnection send
@Override @SuppressWarnings("unchecked") public synchronized void send(Message<?> message)
From source file:org.springframework.integration.ip.tcp.connection.ConnectionEventTests.java
@Test public void testConnectionEvents() throws Exception { Socket socket = mock(Socket.class); final List<TcpConnectionEvent> theEvent = new ArrayList<TcpConnectionEvent>(); TcpNetConnection conn = new TcpNetConnection(socket, false, false, new ApplicationEventPublisher() { @Override/*w ww .j av a2 s. co m*/ public void publishEvent(ApplicationEvent event) { theEvent.add((TcpConnectionEvent) event); } @Override public void publishEvent(Object event) { } }, "foo"); /* * Open is not published by the connection itself; the factory publishes it after initialization. * See ConnectionToConnectionTests. */ @SuppressWarnings("unchecked") Serializer<Object> serializer = mock(Serializer.class); RuntimeException toBeThrown = new RuntimeException("foo"); doThrow(toBeThrown).when(serializer).serialize(Mockito.any(Object.class), Mockito.any(OutputStream.class)); conn.setMapper(new TcpMessageMapper()); conn.setSerializer(serializer); try { conn.send(new GenericMessage<String>("bar")); fail("Expected exception"); } catch (Exception e) { } assertTrue(theEvent.size() > 0); assertNotNull(theEvent.get(0)); assertTrue(theEvent.get(0) instanceof TcpConnectionExceptionEvent); assertTrue( theEvent.get(0).toString().endsWith("[factory=foo, connectionId=" + conn.getConnectionId() + "]")); assertThat(theEvent.get(0).toString(), containsString("RuntimeException: foo, failedMessage=GenericMessage [payload=bar")); TcpConnectionExceptionEvent event = (TcpConnectionExceptionEvent) theEvent.get(0); assertNotNull(event.getCause()); assertSame(toBeThrown, event.getCause().getCause()); assertTrue(theEvent.size() > 1); assertNotNull(theEvent.get(1)); assertTrue(theEvent.get(1).toString() .endsWith("[factory=foo, connectionId=" + conn.getConnectionId() + "] **CLOSED**")); }
From source file:org.springframework.integration.ip.tcp.connection.TcpNetConnectionTests.java
@Test public void transferHeaders() throws Exception { Socket inSocket = mock(Socket.class); PipedInputStream pipe = new PipedInputStream(); when(inSocket.getInputStream()).thenReturn(pipe); TcpConnectionSupport inboundConnection = new TcpNetConnection(inSocket, true, false, nullPublisher, null); inboundConnection.setDeserializer(new MapJsonSerializer()); MapMessageConverter inConverter = new MapMessageConverter(); MessageConvertingTcpMessageMapper inMapper = new MessageConvertingTcpMessageMapper(inConverter); inboundConnection.setMapper(inMapper); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Socket outSocket = mock(Socket.class); TcpNetConnection outboundConnection = new TcpNetConnection(outSocket, true, false, nullPublisher, null); when(outSocket.getOutputStream()).thenReturn(baos); MapMessageConverter outConverter = new MapMessageConverter(); outConverter.setHeaderNames("bar"); MessageConvertingTcpMessageMapper outMapper = new MessageConvertingTcpMessageMapper(outConverter); outboundConnection.setMapper(outMapper); outboundConnection.setSerializer(new MapJsonSerializer()); Message<String> message = MessageBuilder.withPayload("foo").setHeader("bar", "baz").build(); outboundConnection.send(message); PipedOutputStream out = new PipedOutputStream(pipe); out.write(baos.toByteArray());//from ww w .ja v a 2 s . c o m out.close(); final AtomicReference<Message<?>> inboundMessage = new AtomicReference<Message<?>>(); TcpListener listener = new TcpListener() { public boolean onMessage(Message<?> message) { if (!(message instanceof ErrorMessage)) { inboundMessage.set(message); } return false; } }; inboundConnection.registerListener(listener); inboundConnection.run(); assertNotNull(inboundMessage.get()); assertEquals("foo", inboundMessage.get().getPayload()); assertEquals("baz", inboundMessage.get().getHeaders().get("bar")); }