Search in sources :

Example 56 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class AbstractMongoDbMessageGroupStoreTests method testWithMessageHistory.

@Test
@MongoDbAvailable
public void testWithMessageHistory() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store = this.getMessageGroupStore();
    store.getMessageGroup(1);
    Message<?> message = new GenericMessage<String>("Hello");
    DirectChannel fooChannel = new DirectChannel();
    fooChannel.setBeanName("fooChannel");
    DirectChannel barChannel = new DirectChannel();
    barChannel.setBeanName("barChannel");
    message = MessageHistory.write(message, fooChannel);
    message = MessageHistory.write(message, barChannel);
    store.addMessagesToGroup(1, message);
    MessageGroup group = store.getMessageGroup(1);
    assertNotNull(group);
    Collection<Message<?>> messages = group.getMessages();
    assertTrue(!messages.isEmpty());
    message = messages.iterator().next();
    MessageHistory messageHistory = MessageHistory.read(message);
    assertNotNull(messageHistory);
    assertEquals(2, messageHistory.size());
    Properties fooChannelHistory = messageHistory.get(0);
    assertEquals("fooChannel", fooChannelHistory.get("name"));
    assertEquals("channel", fooChannelHistory.get("type"));
}
Also used : MongoClient(com.mongodb.MongoClient) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHistory(org.springframework.integration.history.MessageHistory) AbstractBatchingMessageGroupStore(org.springframework.integration.store.AbstractBatchingMessageGroupStore) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) MessageGroup(org.springframework.integration.store.MessageGroup) Properties(java.util.Properties) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 57 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class RedisMessageStoreTests method testWithMessageHistory.

@Test
@RedisAvailable
public void testWithMessageHistory() throws Exception {
    RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
    RedisMessageStore store = new RedisMessageStore(jcf);
    Message<?> message = new GenericMessage<String>("Hello");
    DirectChannel fooChannel = new DirectChannel();
    fooChannel.setBeanName("fooChannel");
    DirectChannel barChannel = new DirectChannel();
    barChannel.setBeanName("barChannel");
    message = MessageHistory.write(message, fooChannel);
    message = MessageHistory.write(message, barChannel);
    store.addMessage(message);
    message = store.getMessage(message.getHeaders().getId());
    MessageHistory messageHistory = MessageHistory.read(message);
    assertNotNull(messageHistory);
    assertEquals(2, messageHistory.size());
    Properties fooChannelHistory = messageHistory.get(0);
    assertEquals("fooChannel", fooChannelHistory.get("name"));
    assertEquals("channel", fooChannelHistory.get("type"));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHistory(org.springframework.integration.history.MessageHistory) DirectChannel(org.springframework.integration.channel.DirectChannel) Properties(java.util.Properties) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 58 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class TestServerConfig method clientOutboundChannel.

@Bean
public AbstractSubscribableChannel clientOutboundChannel() {
    DirectChannel directChannel = new DirectChannel();
    directChannel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
            headers.setLeaveMutable(true);
            return MessageBuilder.createMessage(message.getPayload(), headers.getMessageHeaders());
        }
    });
    return directChannel;
}
Also used : ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Bean(org.springframework.context.annotation.Bean)

Example 59 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class ConnectionEventTests method testOutboundGatewayNoConnectionEvents.

@Test
public void testOutboundGatewayNoConnectionEvents() {
    TcpOutboundGateway gw = new TcpOutboundGateway();
    AbstractClientConnectionFactory ccf = new AbstractClientConnectionFactory("localhost", 0) {
    };
    final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<ApplicationEvent>();
    ccf.setApplicationEventPublisher(new ApplicationEventPublisher() {

        @Override
        public void publishEvent(Object event) {
        }

        @Override
        public void publishEvent(ApplicationEvent event) {
            theEvent.set(event);
        }
    });
    gw.setConnectionFactory(ccf);
    DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(message -> ((MessageChannel) message.getHeaders().getReplyChannel()).send(message));
    gw.start();
    Message<String> message = MessageBuilder.withPayload("foo").setHeader(IpHeaders.CONNECTION_ID, "bar").build();
    gw.onMessage(message);
    assertNotNull(theEvent.get());
    TcpConnectionFailedCorrelationEvent event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
    assertEquals("bar", event.getConnectionId());
    MessagingException messagingException = (MessagingException) event.getCause();
    assertSame(message, messagingException.getFailedMessage());
    assertEquals("Cannot correlate response - no pending reply for bar", messagingException.getMessage());
    message = new GenericMessage<String>("foo");
    gw.onMessage(message);
    assertNotNull(theEvent.get());
    event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
    assertNull(event.getConnectionId());
    messagingException = (MessagingException) event.getCause();
    assertSame(message, messagingException.getFailedMessage());
    assertEquals("Cannot correlate response - no connection id", messagingException.getMessage());
    gw.stop();
    ccf.stop();
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) ApplicationEvent(org.springframework.context.ApplicationEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TcpOutboundGateway(org.springframework.integration.ip.tcp.TcpOutboundGateway) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Test(org.junit.Test)

Example 60 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class TcpInboundGatewayTests method testErrorFlow.

@Test
public void testErrorFlow() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0);
    scf.setSingleUse(true);
    TcpInboundGateway gateway = new TcpInboundGateway();
    gateway.setConnectionFactory(scf);
    SubscribableChannel errorChannel = new DirectChannel();
    final String errorMessage = "An error occurred";
    errorChannel.subscribe(message -> {
        MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
        replyChannel.send(new GenericMessage<String>(errorMessage));
    });
    gateway.setErrorChannel(errorChannel);
    scf.start();
    TestingUtilities.waitListening(scf, 20000L);
    int port = scf.getPort();
    final SubscribableChannel channel = new DirectChannel();
    gateway.setRequestChannel(channel);
    gateway.setBeanFactory(mock(BeanFactory.class));
    ServiceActivatingHandler handler = new ServiceActivatingHandler(new FailingService());
    channel.subscribe(handler);
    Socket socket1 = SocketFactory.getDefault().createSocket("localhost", port);
    socket1.getOutputStream().write("Test1\r\n".getBytes());
    Socket socket2 = SocketFactory.getDefault().createSocket("localhost", port);
    socket2.getOutputStream().write("Test2\r\n".getBytes());
    byte[] bytes = new byte[errorMessage.length() + 2];
    readFully(socket1.getInputStream(), bytes);
    assertEquals(errorMessage + "\r\n", new String(bytes));
    readFully(socket2.getInputStream(), bytes);
    assertEquals(errorMessage + "\r\n", new String(bytes));
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) MessageChannel(org.springframework.messaging.MessageChannel) TcpNetServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Aggregations

DirectChannel (org.springframework.integration.channel.DirectChannel)215 Test (org.junit.Test)182 MessageChannel (org.springframework.messaging.MessageChannel)71 Message (org.springframework.messaging.Message)68 QueueChannel (org.springframework.integration.channel.QueueChannel)63 BeanFactory (org.springframework.beans.factory.BeanFactory)45 GenericMessage (org.springframework.messaging.support.GenericMessage)38 MessageHandler (org.springframework.messaging.MessageHandler)32 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)28 CountDownLatch (java.util.concurrent.CountDownLatch)27 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)26 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)25 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)23 HashMap (java.util.HashMap)22 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)22 MessagingException (org.springframework.messaging.MessagingException)18 Properties (java.util.Properties)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 SubscribableChannel (org.springframework.messaging.SubscribableChannel)15 Expression (org.springframework.expression.Expression)14