Search in sources :

Example 56 with SourcePollingChannelAdapter

use of org.springframework.integration.endpoint.SourcePollingChannelAdapter in project spring-integration by spring-projects.

the class JpaPollingChannelAdapterTests method testWithJpaQueryAndMaxResults.

/**
 * In this test, a Jpa Polling Channel Adapter will use JpQL query
 * to retrieve a list of records from the database with a maxRows value of 1.
 *
 * @throws Exception
 */
@Test
public void testWithJpaQueryAndMaxResults() throws Exception {
    testTrigger.reset();
    // ~~~~SETUP~~~~~
    final JpaExecutor jpaExecutor = new JpaExecutor(entityManager);
    jpaExecutor.setJpaQuery("from Student");
    jpaExecutor.setMaxResultsExpression(new LiteralExpression("1"));
    jpaExecutor.afterPropertiesSet();
    final JpaPollingChannelAdapter jpaPollingChannelAdapter = new JpaPollingChannelAdapter(jpaExecutor);
    final SourcePollingChannelAdapter adapter = JpaTestUtils.getSourcePollingChannelAdapter(jpaPollingChannelAdapter, this.outputChannel, this.poller, this.context, this.getClass().getClassLoader());
    adapter.start();
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final List<Message<Collection<?>>> received = new ArrayList<Message<Collection<?>>>();
    final Consumer consumer = new Consumer();
    received.add(consumer.poll(10000));
    Message<Collection<?>> message = received.get(0);
    adapter.stop();
    assertNotNull(message);
    assertNotNull(message.getPayload());
    Collection<?> primeNumbers = message.getPayload();
    assertTrue(primeNumbers.size() == 1);
}
Also used : Message(org.springframework.messaging.Message) Consumer(org.springframework.integration.jpa.test.Consumer) JpaExecutor(org.springframework.integration.jpa.core.JpaExecutor) LiteralExpression(org.springframework.expression.common.LiteralExpression) ArrayList(java.util.ArrayList) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) Collection(java.util.Collection) Test(org.junit.Test)

Example 57 with SourcePollingChannelAdapter

use of org.springframework.integration.endpoint.SourcePollingChannelAdapter in project spring-integration by spring-projects.

the class JpaPollingChannelAdapterTests method testWithEntityClass.

/**
 * In this test, a Jpa Polling Channel Adapter will use a plain entity class
 * to retrieve a list of records from the database.
 *
 * @throws Exception
 */
@Test
@DirtiesContext
public void testWithEntityClass() throws Exception {
    testTrigger.reset();
    // ~~~~SETUP~~~~~
    final JpaExecutor jpaExecutor = new JpaExecutor(entityManager);
    jpaExecutor.setEntityClass(StudentDomain.class);
    jpaExecutor.afterPropertiesSet();
    final JpaPollingChannelAdapter jpaPollingChannelAdapter = new JpaPollingChannelAdapter(jpaExecutor);
    final SourcePollingChannelAdapter adapter = JpaTestUtils.getSourcePollingChannelAdapter(jpaPollingChannelAdapter, this.outputChannel, this.poller, this.context, this.getClass().getClassLoader());
    adapter.start();
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final List<Message<Collection<?>>> received = new ArrayList<Message<Collection<?>>>();
    final Consumer consumer = new Consumer();
    received.add(consumer.poll(10000));
    Message<Collection<?>> message = received.get(0);
    adapter.stop();
    assertNotNull(message);
    assertNotNull(message.getPayload());
    Collection<?> primeNumbers = message.getPayload();
    assertTrue(primeNumbers.size() == 3);
}
Also used : Message(org.springframework.messaging.Message) Consumer(org.springframework.integration.jpa.test.Consumer) JpaExecutor(org.springframework.integration.jpa.core.JpaExecutor) ArrayList(java.util.ArrayList) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) Collection(java.util.Collection) Test(org.junit.Test) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 58 with SourcePollingChannelAdapter

use of org.springframework.integration.endpoint.SourcePollingChannelAdapter in project spring-integration by spring-projects.

the class JpaPollingChannelAdapterTests method testWithNativeSqlQuery.

/**
 * In this test, a Jpa Polling Channel Adapter will use a Native SQL query
 * to retrieve a list of records from the database.
 *
 * @throws Exception
 */
@Test
public void testWithNativeSqlQuery() throws Exception {
    testTrigger.reset();
    // ~~~~SETUP~~~~~
    final JpaExecutor jpaExecutor = new JpaExecutor(entityManager);
    jpaExecutor.setNativeQuery("select * from Student where lastName = 'Last One'");
    jpaExecutor.afterPropertiesSet();
    final JpaPollingChannelAdapter jpaPollingChannelAdapter = new JpaPollingChannelAdapter(jpaExecutor);
    final SourcePollingChannelAdapter adapter = JpaTestUtils.getSourcePollingChannelAdapter(jpaPollingChannelAdapter, this.outputChannel, this.poller, this.context, this.getClass().getClassLoader());
    adapter.start();
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final List<Message<Collection<?>>> received = new ArrayList<Message<Collection<?>>>();
    final Consumer consumer = new Consumer();
    received.add(consumer.poll(10000));
    Message<Collection<?>> message = received.get(0);
    adapter.stop();
    assertNotNull(message);
    assertNotNull(message.getPayload());
    Collection<?> students = message.getPayload();
    assertTrue(students.size() == 1);
}
Also used : Message(org.springframework.messaging.Message) Consumer(org.springframework.integration.jpa.test.Consumer) JpaExecutor(org.springframework.integration.jpa.core.JpaExecutor) ArrayList(java.util.ArrayList) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) Collection(java.util.Collection) Test(org.junit.Test)

Example 59 with SourcePollingChannelAdapter

use of org.springframework.integration.endpoint.SourcePollingChannelAdapter in project spring-integration by spring-projects.

the class CharacterStreamSourceTests method testEOFIntegrationTest.

@Test
public void testEOFIntegrationTest() throws Exception {
    StringReader reader = new StringReader("test");
    CharacterStreamReadingMessageSource source = new CharacterStreamReadingMessageSource(reader, -1, true);
    SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
    CountDownLatch latch = new CountDownLatch(2);
    source.setApplicationEventPublisher(e -> {
        if (e instanceof StreamClosedEvent) {
            if (latch.getCount() == 1) {
                adapter.stop();
            }
            latch.countDown();
        }
    });
    adapter.setSource(source);
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.afterPropertiesSet();
    adapter.setTaskScheduler(scheduler);
    adapter.setTrigger(new PeriodicTrigger(100));
    QueueChannel out = new QueueChannel();
    adapter.setOutputChannel(out);
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    adapter.start();
    Message<?> received = out.receive(10000);
    assertNotNull(received);
    assertEquals("test", received.getPayload());
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertFalse(adapter.isRunning());
    scheduler.shutdown();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) StringReader(java.io.StringReader) BeanFactory(org.springframework.beans.factory.BeanFactory) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Test(org.junit.Test)

Example 60 with SourcePollingChannelAdapter

use of org.springframework.integration.endpoint.SourcePollingChannelAdapter in project spring-integration by spring-projects.

the class ConsoleInboundChannelAdapterParserTests method adapterWithDefaultCharset.

@Test
public void adapterWithDefaultCharset() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consoleInboundChannelAdapterParserTests.xml", ConsoleInboundChannelAdapterParserTests.class);
    SourcePollingChannelAdapter adapter = context.getBean("adapterWithDefaultCharset.adapter", SourcePollingChannelAdapter.class);
    MessageSource<?> source = (MessageSource<?>) new DirectFieldAccessor(adapter).getPropertyValue("source");
    assertTrue(source instanceof NamedComponent);
    assertEquals("adapterWithDefaultCharset.adapter", adapter.getComponentName());
    assertEquals("stream:stdin-channel-adapter(character)", adapter.getComponentType());
    assertEquals("stream:stdin-channel-adapter(character)", ((NamedComponent) source).getComponentType());
    DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(source);
    Reader bufferedReader = (Reader) sourceAccessor.getPropertyValue("reader");
    assertEquals(BufferedReader.class, bufferedReader.getClass());
    DirectFieldAccessor bufferedReaderAccessor = new DirectFieldAccessor(bufferedReader);
    Reader reader = (Reader) bufferedReaderAccessor.getPropertyValue("in");
    assertEquals(InputStreamReader.class, reader.getClass());
    Charset readerCharset = Charset.forName(((InputStreamReader) reader).getEncoding());
    assertEquals(Charset.defaultCharset(), readerCharset);
    Message<?> message = source.receive();
    assertNotNull(message);
    assertEquals("foo", message.getPayload());
    adapter = context.getBean("pipedAdapterNoCharset.adapter", SourcePollingChannelAdapter.class);
    source = adapter.getMessageSource();
    assertTrue(TestUtils.getPropertyValue(source, "blockToDetectEOF", Boolean.class));
    context.close();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) MessageSource(org.springframework.integration.core.MessageSource) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Charset(java.nio.charset.Charset) NamedComponent(org.springframework.integration.support.context.NamedComponent) Test(org.junit.Test)

Aggregations

SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)66 Test (org.junit.Test)58 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)29 Message (org.springframework.messaging.Message)15 PollableChannel (org.springframework.messaging.PollableChannel)13 Collection (java.util.Collection)10 ArrayList (java.util.ArrayList)9 QueueChannel (org.springframework.integration.channel.QueueChannel)9 JpaExecutor (org.springframework.integration.jpa.core.JpaExecutor)9 Consumer (org.springframework.integration.jpa.test.Consumer)9 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)8 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)8 Trigger (org.springframework.scheduling.Trigger)7 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)6 Expression (org.springframework.expression.Expression)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)5 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)5 File (java.io.File)4