Search in sources :

Example 61 with Message

use of org.springframework.messaging.Message in project spring-integration by spring-projects.

the class BroadcastingDispatcherTests method applySequenceEnabled.

@Test
public void applySequenceEnabled() {
    BroadcastingDispatcher dispatcher = new BroadcastingDispatcher();
    dispatcher.setApplySequence(true);
    final List<Message<?>> messages = Collections.synchronizedList(new ArrayList<Message<?>>());
    MessageHandler target1 = new MessageStoringTestEndpoint(messages);
    MessageHandler target2 = new MessageStoringTestEndpoint(messages);
    MessageHandler target3 = new MessageStoringTestEndpoint(messages);
    dispatcher.addHandler(target1);
    dispatcher.addHandler(target2);
    dispatcher.addHandler(target3);
    Message<?> inputMessage = new GenericMessage<String>("test");
    Object originalId = inputMessage.getHeaders().getId();
    dispatcher.dispatch(inputMessage);
    assertEquals(3, messages.size());
    assertEquals(1, new IntegrationMessageHeaderAccessor(messages.get(0)).getSequenceNumber());
    assertEquals(3, new IntegrationMessageHeaderAccessor(messages.get(0)).getSequenceSize());
    assertEquals(originalId, new IntegrationMessageHeaderAccessor(messages.get(0)).getCorrelationId());
    assertEquals(2, new IntegrationMessageHeaderAccessor(messages.get(1)).getSequenceNumber());
    assertEquals(3, new IntegrationMessageHeaderAccessor(messages.get(1)).getSequenceSize());
    assertEquals(originalId, new IntegrationMessageHeaderAccessor(messages.get(1)).getCorrelationId());
    assertEquals(3, new IntegrationMessageHeaderAccessor(messages.get(2)).getSequenceNumber());
    assertEquals(3, new IntegrationMessageHeaderAccessor(messages.get(2)).getSequenceSize());
    assertEquals(originalId, new IntegrationMessageHeaderAccessor(messages.get(2)).getCorrelationId());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) Test(org.junit.Test)

Example 62 with Message

use of org.springframework.messaging.Message in project spring-integration by spring-projects.

the class UnicastingDispatcherTests method withInboundGatewayAsyncRequestChannelAndExplicitErrorChannel.

@SuppressWarnings("unchecked")
@Test
public void withInboundGatewayAsyncRequestChannelAndExplicitErrorChannel() throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("unicasting-with-async.xml", this.getClass());
    SubscribableChannel errorChannel = context.getBean("errorChannel", SubscribableChannel.class);
    MessageHandler errorHandler = message -> {
        MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
        assertTrue(message.getPayload() instanceof MessageDeliveryException);
        replyChannel.send(new GenericMessage<String>("reply"));
    };
    errorChannel.subscribe(errorHandler);
    RequestReplyExchanger exchanger = context.getBean(RequestReplyExchanger.class);
    Message<String> reply = (Message<String>) exchanger.exchange(new GenericMessage<String>("Hello"));
    assertEquals("reply", reply.getPayload());
    context.close();
}
Also used : MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) MessageHandler(org.springframework.messaging.MessageHandler) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Assert.assertTrue(org.junit.Assert.assertTrue) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Test(org.junit.Test) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageChannel(org.springframework.messaging.MessageChannel) Assert.assertEquals(org.junit.Assert.assertEquals) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test)

Example 63 with Message

use of org.springframework.messaging.Message in project spring-integration by spring-projects.

the class AsyncGatewayTests method listenableFutureWithMessageReturned.

@Test
public void listenableFutureWithMessageReturned() throws Exception {
    QueueChannel requestChannel = new QueueChannel();
    addThreadEnricher(requestChannel);
    startResponder(requestChannel);
    GatewayProxyFactoryBean proxyFactory = new GatewayProxyFactoryBean();
    proxyFactory.setDefaultRequestChannel(requestChannel);
    proxyFactory.setServiceInterface(TestEchoService.class);
    proxyFactory.setBeanName("testGateway");
    proxyFactory.setBeanFactory(mock(BeanFactory.class));
    proxyFactory.afterPropertiesSet();
    TestEchoService service = (TestEchoService) proxyFactory.getObject();
    ListenableFuture<Message<?>> f = service.returnMessageListenable("foo");
    long start = System.currentTimeMillis();
    final AtomicReference<Message<?>> result = new AtomicReference<Message<?>>();
    final CountDownLatch latch = new CountDownLatch(1);
    f.addCallback(new ListenableFutureCallback<Message<?>>() {

        @Override
        public void onSuccess(Message<?> msg) {
            result.set(msg);
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable t) {
        }
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    long elapsed = System.currentTimeMillis() - start;
    assertTrue(elapsed >= 200);
    assertEquals("foobar", result.get().getPayload());
    Object thread = result.get().getHeaders().get("thread");
    assertNotEquals(Thread.currentThread(), thread);
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 64 with Message

use of org.springframework.messaging.Message in project spring-integration by spring-projects.

the class ObjectToMapTransformerParserTests method testObjectToSpelMapTransformer.

@SuppressWarnings("unchecked")
@Test
public void testObjectToSpelMapTransformer() {
    Employee employee = this.buildEmployee();
    StandardEvaluationContext context = new StandardEvaluationContext(employee);
    context.addPropertyAccessor(new MapAccessor());
    ExpressionParser parser = new SpelExpressionParser();
    Message<Employee> message = MessageBuilder.withPayload(employee).build();
    directInput.send(message);
    Message<Map<String, Object>> outputMessage = (Message<Map<String, Object>>) output.receive();
    Map<String, Object> transformedMap = outputMessage.getPayload();
    assertNotNull(outputMessage.getPayload());
    for (String key : transformedMap.keySet()) {
        Expression expression = parser.parseExpression(key);
        Object valueFromTheMap = transformedMap.get(key);
        Object valueFromExpression = expression.getValue(context);
        assertEquals(valueFromTheMap, valueFromExpression);
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Message(org.springframework.messaging.Message) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) MapAccessor(org.springframework.context.expression.MapAccessor) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 65 with Message

use of org.springframework.messaging.Message in project spring-integration by spring-projects.

the class ServiceActivatorMethodResolutionTests method testRequestReplyExchangerWithGenericMessageMethod.

@Test
public /*
	 * No handler fallback methods; don't force RRE
	 */
void testRequestReplyExchangerWithGenericMessageMethod() {
    RequestReplyExchanger testBean = new RequestReplyExchanger() {

        @Override
        public Message<?> exchange(Message<?> request) {
            return request;
        }

        @SuppressWarnings("unused")
        public String foo(Message<String> request) {
            return request.getPayload().toUpperCase();
        }
    };
    ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(testBean);
    PollableChannel outputChannel = new QueueChannel();
    serviceActivator.setOutputChannel(outputChannel);
    Message<?> test = new GenericMessage<Object>(new Date());
    serviceActivator.handleMessage(test);
    assertEquals(test, outputChannel.receive(10));
    test = new GenericMessage<Object>("foo");
    serviceActivator.handleMessage(test);
    assertEquals("FOO", outputChannel.receive(10).getPayload());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) PollableChannel(org.springframework.messaging.PollableChannel) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Message (org.springframework.messaging.Message)597 Test (org.junit.Test)423 GenericMessage (org.springframework.messaging.support.GenericMessage)272 QueueChannel (org.springframework.integration.channel.QueueChannel)189 MessageChannel (org.springframework.messaging.MessageChannel)128 ArrayList (java.util.ArrayList)113 BeanFactory (org.springframework.beans.factory.BeanFactory)85 CountDownLatch (java.util.concurrent.CountDownLatch)80 DirectChannel (org.springframework.integration.channel.DirectChannel)69 AtomicReference (java.util.concurrent.atomic.AtomicReference)68 Test (org.junit.jupiter.api.Test)65 MessageHandler (org.springframework.messaging.MessageHandler)58 List (java.util.List)56 PollableChannel (org.springframework.messaging.PollableChannel)56 ErrorMessage (org.springframework.messaging.support.ErrorMessage)52 Matchers.containsString (org.hamcrest.Matchers.containsString)50 MessagingException (org.springframework.messaging.MessagingException)50 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)43 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)41 Assert.assertEquals (org.junit.Assert.assertEquals)40