use of org.springframework.messaging.MessagingException in project spring-cloud-stream by spring-cloud.
the class BinderAwareChannelResolverTests method resolveChannel.
@Test
public void resolveChannel() {
Map<String, Bindable> bindables = context.getBeansOfType(Bindable.class);
assertThat(bindables).hasSize(1);
for (Bindable bindable : bindables.values()) {
// producer
assertEquals(0, bindable.getInputs().size());
// consumer
assertEquals(0, bindable.getOutputs().size());
}
MessageChannel registered = resolver.resolveDestination("foo");
assertEquals(2, ((AbstractMessageChannel) registered).getChannelInterceptors().size());
assertTrue(((AbstractMessageChannel) registered).getChannelInterceptors().get(1) instanceof ImmutableMessageChannelInterceptor);
bindables = context.getBeansOfType(Bindable.class);
assertThat(bindables).hasSize(1);
for (Bindable bindable : bindables.values()) {
// producer
assertEquals(0, bindable.getInputs().size());
// consumer
assertEquals(1, bindable.getOutputs().size());
}
DirectChannel testChannel = new DirectChannel();
testChannel.setComponentName("INPUT");
final CountDownLatch latch = new CountDownLatch(1);
final List<Message<?>> received = new ArrayList<>();
testChannel.subscribe(new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
received.add(message);
latch.countDown();
}
});
this.binder.bindConsumer("foo", null, testChannel, new ConsumerProperties());
assertThat(received).hasSize(0);
registered.send(MessageBuilder.withPayload("hello").build());
try {
assertThat(latch.await(1, TimeUnit.SECONDS)).describedAs("Latch timed out");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
fail("interrupted while awaiting latch");
}
assertThat(received).hasSize(1);
assertThat(new String((byte[]) received.get(0).getPayload())).isEqualTo("hello");
this.context.close();
for (Bindable bindable : bindables.values()) {
assertEquals(0, bindable.getInputs().size());
// Must not be bound"
assertEquals(0, bindable.getOutputs().size());
}
}
use of org.springframework.messaging.MessagingException in project spring-cloud-stream by spring-cloud.
the class ExtendedPropertiesBinderAwareChannelResolverTests method resolveChannel.
@Test
@Override
public void resolveChannel() {
Map<String, Bindable> bindables = context.getBeansOfType(Bindable.class);
assertThat(bindables).hasSize(1);
for (Bindable bindable : bindables.values()) {
// producer
assertEquals(0, bindable.getInputs().size());
// consumer
assertEquals(0, bindable.getOutputs().size());
}
MessageChannel registered = resolver.resolveDestination("foo");
bindables = context.getBeansOfType(Bindable.class);
assertThat(bindables).hasSize(1);
for (Bindable bindable : bindables.values()) {
// producer
assertEquals(0, bindable.getInputs().size());
// consumer
assertEquals(1, bindable.getOutputs().size());
}
DirectChannel testChannel = new DirectChannel();
final CountDownLatch latch = new CountDownLatch(1);
final List<Message<?>> received = new ArrayList<>();
testChannel.subscribe(new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
received.add(message);
latch.countDown();
}
});
binder.bindConsumer("foo", null, testChannel, new ExtendedConsumerProperties<ConsumerProperties>(new ConsumerProperties()));
assertThat(received).hasSize(0);
registered.send(MessageBuilder.withPayload("hello").build());
try {
assertThat(latch.await(1, TimeUnit.SECONDS)).describedAs("latch timed out");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
fail("interrupted while awaiting latch");
}
assertThat(received).hasSize(1);
assertThat(new String((byte[]) received.get(0).getPayload())).isEqualTo("hello");
context.close();
for (Bindable bindable : bindables.values()) {
assertEquals(0, bindable.getInputs().size());
// Must not be bound"
assertEquals(0, bindable.getOutputs().size());
}
}
use of org.springframework.messaging.MessagingException in project spring-framework by spring-projects.
the class SubProtocolWebSocketHandler method handleMessage.
/**
* Handle an outbound Spring Message to a WebSocket client.
*/
@Override
public void handleMessage(Message<?> message) throws MessagingException {
String sessionId = resolveSessionId(message);
if (sessionId == null) {
if (logger.isErrorEnabled()) {
logger.error("Could not find session id in " + message);
}
return;
}
WebSocketSessionHolder holder = this.sessions.get(sessionId);
if (holder == null) {
if (logger.isDebugEnabled()) {
// The broker may not have removed the session yet
logger.debug("No session for " + message);
}
return;
}
WebSocketSession session = holder.getSession();
try {
findProtocolHandler(session).handleMessageToClient(session, message);
} catch (SessionLimitExceededException ex) {
try {
if (logger.isDebugEnabled()) {
logger.debug("Terminating '" + session + "'", ex);
} else if (logger.isWarnEnabled()) {
logger.warn("Terminating '" + session + "': " + ex.getMessage());
}
this.stats.incrementLimitExceededCount();
// clear first, session may be unresponsive
clearSession(session, ex.getStatus());
session.close(ex.getStatus());
} catch (Exception secondException) {
logger.debug("Failure while closing session " + sessionId + ".", secondException);
}
} catch (Exception ex) {
// Could be part of normal workflow (e.g. browser tab closed)
if (logger.isDebugEnabled()) {
logger.debug("Failed to send message to client in " + session + ": " + message, ex);
}
}
}
use of org.springframework.messaging.MessagingException in project spring-framework by spring-projects.
the class AbstractMessageChannel method send.
@Override
public final boolean send(Message<?> message, long timeout) {
Assert.notNull(message, "Message must not be null");
Message<?> messageToUse = message;
ChannelInterceptorChain chain = new ChannelInterceptorChain();
boolean sent = false;
try {
messageToUse = chain.applyPreSend(messageToUse, this);
if (messageToUse == null) {
return false;
}
sent = sendInternal(messageToUse, timeout);
chain.applyPostSend(messageToUse, this, sent);
chain.triggerAfterSendCompletion(messageToUse, this, sent, null);
return sent;
} catch (Exception ex) {
chain.triggerAfterSendCompletion(messageToUse, this, sent, ex);
if (ex instanceof MessagingException) {
throw (MessagingException) ex;
}
throw new MessageDeliveryException(messageToUse, "Failed to send message to " + this, ex);
} catch (Throwable err) {
MessageDeliveryException ex2 = new MessageDeliveryException(messageToUse, "Failed to send message to " + this, err);
chain.triggerAfterSendCompletion(messageToUse, this, sent, ex2);
throw ex2;
}
}
use of org.springframework.messaging.MessagingException in project rocketmq-externals by apache.
the class RocketMQTemplate method sendOneWayOrderly.
/**
* Same to {@link #sendOneWay(String, Message)} with send orderly with hashKey by specified.
*
* @param destination formats: `topicName:tags`
* @param message {@link org.springframework.messaging.Message}
* @param hashKey use this key to select queue. for example: orderId, productId ...
*/
public void sendOneWayOrderly(String destination, Message<?> message, String hashKey) {
if (Objects.isNull(message) || Objects.isNull(message.getPayload())) {
log.info("sendOneWayOrderly failed. destination:{}, message is null ", destination);
throw new IllegalArgumentException("`message` and `message.payload` cannot be null");
}
try {
org.apache.rocketmq.common.message.Message rocketMsg = convertToRocketMsg(destination, message);
producer.sendOneway(rocketMsg, messageQueueSelector, hashKey);
} catch (Exception e) {
log.info("sendOneWayOrderly failed. destination:{}, message:{}", destination, message);
throw new MessagingException(e.getMessage(), e);
}
}
Aggregations