private void handle(int methodNumber, Message<?> message) { int count = 0; for (int callNumber : this.calls) { if (callNumber == methodNumber) { count++; if (count >= this.maxCallsPerMethod) { throw new MessageRejectedException(message, null); } } } this.calls.add(methodNumber); }
public void handleMessage(Message<?> message) { this.counter.incrementAndGet(); if (!this.shouldAccept) { throw new MessageRejectedException(message, "intentional test failure"); } }
@Override public void handleMessage(Message<?> message) { this.counter.incrementAndGet(); if ("bad".equals(message.getPayload().toString())) { throw new MessageRejectedException(message, "intentional test failure"); } } }
@Override public Object postProcess(Message<?> message, Object result) { if (result == null) { MessageChannel channel = getDiscardChannel(); if (channel != null) { this.messagingTemplate.send(channel, message); } if (this.throwExceptionOnRejection) { throw new MessageRejectedException(message, "MessageFilter '" + getBeanName() + "' rejected Message"); } } return result; }
@Override public boolean accept(Message<?> message) { SAXParseException[] validationExceptions = null; try { validationExceptions = this.xmlValidator.validate(this.converter.convertToSource(message.getPayload())); } catch (Exception e) { throw new MessageHandlingException(message, e); } boolean validationSuccess = ObjectUtils.isEmpty(validationExceptions); if (!validationSuccess) { if (this.throwExceptionOnRejection) { throw new MessageRejectedException(message, "Message was rejected due to XML Validation errors", new AggregatedXmlMessageValidationException(Arrays.asList(validationExceptions))); } else if (this.logger.isInfoEnabled()) { this.logger.info("Message was rejected due to XML Validation errors", new AggregatedXmlMessageValidationException(Arrays.asList(validationExceptions))); } } return validationSuccess; }
@Override protected Object doInvoke(MethodInvocation invocation, Message<?> message) throws Throwable { boolean accept = this.messageSelector.accept(message); if (!accept) { boolean discarded = false; MessageChannel theDiscardChannel = obtainDiscardChannel(); if (theDiscardChannel != null) { this.messagingTemplate.send(theDiscardChannel, message); discarded = true; } if (this.throwExceptionOnRejection) { throw new MessageRejectedException(message, "IdempotentReceiver '" + this + "' rejected duplicate Message: " + message); } if (!discarded) { invocation.getArguments()[0] = getMessageBuilderFactory() .fromMessage(message) .setHeader(IntegrationMessageHeaderAccessor.DUPLICATE_MESSAGE, true) .build(); } else { return null; } } return invocation.proceed(); }
@Test public void noFailoverNoLoadBalancingConcurrent() throws Exception { final DirectChannel channel = (DirectChannel) ac.getBean("noLoadBalancerNoFailover"); doThrow(new MessageRejectedException(message, null)).when(handlerA).handleMessage(message); UnicastingDispatcher dispatcher = channel.getDispatcher(); dispatcher.addHandler(handlerA);
dispatcher.addHandler(handler1); dispatcher.addHandler(handler2); doThrow(new MessageRejectedException(message, null)).when(handler1).handleMessage(message); final CountDownLatch start = new CountDownLatch(1); final CountDownLatch allDone = new CountDownLatch(TOTAL_EXECUTIONS);
@Test public void noFailoverNoLoadBalancing() { DirectChannel channel = (DirectChannel) ac.getBean("noLoadBalancerNoFailover"); doThrow(new MessageRejectedException(message, null)).when(handlerA).handleMessage(message); UnicastingDispatcher dispatcher = channel.getDispatcher(); dispatcher.addHandler(handlerA); dispatcher.addHandler(handlerB); try { channel.send(message); } catch (Exception e) { /* ignore */ } try { channel.send(message); } catch (Exception e) { /* ignore */ } verify(handlerA, times(2)).handleMessage(message); verify(handlerB, times(0)).handleMessage(message); }
final DirectChannel channel = (DirectChannel) ac .getBean("noLoadBalancerFailover"); doThrow(new MessageRejectedException(message, null)).when(handlerA).handleMessage(message); UnicastingDispatcher dispatcher = channel.getDispatcher(); dispatcher.addHandler(handlerA);
@Test public void noFailoverLoadBalancingConcurrent() throws Exception { final DirectChannel channel = (DirectChannel) ac.getBean("loadBalancerNoFailover"); doThrow(new MessageRejectedException(message, null)).when(handlerA).handleMessage(message); UnicastingDispatcher dispatcher = channel.getDispatcher(); dispatcher.addHandler(handlerA);
@Test public void failoverNoLoadBalancing() { DirectChannel channel = (DirectChannel) ac .getBean("noLoadBalancerFailover"); doThrow(new MessageRejectedException(message, null)).when(handlerA) .handleMessage(message); UnicastingDispatcher dispatcher = channel.getDispatcher(); dispatcher.addHandler(handlerA); dispatcher.addHandler(handlerB); InOrder inOrder = inOrder(handlerA, handlerB); try { channel.send(message); } catch (Exception e) { /* ignore */ } inOrder.verify(handlerA).handleMessage(message); inOrder.verify(handlerB).handleMessage(message); try { channel.send(message); } catch (Exception e) { /* ignore */ } inOrder.verify(handlerA).handleMessage(message); inOrder.verify(handlerB).handleMessage(message); verify(handlerA, times(2)).handleMessage(message); verify(handlerB, times(2)).handleMessage(message); }
@Test public void noFailoverLoadBalancing() { DirectChannel channel = (DirectChannel) ac.getBean("loadBalancerNoFailover"); doThrow(new MessageRejectedException(message, null)).when(handlerA).handleMessage(message); UnicastingDispatcher dispatcher = channel.getDispatcher(); dispatcher.setLoadBalancingStrategy(new RoundRobinLoadBalancingStrategy());
@Test public void testHierarchicalMapping() { IllegalArgumentException rootCause = new IllegalArgumentException("bad argument"); MessageHandlingException error = new MessageRejectedException(new GenericMessage<Object>("foo"), "failed", rootCause); ErrorMessage message = new ErrorMessage(error); ErrorMessageExceptionTypeRouter router = new ErrorMessageExceptionTypeRouter(); router.setBeanFactory(this.context); router.setApplicationContext(this.context); router.setChannelMapping(MessageHandlingException.class.getName(), "messageHandlingExceptionChannel"); router.setDefaultOutputChannel(defaultChannel); router.afterPropertiesSet(); router.handleMessage(message); assertNotNull(messageHandlingExceptionChannel.receive(1000)); assertNull(defaultChannel.receive(0)); }
class MyBookAccount { private final List<String> receivedMessages = new ArrayList<>(); ... public void accept(MyBookAccount sender, String message){ if(!loggedIn){ throw new MessageRejectedException("not online"); } receivedMessages.add(message); //you can also have a list of Objects that are like //class Message{String senderName; String message; Date reveived; /*...*/} //trigger UI update or fire property changed event that announces the list of messages has changed } public Optional<String> getLastMessage(){ return receivedMessages.isEmpty() ? Optional.empty() : Optional.of(receivedMessages.get(receivedMessages.size()-1)); }
@Override public Object postProcess(Message<?> message, Object result) { if (result == null) { MessageChannel channel = getDiscardChannel(); if (channel != null) { this.messagingTemplate.send(channel, message); } if (this.throwExceptionOnRejection) { throw new MessageRejectedException(message, "MessageFilter '" + this.getComponentName() + "' rejected Message"); } } return result; }
@Override protected Object doInvoke(MethodInvocation invocation, Message<?> message) throws Throwable { boolean accept = this.messageSelector.accept(message); if (!accept) { boolean discarded = false; MessageChannel theDiscardChannel = obtainDiscardChannel(); if (theDiscardChannel != null) { this.messagingTemplate.send(theDiscardChannel, message); discarded = true; } if (this.throwExceptionOnRejection) { throw new MessageRejectedException(message, "IdempotentReceiver '" + this + "' rejected duplicate Message: " + message); } if (!discarded) { invocation.getArguments()[0] = getMessageBuilderFactory() .fromMessage(message) .setHeader(IntegrationMessageHeaderAccessor.DUPLICATE_MESSAGE, true) .build(); } else { return null; } } return invocation.proceed(); }