use of org.springframework.messaging.support.ErrorMessage in project spring-integration by spring-projects.
the class TcpNioConnectionTests method testAllMessagesDelivered.
@Test
public void testAllMessagesDelivered() throws Exception {
final int numberOfSockets = 10;
TcpNioServerConnectionFactory factory = new TcpNioServerConnectionFactory(0);
factory.setApplicationEventPublisher(nullPublisher);
CompositeExecutor compositeExec = compositeExecutor();
factory.setTaskExecutor(compositeExec);
final CountDownLatch latch = new CountDownLatch(numberOfSockets * 4);
factory.registerListener(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(1);
}
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(1);
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(1);
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();
cleanupCompositeExecutor(compositeExec);
}
use of org.springframework.messaging.support.ErrorMessage in project spring-integration by spring-projects.
the class ConnectionTimeoutTests method testNetReplyTimeout.
/**
* Ensure we do timeout on the read side (client) if we sent a message within the
* first timeout but the reply takes > 2 timeouts.
* @throws Exception
*/
@Test
public void testNetReplyTimeout() throws Exception {
TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory(0);
this.setupServerCallbacks(server, 4500);
final AtomicReference<Message<?>> reply = new AtomicReference<Message<?>>();
server.start();
TestingUtilities.waitListening(server, null);
TcpNetClientConnectionFactory client = new TcpNetClientConnectionFactory("localhost", server.getPort());
client.registerListener(message -> {
if (!(message instanceof ErrorMessage)) {
reply.set(message);
}
return false;
});
client.setSoTimeout(2000);
CountDownLatch clientCloseLatch = getCloseLatch(client);
setupClientCallback(client);
client.start();
TcpConnection connection = client.getConnection();
Socket socket = TestUtils.getPropertyValue(connection, "socket", Socket.class);
assertEquals(2000, socket.getSoTimeout());
Thread.sleep(1000);
connection.send(MessageBuilder.withPayload("foo").build());
Thread.sleep(1400);
assertTrue(connection.isOpen());
assertTrue(clientCloseLatch.await(2000, TimeUnit.SECONDS));
assertNull(reply.get());
assertFalse(connection.isOpen());
server.stop();
client.stop();
}
use of org.springframework.messaging.support.ErrorMessage in project spring-integration by spring-projects.
the class ConnectionTimeoutTests method testNioReplyTimeout.
/**
* Ensure we do timeout on the read side (client) if we sent a message within the
* first timeout but the reply takes > 2 timeouts.
* @throws Exception
*/
@Test
public void testNioReplyTimeout() throws Exception {
TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory(0);
this.setupServerCallbacks(server, 2100);
final AtomicReference<Message<?>> reply = new AtomicReference<Message<?>>();
server.start();
TestingUtilities.waitListening(server, null);
TcpNioClientConnectionFactory client = new TcpNioClientConnectionFactory("localhost", server.getPort());
client.registerListener(message -> {
if (!(message instanceof ErrorMessage)) {
reply.set(message);
}
return false;
});
client.setSoTimeout(1000);
CountDownLatch clientCloseLatch = getCloseLatch(client);
setupClientCallback(client);
client.start();
TcpConnection connection = client.getConnection();
Thread.sleep(500);
connection.send(MessageBuilder.withPayload("foo").build());
Thread.sleep(700);
assertTrue(connection.isOpen());
assertTrue(clientCloseLatch.await(2, TimeUnit.SECONDS));
assertNull(reply.get());
assertFalse(connection.isOpen());
server.stop();
client.stop();
}
use of org.springframework.messaging.support.ErrorMessage in project spring-integration by spring-projects.
the class ConnectionTimeoutTests method notTimeoutGuts.
private void notTimeoutGuts(AbstractServerConnectionFactory server, AbstractClientConnectionFactory client) throws Exception, InterruptedException {
final AtomicReference<Message<?>> reply = new AtomicReference<Message<?>>();
final CountDownLatch replyLatch = new CountDownLatch(1);
client.registerListener(message -> {
if (!(message instanceof ErrorMessage)) {
reply.set(message);
replyLatch.countDown();
}
return false;
});
client.setSoTimeout(2000);
CountDownLatch clientClosedLatch = getCloseLatch(client);
setupClientCallback(client);
client.start();
TcpConnection connection = client.getConnection();
Thread.sleep(1000);
connection.send(MessageBuilder.withPayload("foo").build());
assertTrue(replyLatch.await(5, TimeUnit.SECONDS));
assertNotNull(reply.get());
assertTrue(clientClosedLatch.await(10, TimeUnit.SECONDS));
assertFalse(connection.isOpen());
server.stop();
client.stop();
}
use of org.springframework.messaging.support.ErrorMessage in project spring-integration by spring-projects.
the class TcpNioConnectionReadTests method testReadCrLfOverflow.
@Test
public void testReadCrLfOverflow() throws Exception {
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
serializer.setMaxMessageSize(1024);
final Semaphore semaphore = new Semaphore(0);
final List<TcpConnection> added = new ArrayList<TcpConnection>();
final List<TcpConnection> removed = new ArrayList<TcpConnection>();
final CountDownLatch errorMessageLetch = new CountDownLatch(1);
final AtomicReference<Throwable> errorMessageRef = new AtomicReference<Throwable>();
AbstractServerConnectionFactory scf = getConnectionFactory(serializer, message -> {
if (message instanceof ErrorMessage) {
errorMessageRef.set(((ErrorMessage) message).getPayload());
errorMessageLetch.countDown();
}
return false;
}, new TcpSender() {
@Override
public void addNewConnection(TcpConnection connection) {
added.add(connection);
semaphore.release();
}
@Override
public void removeDeadConnection(TcpConnection connection) {
removed.add(connection);
semaphore.release();
}
});
// Fire up the sender.
CountDownLatch done = SocketTestUtils.testSendCrLfOverflow(scf.getPort());
whileOpen(semaphore, added);
assertEquals(1, added.size());
assertTrue(errorMessageLetch.await(10, TimeUnit.SECONDS));
assertThat(errorMessageRef.get().getMessage(), anyOf(containsString("Connection is closed"), containsString("CRLF not found before max message length: 1024")));
assertTrue(semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS));
assertTrue(removed.size() > 0);
scf.stop();
done.countDown();
}
Aggregations