Example usage for org.springframework.messaging Message getPayload

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

Introduction

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

Prototype

T getPayload();

Source Link

Document

Return the message payload.

Usage

From source file:org.springframework.integration.config.SourcePollingChannelAdapterFactoryBeanTests.java

@Test
public void testTransactionalAdviceChain() throws Throwable {
    SourcePollingChannelAdapterFactoryBean factoryBean = new SourcePollingChannelAdapterFactoryBean();
    QueueChannel outputChannel = new QueueChannel();
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    factoryBean.setBeanFactory(context.getBeanFactory());
    factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
    factoryBean.setOutputChannel(outputChannel);
    factoryBean.setSource(() -> new GenericMessage<>("test"));
    PollerMetadata pollerMetadata = new PollerMetadata();
    List<Advice> adviceChain = new ArrayList<Advice>();
    final AtomicBoolean adviceApplied = new AtomicBoolean(false);
    adviceChain.add((MethodInterceptor) invocation -> {
        adviceApplied.set(true);//  ww  w.  j a v a 2s. c om
        return invocation.proceed();
    });
    pollerMetadata.setTrigger(new PeriodicTrigger(5000));
    pollerMetadata.setMaxMessagesPerPoll(1);
    final AtomicInteger count = new AtomicInteger();
    final MethodInterceptor txAdvice = mock(MethodInterceptor.class);
    adviceChain.add((MethodInterceptor) invocation -> {
        count.incrementAndGet();
        return invocation.proceed();
    });
    when(txAdvice.invoke(any(MethodInvocation.class))).thenAnswer(invocation -> {
        count.incrementAndGet();
        return ((MethodInvocation) invocation.getArgument(0)).proceed();
    });

    pollerMetadata.setAdviceChain(adviceChain);
    factoryBean.setPollerMetadata(pollerMetadata);
    factoryBean.setAutoStartup(true);
    factoryBean.afterPropertiesSet();
    context.registerEndpoint("testPollingEndpoint", factoryBean.getObject());
    context.refresh();
    Message<?> message = outputChannel.receive(5000);
    assertEquals("test", message.getPayload());
    assertEquals(1, count.get());
    assertTrue("adviceChain was not applied", adviceApplied.get());
}

From source file:org.springframework.integration.config.SourcePollingChannelAdapterFactoryBeanTests.java

@Test
public void testStartSourceBeforeRunPollingTask() {
    TaskScheduler taskScheduler = mock(TaskScheduler.class);

    willAnswer(invocation -> {/*from w ww. j  a v a2 s  . c om*/
        Runnable task = invocation.getArgument(0);
        task.run();
        return null;
    }).given(taskScheduler).schedule(any(Runnable.class), any(Trigger.class));

    SourcePollingChannelAdapter pollingChannelAdapter = new SourcePollingChannelAdapter();
    pollingChannelAdapter.setTaskScheduler(taskScheduler);
    pollingChannelAdapter.setSource(new LifecycleMessageSource());
    pollingChannelAdapter.setMaxMessagesPerPoll(1);
    QueueChannel outputChannel = new QueueChannel();
    pollingChannelAdapter.setOutputChannel(outputChannel);
    pollingChannelAdapter.setBeanFactory(mock(BeanFactory.class));
    pollingChannelAdapter.afterPropertiesSet();
    pollingChannelAdapter.start();

    Message<?> receive = outputChannel.receive(10_000);
    assertNotNull(receive);
    assertEquals(true, receive.getPayload());
    pollingChannelAdapter.stop();
}

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

@Test
public void testOneWay() {
    TestService service = (TestService) context.getBean("oneWay");
    service.oneWay("foo");
    PollableChannel channel = (PollableChannel) context.getBean("requestChannel");
    Message<?> result = channel.receive(1000);
    assertEquals("foo", result.getPayload());
}

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

@Test
public void testOneWayOverride() {
    TestService service = (TestService) context.getBean("methodOverride");
    service.oneWay("foo");
    PollableChannel channel = (PollableChannel) context.getBean("otherRequestChannel");
    Message<?> result = channel.receive(1000);
    assertEquals("fiz", result.getPayload());
    assertEquals("bar", result.getHeaders().get("foo"));
    assertEquals("qux", result.getHeaders().get("baz"));
    GatewayProxyFactoryBean fb = context.getBean("&methodOverride", GatewayProxyFactoryBean.class);
    Map<?, ?> methods = TestUtils.getPropertyValue(fb, "methodMetadataMap", Map.class);
    GatewayMethodMetadata meta = (GatewayMethodMetadata) methods.get("oneWay");
    assertNotNull(meta);/*  w  w  w.  j  a  va2 s  .c  o  m*/
    assertEquals("456", meta.getRequestTimeout());
    assertEquals("123", meta.getReplyTimeout());
    assertEquals("foo", meta.getReplyChannelName());
}

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

@Test
public void testAsyncDisabledGateway() throws Exception {
    PollableChannel requestChannel = (PollableChannel) context.getBean("requestChannel");
    MessageChannel replyChannel = (MessageChannel) context.getBean("replyChannel");
    this.startResponder(requestChannel, replyChannel);
    TestService service = context.getBean("asyncOff", TestService.class);
    Future<Message<?>> result = service.async("futureSync");
    Message<?> reply = result.get(1, TimeUnit.SECONDS);
    assertEquals("futureSync", reply.getPayload());
    Object serviceBean = context.getBean("&asyncOff");
    assertNull(TestUtils.getPropertyValue(serviceBean, "asyncExecutor"));
}

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

@Test
public void testMonoGateway() throws Exception {
    PollableChannel requestChannel = context.getBean("requestChannel", PollableChannel.class);
    MessageChannel replyChannel = context.getBean("replyChannel", MessageChannel.class);
    this.startResponder(requestChannel, replyChannel);
    TestService service = context.getBean("promise", TestService.class);
    Mono<Message<?>> result = service.promise("foo");
    Message<?> reply = result.block(Duration.ofSeconds(1));
    assertEquals("foo", reply.getPayload());
    assertNotNull(TestUtils.getPropertyValue(context.getBean("&promise"), "asyncExecutor"));
}

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

@Test
public void testAsyncCompletableMessage() throws Exception {
    QueueChannel requestChannel = (QueueChannel) context.getBean("requestChannel");
    final AtomicReference<Thread> thread = new AtomicReference<>();
    requestChannel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override// ww w .j a v  a 2s .  c  om
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            thread.set(Thread.currentThread());
            return super.preSend(message, channel);
        }

    });
    MessageChannel replyChannel = (MessageChannel) context.getBean("replyChannel");
    this.startResponder(requestChannel, replyChannel);
    TestService service = context.getBean("asyncCompletable", TestService.class);
    CompletableFuture<Message<?>> result = service.completableReturnsMessage("foo");
    Message<?> reply = result.get(1, TimeUnit.SECONDS);
    assertEquals("foo", reply.getPayload());
    assertThat(thread.get().getName(), startsWith("testExec-"));
    assertNotNull(TestUtils.getPropertyValue(context.getBean("&asyncCompletable"), "asyncExecutor"));
}

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

@Test
public void testAsyncCompletableNoAsyncMessage() throws Exception {
    QueueChannel requestChannel = (QueueChannel) context.getBean("requestChannel");
    final AtomicReference<Thread> thread = new AtomicReference<>();
    requestChannel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override//w  w  w. ja  va  2 s . co m
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            thread.set(Thread.currentThread());
            return super.preSend(message, channel);
        }

    });
    MessageChannel replyChannel = (MessageChannel) context.getBean("replyChannel");
    this.startResponder(requestChannel, replyChannel);
    TestService service = context.getBean("completableNoAsync", TestService.class);
    CompletableFuture<Message<?>> result = service.completableReturnsMessage("flowCompletableM");
    Message<?> reply = result.get(1, TimeUnit.SECONDS);
    assertEquals("flowCompletableM", reply.getPayload());
    assertEquals(Thread.currentThread(), thread.get());
    assertNull(TestUtils.getPropertyValue(context.getBean("&completableNoAsync"), "asyncExecutor"));
}

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

@Test
public void testCustomCompletableNoAsyncMessage() throws Exception {
    QueueChannel requestChannel = (QueueChannel) context.getBean("requestChannel");
    final AtomicReference<Thread> thread = new AtomicReference<>();
    requestChannel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override// w  w  w  .j ava 2s .  c o m
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            thread.set(Thread.currentThread());
            return super.preSend(message, channel);
        }

    });
    MessageChannel replyChannel = (MessageChannel) context.getBean("replyChannel");
    this.startResponder(requestChannel, replyChannel);
    TestService service = context.getBean("completableNoAsync", TestService.class);
    MyCompletableMessageFuture result = service.customCompletableReturnsMessage("flowCustomCompletableM");
    Message<?> reply = result.get(1, TimeUnit.SECONDS);
    assertEquals("flowCustomCompletableM", reply.getPayload());
    assertEquals(Thread.currentThread(), thread.get());
    assertNull(TestUtils.getPropertyValue(context.getBean("&completableNoAsync"), "asyncExecutor"));
}