Search in sources :

Example 16 with NullChannel

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

the class PollerAdviceTests method configure.

private void configure(SourcePollingChannelAdapter adapter) {
    adapter.setOutputChannel(new NullChannel());
    adapter.setBeanFactory(mock(BeanFactory.class));
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.afterPropertiesSet();
    adapter.setTaskScheduler(scheduler);
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) NullChannel(org.springframework.integration.channel.NullChannel) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)

Example 17 with NullChannel

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

the class FileWritingMessageHandlerTests method supportedTypeAndPermissions.

@Test
public void supportedTypeAndPermissions() throws Exception {
    if (FileUtils.IS_POSIX) {
        handler.setChmod(0777);
    }
    handler.setOutputChannel(new NullChannel());
    handler.handleMessage(new GenericMessage<String>("test"));
    File[] output = outputDirectory.listFiles();
    assertThat(output.length, equalTo(1));
    assertThat(output[0], notNullValue());
    if (FileUtils.IS_POSIX) {
        Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(output[0].toPath());
        assertThat(permissions.size(), equalTo(9));
    }
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) NullChannel(org.springframework.integration.channel.NullChannel) File(java.io.File) Test(org.junit.Test)

Example 18 with NullChannel

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

the class ConnectionFactoryTests method testObtainConnectionIds.

public void testObtainConnectionIds(AbstractServerConnectionFactory serverFactory) throws Exception {
    final List<IpIntegrationEvent> events = Collections.synchronizedList(new ArrayList<IpIntegrationEvent>());
    int expectedEvents = serverFactory instanceof TcpNetServerConnectionFactory ? // Listening, + OPEN, CLOSE, EXCEPTION for each side
    7 : // Listening, + OPEN, CLOSE (but we *might* get exceptions, depending on timing).
    5;
    final CountDownLatch serverListeningLatch = new CountDownLatch(1);
    final CountDownLatch eventLatch = new CountDownLatch(expectedEvents);
    ApplicationEventPublisher publisher = new ApplicationEventPublisher() {

        @Override
        public void publishEvent(ApplicationEvent event) {
            LogFactory.getLog(this.getClass()).trace("Received: " + event);
            events.add((IpIntegrationEvent) event);
            if (event instanceof TcpConnectionServerListeningEvent) {
                serverListeningLatch.countDown();
            }
            eventLatch.countDown();
        }

        @Override
        public void publishEvent(Object event) {
        }
    };
    serverFactory.setBeanName("serverFactory");
    serverFactory.setApplicationEventPublisher(publisher);
    serverFactory = spy(serverFactory);
    final CountDownLatch serverConnectionInitLatch = new CountDownLatch(1);
    doAnswer(invocation -> {
        Object result = invocation.callRealMethod();
        serverConnectionInitLatch.countDown();
        return result;
    }).when(serverFactory).wrapConnection(any(TcpConnectionSupport.class));
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.setPoolSize(10);
    scheduler.afterPropertiesSet();
    BeanFactory bf = mock(BeanFactory.class);
    when(bf.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
    when(bf.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class)).thenReturn(scheduler);
    serverFactory.setBeanFactory(bf);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setOutputChannel(new NullChannel());
    adapter.setConnectionFactory(serverFactory);
    adapter.start();
    assertTrue("Listening event not received", serverListeningLatch.await(10, TimeUnit.SECONDS));
    assertThat(events.get(0), instanceOf(TcpConnectionServerListeningEvent.class));
    assertThat(((TcpConnectionServerListeningEvent) events.get(0)).getPort(), equalTo(serverFactory.getPort()));
    int port = serverFactory.getPort();
    TcpNetClientConnectionFactory clientFactory = new TcpNetClientConnectionFactory("localhost", port);
    clientFactory.registerListener(message -> false);
    clientFactory.setBeanName("clientFactory");
    clientFactory.setApplicationEventPublisher(publisher);
    clientFactory.start();
    TcpConnectionSupport client = clientFactory.getConnection();
    List<String> clients = clientFactory.getOpenConnectionIds();
    assertEquals(1, clients.size());
    assertTrue(clients.contains(client.getConnectionId()));
    assertTrue("Server connection failed to register", serverConnectionInitLatch.await(10, TimeUnit.SECONDS));
    List<String> servers = serverFactory.getOpenConnectionIds();
    assertEquals(1, servers.size());
    assertTrue(serverFactory.closeConnection(servers.get(0)));
    servers = serverFactory.getOpenConnectionIds();
    assertEquals(0, servers.size());
    int n = 0;
    clients = clientFactory.getOpenConnectionIds();
    while (n++ < 100 && clients.size() > 0) {
        Thread.sleep(100);
        clients = clientFactory.getOpenConnectionIds();
    }
    assertEquals(0, clients.size());
    assertTrue(eventLatch.await(10, TimeUnit.SECONDS));
    assertThat("Expected at least " + expectedEvents + " events; got: " + events.size() + " : " + events, events.size(), greaterThanOrEqualTo(expectedEvents));
    FooEvent event = new FooEvent(client, "foo");
    client.publishEvent(event);
    assertThat("Expected at least " + expectedEvents + " events; got: " + events.size() + " : " + events, events.size(), greaterThanOrEqualTo(expectedEvents + 1));
    try {
        event = new FooEvent(mock(TcpConnectionSupport.class), "foo");
        client.publishEvent(event);
        fail("Expected exception");
    } catch (IllegalArgumentException e) {
        assertTrue("Can only publish events with this as the source".equals(e.getMessage()));
    }
    SocketAddress address = serverFactory.getServerSocketAddress();
    if (address instanceof InetSocketAddress) {
        InetSocketAddress inetAddress = (InetSocketAddress) address;
        assertEquals(port, inetAddress.getPort());
    }
    serverFactory.stop();
    scheduler.shutdown();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IpIntegrationEvent(org.springframework.integration.ip.event.IpIntegrationEvent) ApplicationEvent(org.springframework.context.ApplicationEvent) TcpReceivingChannelAdapter(org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) BeanFactory(org.springframework.beans.factory.BeanFactory) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) NullChannel(org.springframework.integration.channel.NullChannel)

Example 19 with NullChannel

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

the class MqttAdapterTests method buildAdapter.

private MqttPahoMessageDrivenChannelAdapter buildAdapter(final IMqttClient client, Boolean cleanSession, ConsumerStopAction action) throws MqttException {
    DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory() {

        @Override
        public IMqttClient getClientInstance(String uri, String clientId) throws MqttException {
            return client;
        }
    };
    factory.setServerURIs("tcp://localhost:1883");
    if (cleanSession != null) {
        factory.setCleanSession(cleanSession);
    }
    if (action != null) {
        factory.setConsumerStopAction(action);
    }
    given(client.isConnected()).willReturn(true);
    MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("client", factory, "foo");
    adapter.setApplicationEventPublisher(mock(ApplicationEventPublisher.class));
    adapter.setOutputChannel(new NullChannel());
    adapter.setTaskScheduler(mock(TaskScheduler.class));
    adapter.afterPropertiesSet();
    return adapter;
}
Also used : DefaultMqttPahoClientFactory(org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory) MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TaskScheduler(org.springframework.scheduling.TaskScheduler) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) NullChannel(org.springframework.integration.channel.NullChannel)

Aggregations

NullChannel (org.springframework.integration.channel.NullChannel)19 Test (org.junit.Test)12 BeanFactory (org.springframework.beans.factory.BeanFactory)10 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)7 File (java.io.File)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Log (org.apache.commons.logging.Log)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Message (org.springframework.amqp.core.Message)2 AsyncRabbitTemplate (org.springframework.amqp.rabbit.AsyncRabbitTemplate)2 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)2 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)2 TaskScheduler (org.springframework.scheduling.TaskScheduler)2 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1