use of org.springframework.core.serializer.DefaultSerializer in project spring-integration by spring-projects.
the class TcpMessageMapperTests method testMapMessageConvertingBothWaysJava.
@Test
public void testMapMessageConvertingBothWaysJava() throws Exception {
Message<String> outMessage = MessageBuilder.withPayload("foo").setHeader("bar", "baz").build();
MapMessageConverter converter = new MapMessageConverter();
converter.setHeaderNames("bar");
MessageConvertingTcpMessageMapper mapper = new MessageConvertingTcpMessageMapper(converter);
Map<?, ?> map = (Map<?, ?>) mapper.fromMessage(outMessage);
DefaultSerializer serializer = new DefaultSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
serializer.serialize(map, baos);
DefaultDeserializer deserializer = new DefaultDeserializer();
map = (Map<?, ?>) deserializer.deserialize(new ByteArrayInputStream(baos.toByteArray()));
TcpConnection connection = mock(TcpConnection.class);
when(connection.getPayload()).thenReturn(map);
when(connection.getHostName()).thenReturn("someHost");
when(connection.getHostAddress()).thenReturn("1.1.1.1");
when(connection.getPort()).thenReturn(1234);
when(connection.getConnectionId()).thenReturn("someId");
Message<?> message = mapper.toMessage(connection);
assertEquals("foo", message.getPayload());
assertEquals("baz", message.getHeaders().get("bar"));
assertEquals("someHost", message.getHeaders().get(IpHeaders.HOSTNAME));
assertEquals("1.1.1.1", message.getHeaders().get(IpHeaders.IP_ADDRESS));
assertEquals(1234, message.getHeaders().get(IpHeaders.REMOTE_PORT));
assertEquals("someId", message.getHeaders().get(IpHeaders.CONNECTION_ID));
}
use of org.springframework.core.serializer.DefaultSerializer in project spring-integration by spring-projects.
the class TcpReceivingChannelAdapterTests method singleNoOutboundInterceptorsGuts.
private void singleNoOutboundInterceptorsGuts(AbstractServerConnectionFactory scf) throws Exception {
scf.setSerializer(new DefaultSerializer());
scf.setDeserializer(new DefaultDeserializer());
scf.setSingleUse(true);
scf.setSoTimeout(10000);
TcpConnectionInterceptorFactoryChain fc = new TcpConnectionInterceptorFactoryChain();
fc.setInterceptors(new TcpConnectionInterceptorFactory[] { newInterceptorFactory(), newInterceptorFactory() });
scf.setInterceptorFactoryChain(fc);
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setConnectionFactory(scf);
scf.start();
TestingUtilities.waitListening(scf, null);
int port = scf.getPort();
QueueChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
socket.setSoTimeout(10000);
new ObjectOutputStream(socket.getOutputStream()).writeObject("Hello");
assertEquals("world!", new ObjectInputStream(socket.getInputStream()).readObject());
new ObjectOutputStream(socket.getOutputStream()).writeObject("Hello");
assertEquals("world!", new ObjectInputStream(socket.getInputStream()).readObject());
new ObjectOutputStream(socket.getOutputStream()).writeObject("Test1");
socket = SocketFactory.getDefault().createSocket("localhost", port);
new ObjectOutputStream(socket.getOutputStream()).writeObject("Hello");
assertEquals("world!", new ObjectInputStream(socket.getInputStream()).readObject());
new ObjectOutputStream(socket.getOutputStream()).writeObject("Hello");
assertEquals("world!", new ObjectInputStream(socket.getInputStream()).readObject());
new ObjectOutputStream(socket.getOutputStream()).writeObject("Test2");
Message<?> message = channel.receive(10000);
assertNotNull(message);
// with single use, results may come back in a different order
Set<Object> results = new HashSet<Object>();
results.add(message.getPayload());
message = channel.receive(10000);
assertNotNull(message);
results.add(message.getPayload());
assertTrue(results.contains("Test1"));
assertTrue(results.contains("Test2"));
}
use of org.springframework.core.serializer.DefaultSerializer in project spring-integration by spring-projects.
the class TcpReceivingChannelAdapterTests method singleSharedInterceptorsGuts.
private void singleSharedInterceptorsGuts(AbstractServerConnectionFactory scf) throws Exception {
scf.setSerializer(new DefaultSerializer());
scf.setDeserializer(new DefaultDeserializer());
scf.setSingleUse(true);
scf.setSoTimeout(60000);
TcpConnectionInterceptorFactoryChain fc = new TcpConnectionInterceptorFactoryChain();
fc.setInterceptors(new TcpConnectionInterceptorFactory[] { newInterceptorFactory(), newInterceptorFactory() });
scf.setInterceptorFactoryChain(fc);
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
handler.setConnectionFactory(scf);
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setConnectionFactory(scf);
scf.start();
QueueChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
TestingUtilities.waitListening(scf, null);
int port = scf.getPort();
Socket socket1 = SocketFactory.getDefault().createSocket("localhost", port);
socket1.setSoTimeout(60000);
new ObjectOutputStream(socket1.getOutputStream()).writeObject("Hello");
assertEquals("world!", new ObjectInputStream(socket1.getInputStream()).readObject());
new ObjectOutputStream(socket1.getOutputStream()).writeObject("Hello");
assertEquals("world!", new ObjectInputStream(socket1.getInputStream()).readObject());
new ObjectOutputStream(socket1.getOutputStream()).writeObject("Test1");
Socket socket2 = SocketFactory.getDefault().createSocket("localhost", port);
socket2.setSoTimeout(60000);
new ObjectOutputStream(socket2.getOutputStream()).writeObject("Hello");
assertEquals("world!", new ObjectInputStream(socket2.getInputStream()).readObject());
new ObjectOutputStream(socket2.getOutputStream()).writeObject("Hello");
assertEquals("world!", new ObjectInputStream(socket2.getInputStream()).readObject());
new ObjectOutputStream(socket2.getOutputStream()).writeObject("Test2");
Message<?> message = channel.receive(10000);
assertNotNull(message);
handler.handleMessage(message);
message = channel.receive(10000);
assertNotNull(message);
handler.handleMessage(message);
assertEquals("Test1", new ObjectInputStream(socket1.getInputStream()).readObject());
assertEquals("Test2", new ObjectInputStream(socket2.getInputStream()).readObject());
}
use of org.springframework.core.serializer.DefaultSerializer in project spring-integration by spring-projects.
the class TcpOutboundGatewayTests method testFailoverGWPropagatesSocketClose.
@Test
public void testFailoverGWPropagatesSocketClose() throws Exception {
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0);
final int port = serverSocket.getLocalPort();
AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
ccf.setSerializer(new DefaultSerializer());
ccf.setDeserializer(new DefaultDeserializer());
ccf.setSoTimeout(10000);
ccf.setSingleUse(false);
FailoverClientConnectionFactory focf = new FailoverClientConnectionFactory(Collections.singletonList(ccf));
focf.start();
testGWPropagatesSocketCloseGuts(port, focf, serverSocket);
serverSocket.close();
}
use of org.springframework.core.serializer.DefaultSerializer in project spring-integration by spring-projects.
the class TcpOutboundGatewayTests method testGoodNetTimeout.
@Test
public void testGoodNetTimeout() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
final AtomicReference<ServerSocket> serverSocket = new AtomicReference<>();
this.executor.execute(() -> {
try {
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
serverSocket.set(server);
latch.countDown();
int i = 0;
Socket socket = server.accept();
while (true) {
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
ois.readObject();
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
Thread.sleep(1000);
oos.writeObject("Reply" + (i++));
}
} catch (Exception e) {
if (!done.get()) {
e.printStackTrace();
}
}
});
assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
ccf.setSerializer(new DefaultSerializer());
ccf.setDeserializer(new DefaultDeserializer());
ccf.setSoTimeout(10000);
ccf.setSingleUse(false);
ccf.start();
final TcpOutboundGateway gateway = new TcpOutboundGateway();
gateway.setConnectionFactory(ccf);
gateway.setRequestTimeout(1);
QueueChannel replyChannel = new QueueChannel();
gateway.setRequiresReply(true);
gateway.setOutputChannel(replyChannel);
@SuppressWarnings("unchecked") Future<Integer>[] results = (Future<Integer>[]) new Future<?>[2];
for (int i = 0; i < 2; i++) {
final int j = i;
results[j] = (this.executor.submit(() -> {
gateway.handleMessage(MessageBuilder.withPayload("Test" + j).build());
return 0;
}));
}
Set<String> replies = new HashSet<>();
int timeouts = 0;
for (int i = 0; i < 2; i++) {
try {
results[i].get();
} catch (ExecutionException e) {
if (timeouts > 0) {
fail("Unexpected " + e.getMessage());
} else {
assertNotNull(e.getCause());
assertTrue(e.getCause() instanceof MessageTimeoutException);
}
timeouts++;
continue;
}
Message<?> m = replyChannel.receive(10000);
assertNotNull(m);
replies.add((String) m.getPayload());
}
if (timeouts < 1) {
fail("Expected ExecutionException");
}
for (int i = 0; i < 1; i++) {
assertTrue(replies.remove("Reply" + i));
}
done.set(true);
gateway.stop();
ccf.stop();
serverSocket.get().close();
}
Aggregations