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);
}
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);
}
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);
}
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();
}
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();
}
Aggregations