Search in sources :

Example 16 with JpaExecutor

use of org.springframework.integration.jpa.core.JpaExecutor in project spring-integration by spring-projects.

the class JpaPollingChannelAdapterTests method testWithJpaQuery.

/**
 * In this test, a Jpa Polling Channel Adapter will use JpQL query
 * to retrieve a list of records from the database.
 *
 * @throws Exception
 */
@Test
public void testWithJpaQuery() throws Exception {
    testTrigger.reset();
    // ~~~~SETUP~~~~~
    final JpaExecutor jpaExecutor = new JpaExecutor(entityManager);
    jpaExecutor.setJpaQuery("from Student");
    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)

Example 17 with JpaExecutor

use of org.springframework.integration.jpa.core.JpaExecutor in project spring-integration by spring-projects.

the class JpaPollingChannelAdapterTests method testWithJpaQueryAndDeletePerRow.

/**
 * In this test, a Jpa Polling Channel Adapter will use JpQL query
 * to retrieve a list of records from the database. Additionaly, the records
 * will be deleted after the polling.
 *
 * @throws Exception
 */
@Test
@DirtiesContext
public void testWithJpaQueryAndDeletePerRow() throws Exception {
    testTrigger.reset();
    // ~~~~SETUP~~~~~
    final JpaExecutor jpaExecutor = new JpaExecutor(jpaOperations);
    jpaExecutor.setJpaQuery("from Student s");
    jpaExecutor.setDeleteAfterPoll(true);
    jpaExecutor.setDeleteInBatch(false);
    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();
    Thread.sleep(1000);
    final Consumer consumer = new Consumer();
    final List<Message<Collection<?>>> received = new ArrayList<Message<Collection<?>>>();
    received.add(consumer.poll(10000));
    final Message<Collection<?>> message = received.get(0);
    adapter.stop();
    assertNotNull("Message is null.", message);
    assertNotNull(message.getPayload());
    final Collection<?> students = message.getPayload();
    assertTrue(students.size() == 3);
    Long studentCount = waitForDeletes(students);
    assertEquals(Long.valueOf(0), studentCount);
}
Also used : Consumer(org.springframework.integration.jpa.test.Consumer) Message(org.springframework.messaging.Message) 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 18 with JpaExecutor

use of org.springframework.integration.jpa.core.JpaExecutor 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 19 with JpaExecutor

use of org.springframework.integration.jpa.core.JpaExecutor 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 20 with JpaExecutor

use of org.springframework.integration.jpa.core.JpaExecutor 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)

Aggregations

Test (org.junit.Test)24 JpaExecutor (org.springframework.integration.jpa.core.JpaExecutor)24 ArrayList (java.util.ArrayList)9 Collection (java.util.Collection)9 SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)9 Consumer (org.springframework.integration.jpa.test.Consumer)9 Message (org.springframework.messaging.Message)9 AbstractMessageChannel (org.springframework.integration.channel.AbstractMessageChannel)7 JpaOperations (org.springframework.integration.jpa.core.JpaOperations)7 StudentDomain (org.springframework.integration.jpa.test.entity.StudentDomain)4 DirtiesContext (org.springframework.test.annotation.DirtiesContext)4 BeanFactory (org.springframework.beans.factory.BeanFactory)3 LiteralExpression (org.springframework.expression.common.LiteralExpression)3 JpaParameter (org.springframework.integration.jpa.support.JpaParameter)3 PersistMode (org.springframework.integration.jpa.support.PersistMode)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)3 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)3 JpaOutboundGateway (org.springframework.integration.jpa.outbound.JpaOutboundGateway)2 OutboundGatewayType (org.springframework.integration.jpa.support.OutboundGatewayType)2