Search in sources :

Example 61 with DirectChannel

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

the class TcpReceivingChannelAdapterTests method testException.

@Test
public void testException() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0);
    noopPublisher(scf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    scf.setSerializer(serializer);
    scf.setDeserializer(serializer);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    scf.start();
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    SubscribableChannel channel = new DirectChannel();
    adapter.setOutputChannel(channel);
    ServiceActivatingHandler handler = new ServiceActivatingHandler(new FailingService());
    channel.subscribe(handler);
    QueueChannel errorChannel = new QueueChannel();
    adapter.setErrorChannel(errorChannel);
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write("Test1\r\n".getBytes());
    socket.getOutputStream().write("Test2\r\n".getBytes());
    Message<?> message = errorChannel.receive(10000);
    assertNotNull(message);
    assertEquals("Failed", ((Exception) message.getPayload()).getCause().getMessage());
    message = errorChannel.receive(10000);
    assertNotNull(message);
    assertEquals("Failed", ((Exception) message.getPayload()).getCause().getMessage());
    scf.stop();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) IOException(java.io.IOException) TcpNetServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 62 with DirectChannel

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

the class FailoverClientConnectionFactoryTests method testFailoverCachedWithGateway.

@SuppressWarnings("unchecked")
@Test
public void testFailoverCachedWithGateway() throws Exception {
    final TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory(0);
    server.setBeanName("server");
    server.afterPropertiesSet();
    DirectChannel inChannel = new DirectChannel();
    inChannel.setBeanName("inChannel");
    TcpInboundGateway inbound = new TcpInboundGateway();
    inbound.setConnectionFactory(server);
    inbound.setRequestChannel(inChannel);
    inbound.afterPropertiesSet();
    inChannel.subscribe(new BridgeHandler());
    inbound.start();
    TestingUtilities.waitListening(server, 10000L);
    int port = server.getPort();
    AbstractClientConnectionFactory client = new TcpNetClientConnectionFactory("localhost", port);
    client.setBeanName("client");
    // Cache
    CachingClientConnectionFactory cachingClient = new CachingClientConnectionFactory(client, 2);
    cachingClient.setBeanName("cache");
    cachingClient.afterPropertiesSet();
    // Failover
    List<AbstractClientConnectionFactory> clientFactories = new ArrayList<AbstractClientConnectionFactory>();
    clientFactories.add(cachingClient);
    FailoverClientConnectionFactory failoverClient = new FailoverClientConnectionFactory(clientFactories);
    failoverClient.setSingleUse(true);
    failoverClient.afterPropertiesSet();
    TcpOutboundGateway outbound = new TcpOutboundGateway();
    outbound.setConnectionFactory(failoverClient);
    QueueChannel replyChannel = new QueueChannel();
    replyChannel.setBeanName("replyChannel");
    outbound.setReplyChannel(replyChannel);
    outbound.setBeanFactory(mock(BeanFactory.class));
    outbound.afterPropertiesSet();
    outbound.start();
    outbound.handleMessage(new GenericMessage<String>("foo"));
    Message<byte[]> result = (Message<byte[]>) replyChannel.receive(10000);
    assertNotNull(result);
    assertEquals("foo", new String(result.getPayload()));
    // INT-4024 - second reply had bad connection id
    outbound.handleMessage(new GenericMessage<String>("foo"));
    result = (Message<byte[]>) replyChannel.receive(10000);
    assertNotNull(result);
    assertEquals("foo", new String(result.getPayload()));
    inbound.stop();
    outbound.stop();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) BridgeHandler(org.springframework.integration.handler.BridgeHandler) ArrayList(java.util.ArrayList) TcpOutboundGateway(org.springframework.integration.ip.tcp.TcpOutboundGateway) TcpInboundGateway(org.springframework.integration.ip.tcp.TcpInboundGateway) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 63 with DirectChannel

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

the class RedisMessageGroupStoreTests method testWithMessageHistory.

@Test
@RedisAvailable
public void testWithMessageHistory() {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    RedisMessageStore store = new RedisMessageStore(jcf);
    Message<?> message = new GenericMessage<>("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(this.groupId, message);
    message = store.getMessageGroup(this.groupId).getMessages().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 : 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 64 with DirectChannel

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

the class ContinuousQueryMessageProducerTests method setUp.

@Before
public void setUp() {
    queryListenerContainer = mock(ContinuousQueryListenerContainer.class);
    cqMessageProducer = new ContinuousQueryMessageProducer(queryListenerContainer, "foo");
    DirectChannel outputChannel = new DirectChannel();
    cqMessageProducer.setOutputChannel(outputChannel);
    cqMessageProducer.setBeanFactory(mock(BeanFactory.class));
    handler = new CqMessageHandler();
    outputChannel.subscribe(handler);
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) ContinuousQueryListenerContainer(org.springframework.data.gemfire.listener.ContinuousQueryListenerContainer) Before(org.junit.Before)

Example 65 with DirectChannel

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

the class GemfireMessageStoreTests method testWithMessageHistory.

@Test
public void testWithMessageHistory() throws Exception {
    GemfireMessageStore store = new GemfireMessageStore(region);
    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) 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