use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class ConnectionEventTests method testOutboundGatewayNoConnectionEvents.
@Test
public void testOutboundGatewayNoConnectionEvents() {
TcpOutboundGateway gw = new TcpOutboundGateway();
AbstractClientConnectionFactory ccf = new AbstractClientConnectionFactory("localhost", 0) {
};
final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<ApplicationEvent>();
ccf.setApplicationEventPublisher(new ApplicationEventPublisher() {
@Override
public void publishEvent(Object event) {
}
@Override
public void publishEvent(ApplicationEvent event) {
theEvent.set(event);
}
});
gw.setConnectionFactory(ccf);
DirectChannel requestChannel = new DirectChannel();
requestChannel.subscribe(message -> ((MessageChannel) message.getHeaders().getReplyChannel()).send(message));
gw.start();
Message<String> message = MessageBuilder.withPayload("foo").setHeader(IpHeaders.CONNECTION_ID, "bar").build();
gw.onMessage(message);
assertNotNull(theEvent.get());
TcpConnectionFailedCorrelationEvent event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
assertEquals("bar", event.getConnectionId());
MessagingException messagingException = (MessagingException) event.getCause();
assertSame(message, messagingException.getFailedMessage());
assertEquals("Cannot correlate response - no pending reply for bar", messagingException.getMessage());
message = new GenericMessage<String>("foo");
gw.onMessage(message);
assertNotNull(theEvent.get());
event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
assertNull(event.getConnectionId());
messagingException = (MessagingException) event.getCause();
assertSame(message, messagingException.getFailedMessage());
assertEquals("Cannot correlate response - no connection id", messagingException.getMessage());
gw.stop();
ccf.stop();
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class ConnectionFactoryShutDownTests method testShutdownDoesntDeadlock.
@Test
public void testShutdownDoesntDeadlock() throws Exception {
final AbstractConnectionFactory factory = new AbstractConnectionFactory(0) {
@Override
public TcpConnection getConnection() {
return null;
}
};
factory.setActive(true);
Executor executor = factory.getTaskExecutor();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
executor.execute(() -> {
latch1.countDown();
try {
while (true) {
factory.getTaskExecutor();
Thread.sleep(10);
}
} catch (MessagingException e1) {
} catch (InterruptedException e2) {
Thread.currentThread().interrupt();
}
latch2.countDown();
});
assertTrue(latch1.await(10, TimeUnit.SECONDS));
StopWatch watch = new StopWatch();
watch.start();
factory.stop();
watch.stop();
assertTrue("Expected < 10000, was: " + watch.getLastTaskTimeMillis(), watch.getLastTaskTimeMillis() < 10000);
assertTrue(latch1.await(10, TimeUnit.SECONDS));
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class HelloWorldInterceptor method onMessage.
@Override
public boolean onMessage(Message<?> message) {
if (!this.negotiated) {
synchronized (this) {
if (!this.negotiated) {
Object payload = message.getPayload();
logger.debug(this.toString() + " received " + payload);
if (this.isServer()) {
if (payload.equals(hello)) {
try {
logger.debug(this.toString() + " sending " + this.world);
super.send(MessageBuilder.withPayload(world).build());
this.negotiated = true;
return true;
} catch (Exception e) {
throw new MessagingException("Negotiation error", e);
}
} else {
throw new MessagingException("Negotiation error, expected '" + hello + "' received '" + payload + "'");
}
} else {
if (payload.equals(world)) {
this.negotiated = true;
this.negotiationSemaphore.release();
} else {
throw new MessagingException("Negotiation error - expected '" + world + "' received " + payload);
}
return true;
}
}
}
}
try {
return super.onMessage(message);
} finally {
// on the server side, we don't want to close if we are expecting a response
if (!(this.isServer() && this.hasRealSender()) && !this.pendingSend) {
this.checkDeferredClose();
}
}
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class TcpNetConnection method send.
@Override
@SuppressWarnings("unchecked")
public synchronized void send(Message<?> message) throws Exception {
if (this.socketOutputStream == null) {
int writeBufferSize = this.socket.getSendBufferSize();
this.socketOutputStream = new BufferedOutputStream(this.socket.getOutputStream(), writeBufferSize > 0 ? writeBufferSize : 8192);
}
Object object = this.getMapper().fromMessage(message);
this.lastSend = System.currentTimeMillis();
try {
((Serializer<Object>) this.getSerializer()).serialize(object, this.socketOutputStream);
this.socketOutputStream.flush();
} catch (Exception e) {
this.publishConnectionExceptionEvent(new MessagingException(message, "Failed TCP serialization", e));
this.closeConnection(true);
throw e;
}
if (logger.isDebugEnabled()) {
logger.debug(getConnectionId() + " Message sent " + message);
}
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class TcpNioConnection method send.
@Override
@SuppressWarnings("unchecked")
public void send(Message<?> message) throws Exception {
synchronized (this.socketChannel) {
if (this.bufferedOutputStream == null) {
int writeBufferSize = this.socketChannel.socket().getSendBufferSize();
this.bufferedOutputStream = new BufferedOutputStream(this.getChannelOutputStream(), writeBufferSize > 0 ? writeBufferSize : 8192);
}
Object object = this.getMapper().fromMessage(message);
this.lastSend = System.currentTimeMillis();
try {
((Serializer<Object>) this.getSerializer()).serialize(object, this.bufferedOutputStream);
this.bufferedOutputStream.flush();
} catch (Exception e) {
this.publishConnectionExceptionEvent(new MessagingException(message, "Failed TCP serialization", e));
this.closeConnection(true);
throw e;
}
if (logger.isDebugEnabled()) {
logger.debug(getConnectionId() + " Message sent " + message);
}
}
}
Aggregations