/** * A shortcut factory method for creating a message with the given payload * and {@code MessageHeaders}. * <p><strong>Note:</strong> the given {@code MessageHeaders} instance is used * directly in the new message, i.e. it is not copied. * @param payload the payload to use (never {@code null}) * @param messageHeaders the headers to use (never {@code null}) * @return the created message * @since 4.1 */ @SuppressWarnings("unchecked") public static <T> Message<T> createMessage(@Nullable T payload, MessageHeaders messageHeaders) { Assert.notNull(payload, "Payload must not be null"); Assert.notNull(messageHeaders, "MessageHeaders must not be null"); if (payload instanceof Throwable) { return (Message<T>) new ErrorMessage((Throwable) payload, messageHeaders); } else { return new GenericMessage<>(payload, messageHeaders); } }
@Test public void existingHeadersModification() throws InterruptedException { Map<String, Object> map = new HashMap<>(); map.put("foo", "bar"); map.put("bar", "baz"); GenericMessage<String> message = new GenericMessage<>("payload", map); Thread.sleep(50); MessageHeaderAccessor accessor = new MessageHeaderAccessor(message); accessor.setHeader("foo", "BAR"); MessageHeaders actual = accessor.getMessageHeaders(); assertEquals(3, actual.size()); assertNotEquals(message.getHeaders().getId(), actual.getId()); assertEquals("BAR", actual.get("foo")); assertEquals("baz", actual.get("bar")); }
@Test public void testProcessMessageWithStaticKey() { Expression expression = expressionParser.parseExpression("headers[headers.ID]"); ExpressionEvaluatingMessageProcessor<UUID> processor = new ExpressionEvaluatingMessageProcessor<>(expression); processor.setBeanFactory(mock(BeanFactory.class)); GenericMessage<String> message = new GenericMessage<>("foo"); assertEquals(message.getHeaders().getId(), processor.processMessage(message)); }
public void runAdapterWithConveter() throws Exception { @SuppressWarnings("resource") ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("mongodb-out-config.xml", MongoDbOutboundAdapterDemo.class); MessageChannel messageChannel = context.getBean("adapterWithConverter", MessageChannel.class); messageChannel.send(new GenericMessage<String>("John, Dow, Palo Alto, 3401 Hillview Ave, 94304, CA")); }
@Test public void testMessageHeadersNotConverted() { BeanFactoryTypeConverter typeConverter = new BeanFactoryTypeConverter(); typeConverter.setBeanFactory(new DefaultListableBeanFactory()); MessageHeaders headers = new GenericMessage<>("foo").getHeaders(); assertSame(headers, typeConverter.convertValue(headers, TypeDescriptor.valueOf(MessageHeaders.class), TypeDescriptor.valueOf(MessageHeaders.class))); }
private static GenericMessage<Document> createXmlMessageFromResource(String path) throws Exception { Resource orderRes = new ClassPathResource(path); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document orderDoc = builder.parse(orderRes.getInputStream()); return new GenericMessage<Document>(orderDoc); }
public void runDefaultAdapter() throws Exception { @SuppressWarnings("resource") ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("mongodb-out-config.xml", MongoDbOutboundAdapterDemo.class); MessageChannel messageChannel = context.getBean("deafultAdapter", MessageChannel.class); messageChannel.send(new GenericMessage<Person>(this.createPersonA())); messageChannel.send(new GenericMessage<Person>(this.createPersonB())); messageChannel.send(new GenericMessage<Person>(this.createPersonC())); }
public Message<String> receive() { return new GenericMessage<>(payload); } }
@Override public void handleMessage(Message<?> message) throws MessagingException { MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel(); replyChannel.send(new GenericMessage<>("response")); } });
@SuppressWarnings("unchecked") public Message<T> build() { if (this.originalMessage != null && !this.headerAccessor.isModified()) { return this.originalMessage; } MessageHeaders headersToUse = this.headerAccessor.toMessageHeaders(); if (this.payload instanceof Throwable) { return (Message<T>) new ErrorMessage((Throwable) this.payload, headersToUse); } else { return new GenericMessage<>(this.payload, headersToUse); } }
public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration/helloWorldDemo.xml", HelloWorldApp.class); MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class); PollableChannel outputChannel = context.getBean("outputChannel", PollableChannel.class); inputChannel.send(new GenericMessage<String>("World")); logger.info("==> HelloWorldDemo: " + outputChannel.receive(0).getPayload()); context.close(); }
@Test(expected = IllegalStateException.class) public void sendMissingDestination() { Message<?> message = new GenericMessage<Object>("payload"); this.template.send(message); }
private MessageHandler createLateReplier(final CountDownLatch latch, final AtomicReference<Throwable> failure) { MessageHandler handler = message -> { try { Thread.sleep(500); MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel(); replyChannel.send(new GenericMessage<>("response")); failure.set(new IllegalStateException("Expected exception")); } catch (InterruptedException e) { failure.set(e); } catch (MessageDeliveryException ex) { String expected = "Reply message received but the receiving thread has exited due to a timeout"; String actual = ex.getMessage(); if (!expected.equals(actual)) { failure.set(new IllegalStateException( "Unexpected error: '" + actual + "'")); } } finally { latch.countDown(); } }; return handler; }
@Test public void testToString() { ErrorMessage em = new ErrorMessage(new RuntimeException("foo")); String emString = em.toString(); assertThat(emString, not(containsString("original"))); em = new ErrorMessage(new RuntimeException("foo"), new GenericMessage<>("bar")); emString = em.toString(); assertThat(emString, containsString("original")); assertThat(emString, containsString(em.getOriginalMessage().toString())); }
@Test(expected = IllegalStateException.class) public void sendAndReceiveMissingDestination() { this.template.sendAndReceive(new GenericMessage<Object>("request")); }
@Test public void handleMessageWhenBrokerNotRunning() { this.handler.handleMessage(new GenericMessage<Object>("payload")); assertEquals(Collections.emptyList(), this.handler.messages); }
@Test public void runDemo() throws Exception{ ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/integration/SendInstantMessageSample-context.xml"); MessageChannel toUserChannel = context.getBean("toUserChannel", MessageChannel.class); Message<String> message = new GenericMessage<String>("Hello from Spring Integration XMPP"); toUserChannel.send(message); } }
@Test public void runDemo() throws Exception{ ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/integration/TwitterSendUpdates-context.xml"); MessageChannel twitterOutChannel = context.getBean("twitterOut", MessageChannel.class); Message<String> twitterUpdate = new GenericMessage<String>("Testing new Twitter samples for #springintegration"); twitterOutChannel.send(twitterUpdate); } }
public void write(List<? extends T> items) throws Exception { // Block until expecting <= throttle limit while (localState.getExpecting() > throttleLimit) { getNextResult(); } if (!items.isEmpty()) { ChunkRequest<T> request = localState.getRequest(items); if (logger.isDebugEnabled()) { logger.debug("Dispatching chunk: " + request); } messagingGateway.send(new GenericMessage<>(request)); localState.incrementExpected(); } }