Example usage for org.springframework.messaging Message getHeaders

List of usage examples for org.springframework.messaging Message getHeaders

Introduction

In this page you can find the example usage for org.springframework.messaging Message getHeaders.

Prototype

MessageHeaders getHeaders();

Source Link

Document

Return message headers for the message (never null but may be empty).

Usage

From source file:org.springframework.integration.config.xml.GatewayParserTests.java

@Test
public void testAsyncGateway() throws Exception {
    PollableChannel requestChannel = (PollableChannel) context.getBean("requestChannel");
    MessageChannel replyChannel = (MessageChannel) context.getBean("replyChannel");
    this.startResponder(requestChannel, replyChannel);
    TestService service = context.getBean("async", TestService.class);
    Future<Message<?>> result = service.async("foo");
    Message<?> reply = result.get(1, TimeUnit.SECONDS);
    assertEquals("foo", reply.getPayload());
    assertEquals("testExecutor", reply.getHeaders().get("executor"));
    assertNotNull(TestUtils.getPropertyValue(context.getBean("&async"), "asyncExecutor"));
}

From source file:org.springframework.integration.config.xml.GatewayParserTests.java

private void startResponder(final PollableChannel requestChannel, final MessageChannel replyChannel) {
    Executors.newSingleThreadExecutor().execute(() -> {
        Message<?> request = requestChannel.receive(60000);
        assertNotNull("Request not received", request);
        Message<?> reply = MessageBuilder.fromMessage(request).setCorrelationId(request.getHeaders().getId())
                .build();/*from  ww w.  j  av a 2 s. c  o  m*/
        Object payload = null;
        if (request.getPayload().equals("futureSync")) {
            payload = new AsyncResult<Message<?>>(reply);
        } else if (request.getPayload().equals("flowCompletable")) {
            payload = CompletableFuture.<String>completedFuture("SYNC_COMPLETABLE");
        } else if (request.getPayload().equals("flowCustomCompletable")) {
            MyCompletableFuture myCompletableFuture1 = new MyCompletableFuture();
            myCompletableFuture1.complete("SYNC_CUSTOM_COMPLETABLE");
            payload = myCompletableFuture1;
        } else if (request.getPayload().equals("flowCompletableM")) {
            payload = CompletableFuture.<Message<?>>completedFuture(reply);
        } else if (request.getPayload().equals("flowCustomCompletableM")) {
            MyCompletableMessageFuture myCompletableFuture2 = new MyCompletableMessageFuture();
            myCompletableFuture2.complete(reply);
            payload = myCompletableFuture2;
        }
        if (payload != null) {
            reply = MessageBuilder.withPayload(payload).copyHeaders(reply.getHeaders()).build();
        }
        replyChannel.send(reply);
    });
}

From source file:org.springframework.integration.configuration.EnableIntegrationTests.java

@Test
public void testAnnotatedServiceActivator() throws Exception {
    this.serviceActivatorEndpoint.setReceiveTimeout(10000);
    this.serviceActivatorEndpoint.start();
    assertTrue(this.inputReceiveLatch.await(10, TimeUnit.SECONDS));

    assertEquals(10L, TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "maxMessagesPerPoll"));

    Trigger trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(100L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));

    assertTrue(this.annotationTestService.isRunning());
    Log logger = spy(TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "logger", Log.class));
    when(logger.isDebugEnabled()).thenReturn(true);
    final CountDownLatch pollerInterruptedLatch = new CountDownLatch(1);
    doAnswer(invocation -> {//www. j av  a 2s.c  o m
        pollerInterruptedLatch.countDown();
        invocation.callRealMethod();
        return null;
    }).when(logger).debug("Received no Message during the poll, returning 'false'");
    new DirectFieldAccessor(this.serviceActivatorEndpoint).setPropertyValue("logger", logger);

    this.serviceActivatorEndpoint.stop();
    assertFalse(this.annotationTestService.isRunning());

    // wait until the service activator's poller is interrupted.
    assertTrue(pollerInterruptedLatch.await(10, TimeUnit.SECONDS));
    this.serviceActivatorEndpoint.start();
    assertTrue(this.annotationTestService.isRunning());

    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint1, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(100L, TestUtils.getPropertyValue(trigger, "period"));
    assertTrue(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));

    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint2, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(CronTrigger.class));
    assertEquals("0 5 7 * * *", TestUtils.getPropertyValue(trigger, "sequenceGenerator.expression"));

    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint3, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(11L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));

    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint4, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(1000L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));
    assertSame(this.myTrigger, trigger);

    trigger = TestUtils.getPropertyValue(this.transformer, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(10L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));

    this.input.send(MessageBuilder.withPayload("Foo").build());

    Message<?> interceptedMessage = this.wireTapChannel.receive(10000);
    assertNotNull(interceptedMessage);
    assertEquals("Foo", interceptedMessage.getPayload());

    Message<?> receive = this.output.receive(10000);
    assertNotNull(receive);
    assertEquals("FOO", receive.getPayload());

    receive = this.wireTapFromOutput.receive(10000);
    assertNotNull(receive);
    assertEquals("FOO", receive.getPayload());

    MessageHistory messageHistory = receive.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class);
    assertNotNull(messageHistory);
    String messageHistoryString = messageHistory.toString();
    assertThat(messageHistoryString, Matchers.containsString("input"));
    assertThat(messageHistoryString,
            Matchers.containsString("annotationTestService.handle.serviceActivator.handler"));
    assertThat(messageHistoryString, Matchers.not(Matchers.containsString("output")));

    receive = this.publishedChannel.receive(10000);

    assertNotNull(receive);
    assertEquals("foo", receive.getPayload());

    messageHistory = receive.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class);
    assertNotNull(messageHistory);
    messageHistoryString = messageHistory.toString();
    assertThat(messageHistoryString, Matchers.not(Matchers.containsString("input")));
    assertThat(messageHistoryString, Matchers.not(Matchers.containsString("output")));
    assertThat(messageHistoryString, Matchers.containsString("publishedChannel"));

    assertNull(this.wireTapChannel.receive(0));
    assertThat(this.testChannelInterceptor.getInvoked(), Matchers.greaterThan(0));
    assertThat(this.fbInterceptorCounter.get(), Matchers.greaterThan(0));

    assertTrue(this.context.containsBean("annotationTestService.count.inboundChannelAdapter.source"));
    Object messageSource = this.context.getBean("annotationTestService.count.inboundChannelAdapter.source");
    assertThat(messageSource, Matchers.instanceOf(MethodInvokingMessageSource.class));

    assertNull(this.counterChannel.receive(10));

    SmartLifecycle countSA = this.context.getBean("annotationTestService.count.inboundChannelAdapter",
            SmartLifecycle.class);
    assertFalse(countSA.isAutoStartup());
    assertEquals(23, countSA.getPhase());
    countSA.start();

    for (int i = 0; i < 10; i++) {
        Message<?> message = this.counterChannel.receive(1000);
        assertNotNull(message);
        assertEquals(i + 1, message.getPayload());
    }

    Message<?> message = this.fooChannel.receive(1000);
    assertNotNull(message);
    assertEquals("foo", message.getPayload());
    message = this.fooChannel.receive(1000);
    assertNotNull(message);
    assertEquals("foo", message.getPayload());
    assertNull(this.fooChannel.receive(10));

    message = this.messageChannel.receive(1000);
    assertNotNull(message);
    assertEquals("bar", message.getPayload());
    assertTrue(message.getHeaders().containsKey("foo"));
    assertEquals("FOO", message.getHeaders().get("foo"));

    MessagingTemplate messagingTemplate = new MessagingTemplate(this.controlBusChannel);
    assertFalse(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class));
    this.controlBusChannel.send(new GenericMessage<String>("@lifecycle.start()"));
    assertTrue(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class));
    this.controlBusChannel.send(new GenericMessage<String>("@lifecycle.stop()"));
    assertFalse(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class));
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testContentEnricher() {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message = MessageBuilder.withPayload(new TestPojo("Bar"))
            .setHeader(MessageHeaders.REPLY_CHANNEL, replyChannel).build();
    this.enricherInput.send(message);
    Message<?> receive = replyChannel.receive(5000);
    assertNotNull(receive);//  w w  w.j a  v  a  2s .c o  m
    assertEquals("Bar Bar", receive.getHeaders().get("foo"));
    Object payload = receive.getPayload();
    assertThat(payload, instanceOf(TestPojo.class));
    TestPojo result = (TestPojo) payload;
    assertEquals("Bar Bar", result.getName());
    assertNotNull(result.getDate());
    assertThat(new Date(), Matchers.greaterThanOrEqualTo(result.getDate()));
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testContentEnricher2() {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message = MessageBuilder.withPayload(new TestPojo("Bar"))
            .setHeader(MessageHeaders.REPLY_CHANNEL, replyChannel).build();
    this.enricherInput2.send(message);
    Message<?> receive = replyChannel.receive(5000);
    assertNotNull(receive);//from ww  w.java2  s.  c  o  m
    assertNull(receive.getHeaders().get("foo"));
    Object payload = receive.getPayload();
    assertThat(payload, instanceOf(TestPojo.class));
    TestPojo result = (TestPojo) payload;
    assertEquals("Bar Bar", result.getName());
    assertNotNull(result.getDate());
    assertThat(new Date(), Matchers.greaterThanOrEqualTo(result.getDate()));
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testContentEnricher3() {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message = MessageBuilder.withPayload(new TestPojo("Bar"))
            .setHeader(MessageHeaders.REPLY_CHANNEL, replyChannel).build();
    this.enricherInput3.send(message);
    Message<?> receive = replyChannel.receive(5000);
    assertNotNull(receive);/* w  ww.  ja  va 2 s .com*/
    assertEquals("Bar Bar", receive.getHeaders().get("foo"));
    Object payload = receive.getPayload();
    assertThat(payload, instanceOf(TestPojo.class));
    TestPojo result = (TestPojo) payload;
    assertEquals("Bar", result.getName());
    assertNull(result.getDate());
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testSplitterResequencer() {
    QueueChannel replyChannel = new QueueChannel();

    this.splitInput
            .send(MessageBuilder.withPayload("").setReplyChannel(replyChannel).setHeader("foo", "bar").build());

    for (int i = 0; i < 12; i++) {
        Message<?> receive = replyChannel.receive(2000);
        assertNotNull(receive);/*w ww  .  j  av  a2 s .com*/
        assertFalse(receive.getHeaders().containsKey("foo"));
        assertTrue(receive.getHeaders().containsKey("FOO"));
        assertEquals("BAR", receive.getHeaders().get("FOO"));
        assertEquals(i + 1, receive.getPayload());
    }
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testHeaderEnricher() {
    QueueChannel replyChannel = new QueueChannel();

    Message<String> message = MessageBuilder
            .withPayload("<root><elementOne>1</elementOne><elementTwo>2</elementTwo></root>")
            .setReplyChannel(replyChannel).build();

    try {/*  w w w .  j a  va 2 s.c  o m*/
        this.xpathHeaderEnricherInput.send(message);
        fail("Expected MessageDispatchingException");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getCause(), instanceOf(MessageDispatchingException.class));
        assertThat(e.getMessage(), containsString("Dispatcher has no subscribers"));
    }

    this.controlBus.send("@xpathHeaderEnricher.start()");
    this.xpathHeaderEnricherInput.send(message);

    Message<?> result = replyChannel.receive(2000);
    assertNotNull(result);
    MessageHeaders headers = result.getHeaders();
    assertEquals("1", headers.get("one"));
    assertEquals("2", headers.get("two"));
    assertThat(headers.getReplyChannel(), instanceOf(String.class));
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testClaimCheck() {
    QueueChannel replyChannel = new QueueChannel();

    Message<String> message = MutableMessageBuilder.withPayload("foo").setReplyChannel(replyChannel).build();

    this.claimCheckInput.send(message);

    Message<?> receive = replyChannel.receive(2000);
    assertNotNull(receive);//from   w  w w  .  java  2  s.  co  m
    assertSame(message, receive);

    assertEquals(1, this.messageStore.getMessageCount());
    assertSame(message, this.messageStore.getMessage(message.getHeaders().getId()));
}

From source file:org.springframework.integration.dsl.test.xml.XmlTests.java

@Test
public void testHeaderEnricher() {
    QueueChannel replyChannel = new QueueChannel();

    Message<String> message = MessageBuilder
            .withPayload("<root><elementOne>1</elementOne><elementTwo>2</elementTwo></root>")
            .setReplyChannel(replyChannel).build();

    try {//w w w.j a va2s . co m
        this.xpathHeaderEnricherInput.send(message);
        fail("Expected MessageDispatchingException");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getCause(), instanceOf(MessageDispatchingException.class));
        assertThat(e.getMessage(), containsString("Dispatcher has no subscribers"));
    }

    this.controlBusFlowInput.send(new GenericMessage<>("@xpathHeaderEnricher.start()"));
    this.xpathHeaderEnricherInput.send(message);

    Message<?> result = replyChannel.receive(2000);
    assertNotNull(result);
    MessageHeaders headers = result.getHeaders();
    assertEquals("1", headers.get("one"));
    assertEquals("2", headers.get("two"));
    assertThat(headers.getReplyChannel(), instanceOf(String.class));
    assertFalse(headers.containsKey("foo"));
}