Copy to ClipboardCopied!Toggle word wrapToggle overflow
이제 Camel 경로 및 JPA 끝점을 구성합니다.
WildFlyCamelContext camelctx = contextFactory.createCamelContext(getClass().getClassLoader());
EntityManagerFactory entityManagerFactory = em.getEntityManagerFactory();
// Configure a transaction manager
JtaTransactionManager transactionManager = new JtaTransactionManager();
transactionManager.setUserTransaction(userTransaction);
transactionManager.afterPropertiesSet();
// Configure the JPA endpoint to use the correct EntityManagerFactory and JtaTransactionManager
final JpaEndpoint jpaEndpoint = new JpaEndpoint();
jpaEndpoint.setCamelContext(camelctx);
jpaEndpoint.setEntityType(Customer.class);
jpaEndpoint.setEntityManagerFactory(entityManagerFactory);
jpaEndpoint.setTransactionManager(transactionManager);
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.to(jpaEndpoint);
}
});
camelctx.start();
WildFlyCamelContext camelctx = contextFactory.createCamelContext(getClass().getClassLoader());
EntityManagerFactory entityManagerFactory = em.getEntityManagerFactory();
// Configure a transaction manager
JtaTransactionManager transactionManager = new JtaTransactionManager();
transactionManager.setUserTransaction(userTransaction);
transactionManager.afterPropertiesSet();
// Configure the JPA endpoint to use the correct EntityManagerFactory and JtaTransactionManager
final JpaEndpoint jpaEndpoint = new JpaEndpoint();
jpaEndpoint.setCamelContext(camelctx);
jpaEndpoint.setEntityType(Customer.class);
jpaEndpoint.setEntityManagerFactory(entityManagerFactory);
jpaEndpoint.setTransactionManager(transactionManager);
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.to(jpaEndpoint);
}
});
camelctx.start();
Copy to ClipboardCopied!Toggle word wrapToggle overflow
마지막으로 '고객' 엔터티를 'direct:start' 엔드포인트에 보낸 다음 ExampleDS 데이터 소스를 쿼리하여 레코드가 저장되었는지 확인할 수 있습니다.
Customer customer = new Customer("John", "Doe");
ProducerTemplate producer = camelctx.createProducerTemplate();
producer.sendBody("direct:start", customer);
// Query the in memory database customer table to verify that a record was saved
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Long> query = criteriaBuilder.createQuery(Long.class);
query.select(criteriaBuilder.count(query.from(Customer.class)));
long recordCount = em.createQuery(query).getSingleResult();
Assert.assertEquals(1L, recordCount);
Customer customer = new Customer("John", "Doe");
ProducerTemplate producer = camelctx.createProducerTemplate();
producer.sendBody("direct:start", customer);
// Query the in memory database customer table to verify that a record was saved
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Long> query = criteriaBuilder.createQuery(Long.class);
query.select(criteriaBuilder.count(query.from(Customer.class)));
long recordCount = em.createQuery(query).getSingleResult();
Assert.assertEquals(1L, recordCount);
Copy to ClipboardCopied!Toggle word wrapToggle overflow