下面列出了怎么用org.springframework.messaging.MessagingException的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void duplicateError() {
int productId = 1;
assertNull(repository.findByProductId(productId).block());
sendCreateProductEvent(productId);
assertNotNull(repository.findByProductId(productId).block());
try {
sendCreateProductEvent(productId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: " + productId, iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
}
@Test
public void duplicateError() {
int productId = 1;
int recommendationId = 1;
sendCreateRecommendationEvent(productId, recommendationId);
assertEquals(1, (long)repository.count().block());
try {
sendCreateRecommendationEvent(productId, recommendationId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: 1, Recommendation Id:1", iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
assertEquals(1, (long)repository.count().block());
}
@Test
public void duplicateError() {
int productId = 1;
assertNull(repository.findByProductId(productId).block());
sendCreateProductEvent(productId);
assertNotNull(repository.findByProductId(productId).block());
try {
sendCreateProductEvent(productId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: " + productId, iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
}
/**
* Same to {@link #syncSendOrderly(String, Message, String)} with send timeout specified in addition.
*
* @param destination formats: `topicName:tags`
* @param message {@link org.springframework.messaging.Message}
* @param hashKey use this key to select queue. for example: orderId, productId ...
* @param timeout send timeout with millis
* @return {@link SendResult}
*/
public SendResult syncSendOrderly(String destination, Message<?> message, String hashKey, long timeout) {
if (Objects.isNull(message) || Objects.isNull(message.getPayload())) {
log.error("syncSendOrderly failed. destination:{}, message is null ", destination);
throw new IllegalArgumentException("`message` and `message.payload` cannot be null");
}
try {
long now = System.currentTimeMillis();
org.apache.rocketmq.common.message.Message rocketMsg = this.createRocketMqMessage(destination, message);
SendResult sendResult = producer.send(rocketMsg, messageQueueSelector, hashKey, timeout);
long costTime = System.currentTimeMillis() - now;
if (log.isDebugEnabled()) {
log.debug("send message cost: {} ms, msgId:{}", costTime, sendResult.getMsgId());
}
return sendResult;
} catch (Exception e) {
log.error("syncSendOrderly failed. destination:{}, message:{} ", destination, message);
throw new MessagingException(e.getMessage(), e);
}
}
@Test
public void duplicateError() {
int productId = 1;
assertNull(repository.findByProductId(productId).block());
sendCreateProductEvent(productId);
assertNotNull(repository.findByProductId(productId).block());
try {
sendCreateProductEvent(productId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: " + productId, iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
}
@Test
public void duplicateError() {
int productId = 1;
int recommendationId = 1;
sendCreateRecommendationEvent(productId, recommendationId);
assertEquals(1, (long)repository.count().block());
try {
sendCreateRecommendationEvent(productId, recommendationId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: 1, Recommendation Id:1", iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
assertEquals(1, (long)repository.count().block());
}
private void testExtRocketMQTemplateTransaction() throws MessagingException {
for (int i = 0; i < 10; i++) {
try {
Message msg = MessageBuilder.withPayload("extRocketMQTemplate transactional message " + i).
setHeader(RocketMQHeaders.TRANSACTION_ID, "KEY_" + i).build();
SendResult sendResult = extRocketMQTemplate.sendMessageInTransaction(
springTransTopic, msg, null);
System.out.printf("------ExtRocketMQTemplate send Transactional msg body = %s , sendResult=%s %n",
msg.getPayload(), sendResult.getSendStatus());
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Test
public void duplicateError() {
int productId = 1;
int recommendationId = 1;
sendCreateRecommendationEvent(productId, recommendationId);
assertEquals(1, (long)repository.count().block());
try {
sendCreateRecommendationEvent(productId, recommendationId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: 1, Recommendation Id:1", iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
assertEquals(1, (long)repository.count().block());
}
@Test
public void duplicateError() {
int productId = 1;
int reviewId = 1;
assertEquals(0, repository.count());
sendCreateReviewEvent(productId, reviewId);
assertEquals(1, repository.count());
try {
sendCreateReviewEvent(productId, reviewId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: 1, Review Id:1", iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
assertEquals(1, repository.count());
}
private void testRocketMQTemplateTransaction() throws MessagingException {
String[] tags = new String[] {"TagA", "TagB", "TagC", "TagD", "TagE"};
for (int i = 0; i < 10; i++) {
try {
Message msg = MessageBuilder.withPayload("rocketMQTemplate transactional message " + i).
setHeader(RocketMQHeaders.TRANSACTION_ID, "KEY_" + i).build();
SendResult sendResult = rocketMQTemplate.sendMessageInTransaction(
springTransTopic + ":" + tags[i % tags.length], msg, null);
System.out.printf("------rocketMQTemplate send Transactional msg body = %s , sendResult=%s %n",
msg.getPayload(), sendResult.getSendStatus());
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Same to {@link #asyncSend(String, Message, SendCallback)} with send timeout and delay level specified in
* addition.
*
* @param destination formats: `topicName:tags`
* @param message {@link org.springframework.messaging.Message}
* @param sendCallback {@link SendCallback}
* @param timeout send timeout with millis
* @param delayLevel level for the delay message
*/
public void asyncSend(String destination, Message<?> message, SendCallback sendCallback, long timeout,
int delayLevel) {
if (Objects.isNull(message) || Objects.isNull(message.getPayload())) {
log.error("asyncSend failed. destination:{}, message is null ", destination);
throw new IllegalArgumentException("`message` and `message.payload` cannot be null");
}
try {
org.apache.rocketmq.common.message.Message rocketMsg = this.createRocketMqMessage(destination, message);
if (delayLevel > 0) {
rocketMsg.setDelayTimeLevel(delayLevel);
}
producer.send(rocketMsg, sendCallback, timeout);
} catch (Exception e) {
log.info("asyncSend failed. destination:{}, message:{} ", destination, message);
throw new MessagingException(e.getMessage(), e);
}
}
@Test
public void duplicateError() {
int productId = 1;
int reviewId = 1;
assertEquals(0, repository.count());
sendCreateReviewEvent(productId, reviewId);
assertEquals(1, repository.count());
try {
sendCreateReviewEvent(productId, reviewId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: 1, Review Id:1", iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
assertEquals(1, repository.count());
}
@Test
public void duplicateError() {
int productId = 1;
int reviewId = 1;
assertEquals(0, repository.count());
sendCreateReviewEvent(productId, reviewId);
assertEquals(1, repository.count());
try {
sendCreateReviewEvent(productId, reviewId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: 1, Review Id:1", iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
assertEquals(1, repository.count());
}
@Test
public void duplicateError() {
int productId = 1;
int reviewId = 1;
assertEquals(0, repository.count());
sendCreateReviewEvent(productId, reviewId);
assertEquals(1, repository.count());
try {
sendCreateReviewEvent(productId, reviewId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: 1, Review Id:1", iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
assertEquals(1, repository.count());
}
@Test
public void duplicateError() {
int productId = 1;
int reviewId = 1;
assertEquals(0, repository.count());
sendCreateReviewEvent(productId, reviewId);
assertEquals(1, repository.count());
try {
sendCreateReviewEvent(productId, reviewId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: 1, Review Id:1", iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
assertEquals(1, repository.count());
}
@Test
public void duplicateError() {
int productId = 1;
int recommendationId = 1;
sendCreateRecommendationEvent(productId, recommendationId);
assertEquals(1, (long)repository.count().block());
try {
sendCreateRecommendationEvent(productId, recommendationId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: 1, Recommendation Id:1", iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
assertEquals(1, (long)repository.count().block());
}
private void testTransaction() throws MessagingException {
String[] tags = new String[]{"TagA", "TagB", "TagC", "TagD", "TagE"};
for (int i = 0; i < 10; i++) {
try {
Message msg = MessageBuilder.withPayload("Hello RocketMQ " + i).
setHeader(RocketMQHeaders.TRANSACTION_ID, "KEY_" + i).build();
SendResult sendResult = rocketMQTemplate.sendMessageInTransaction(
springTransTopic + ":" + tags[i % tags.length], msg, null);
System.out.printf("------ send Transactional msg body = %s , sendResult=%s %n",
msg.getPayload(), sendResult.getSendStatus());
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Test
public void duplicateError() {
int productId = 1;
assertNull(repository.findByProductId(productId).block());
sendCreateProductEvent(productId);
assertNotNull(repository.findByProductId(productId).block());
try {
sendCreateProductEvent(productId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: " + productId, iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
}
@Test
public void duplicateError() {
int productId = 1;
int reviewId = 1;
assertEquals(0, repository.count());
sendCreateReviewEvent(productId, reviewId);
assertEquals(1, repository.count());
try {
sendCreateReviewEvent(productId, reviewId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: 1, Review Id:1", iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
assertEquals(1, repository.count());
}
@Test
public void duplicateError() {
int productId = 1;
int recommendationId = 1;
sendCreateRecommendationEvent(productId, recommendationId);
assertEquals(1, (long)repository.count().block());
try {
sendCreateRecommendationEvent(productId, recommendationId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: 1, Recommendation Id:1", iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
assertEquals(1, (long)repository.count().block());
}
/**
* Same to {@link #syncSend(String, Message)} with send timeout specified in addition.
*
* @param destination formats: `topicName:tags`
* @param message {@link org.springframework.messaging.Message}
* @param timeout send timeout with millis
* @param delayLevel level for the delay message
* @return {@link SendResult}
*/
public SendResult syncSend(String destination, Message<?> message, long timeout, int delayLevel) {
if (Objects.isNull(message) || Objects.isNull(message.getPayload())) {
log.error("syncSend failed. destination:{}, message is null ", destination);
throw new IllegalArgumentException("`message` and `message.payload` cannot be null");
}
try {
long now = System.currentTimeMillis();
org.apache.rocketmq.common.message.Message rocketMsg = this.createRocketMqMessage(destination, message);
if (delayLevel > 0) {
rocketMsg.setDelayTimeLevel(delayLevel);
}
SendResult sendResult = producer.send(rocketMsg, timeout);
long costTime = System.currentTimeMillis() - now;
if (log.isDebugEnabled()) {
log.debug("send message cost: {} ms, msgId:{}", costTime, sendResult.getMsgId());
}
return sendResult;
} catch (Exception e) {
log.error("syncSend failed. destination:{}, message:{} ", destination, message);
throw new MessagingException(e.getMessage(), e);
}
}
@Test
public void duplicateError() {
int productId = 1;
int recommendationId = 1;
sendCreateRecommendationEvent(productId, recommendationId);
assertEquals(1, (long)repository.count().block());
try {
sendCreateRecommendationEvent(productId, recommendationId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: 1, Recommendation Id:1", iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
assertEquals(1, (long)repository.count().block());
}
@Test
public void duplicateError() {
int productId = 1;
assertNull(repository.findByProductId(productId).block());
sendCreateProductEvent(productId);
assertNotNull(repository.findByProductId(productId).block());
try {
sendCreateProductEvent(productId);
fail("Expected a MessagingException here!");
} catch (MessagingException me) {
if (me.getCause() instanceof InvalidInputException) {
InvalidInputException iie = (InvalidInputException)me.getCause();
assertEquals("Duplicate key, Product Id: " + productId, iie.getMessage());
} else {
fail("Expected a InvalidInputException as the root cause!");
}
}
}
@Override
public void convertAndSend(Object payload, @Nullable MessagePostProcessor postProcessor) throws MessagingException {
Destination defaultDestination = getDefaultDestination();
if (defaultDestination != null) {
convertAndSend(defaultDestination, payload, postProcessor);
}
else {
convertAndSend(getRequiredDefaultDestinationName(), payload, postProcessor);
}
}
@Override
@Nullable
public <T> T convertSendAndReceive(String destinationName, Object request, Class<T> targetClass)
throws MessagingException {
return convertSendAndReceive(destinationName, request, null, targetClass);
}
@Override
@Nullable
public <T> T convertSendAndReceive(String destinationName, Object request, Class<T> targetClass,
@Nullable MessagePostProcessor requestPostProcessor) throws MessagingException {
return convertSendAndReceive(destinationName, request, null, targetClass, requestPostProcessor);
}
@Nullable
public Message<?> preHandle(Message<?> message) throws MessagingException {
String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders());
if (!getBroadcastDestination().equals(destination)) {
return message;
}
SimpMessageHeaderAccessor accessor =
SimpMessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);
Assert.state(accessor != null, "No SimpMessageHeaderAccessor");
if (accessor.getSessionId() == null) {
// Our own broadcast
return null;
}
destination = accessor.getFirstNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION);
if (logger.isTraceEnabled()) {
logger.trace("Checking unresolved user destination: " + destination);
}
SimpMessageHeaderAccessor newAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
for (String name : accessor.toNativeHeaderMap().keySet()) {
if (NO_COPY_LIST.contains(name)) {
continue;
}
newAccessor.setNativeHeader(name, accessor.getFirstNativeHeader(name));
}
if (destination != null) {
newAccessor.setDestination(destination);
}
newAccessor.setHeader(SimpMessageHeaderAccessor.IGNORE_ERROR, true); // ensure send doesn't block
return MessageBuilder.createMessage(message.getPayload(), newAccessor.getMessageHeaders());
}
@Override
public Mono<Void> handleMessage(Message<?> message) throws MessagingException {
Match<T> match = getHandlerMethod(message);
if (match == null) {
// handleNoMatch would have been invoked already
return Mono.empty();
}
return handleMatch(match.mapping, match.handlerMethod, message);
}
@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;
}
}
@Test
public void convertMessageNotReadableException() throws JMSException {
willThrow(MessageNotReadableException.class).given(this.jmsTemplate).receive("myQueue");
this.thrown.expect(MessagingException.class);
this.messagingTemplate.receive("myQueue");
}