org.apache.camel.component.jpa.JpaComponent Java Examples

The following examples show how to use org.apache.camel.component.jpa.JpaComponent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JPATransactionManagerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testJpaTransactionManagerRouteRoute() throws Exception {

    CamelContext camelctx = contextRegistry.getCamelContext("jpa-context");
    Assert.assertNotNull("Expected jpa-context to not be null", camelctx);

    // Persist a new account entity
    Account account = new Account(1, 500);
    camelctx.createProducerTemplate().sendBody("direct:start", account);

    JpaComponent component = camelctx.getComponent("jpa", JpaComponent.class);
    EntityManagerFactory entityManagerFactory = component.getEntityManagerFactory();

    // Read the saved entity back from the database
    EntityManager em = entityManagerFactory.createEntityManager();
    Account result = em.getReference(Account.class, 1);
    Assert.assertEquals(account, result);
}
 
Example #2
Source File: JpaComponentAutoConfiguration.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
@Lazy
@Bean(name = "jpa-component")
@ConditionalOnMissingBean(JpaComponent.class)
public JpaComponent configureJpaComponent() throws Exception {
    JpaComponent component = new JpaComponent();
    component.setCamelContext(camelContext);
    Map<String, Object> parameters = new HashMap<>();
    IntrospectionSupport.getProperties(configuration, parameters, null,
            false);
    CamelPropertiesHelper.setCamelProperties(camelContext, component,
            parameters, false);
    if (ObjectHelper.isNotEmpty(customizers)) {
        for (ComponentCustomizer<JpaComponent> customizer : customizers) {
            boolean useCustomizer = (customizer instanceof HasId)
                    ? HierarchicalPropertiesEvaluator.evaluate(
                            applicationContext.getEnvironment(),
                            "camel.component.customizer",
                            "camel.component.jpa.customizer",
                            ((HasId) customizer).getId())
                    : HierarchicalPropertiesEvaluator.evaluate(
                            applicationContext.getEnvironment(),
                            "camel.component.customizer",
                            "camel.component.jpa.customizer");
            if (useCustomizer) {
                LOGGER.debug("Configure component {}, with customizer {}",
                        component, customizer);
                customizer.customize(component);
            }
        }
    }
    return component;
}
 
Example #3
Source File: JPAComponentProducer.java    From wildfly-camel-examples with Apache License 2.0 5 votes vote down vote up
@Produces
@ApplicationScoped
@Named("jpa")
public JpaComponent jpaComponent(PlatformTransactionManager transactionManager, EntityManager entityManager) {
    JpaComponent component = new JpaComponent();
    component.setTransactionManager(transactionManager);
    component.setEntityManagerFactory(entityManager.getEntityManagerFactory());
    return component;
}
 
Example #4
Source File: JpaComponentProducer.java    From wildfly-camel-examples with Apache License 2.0 5 votes vote down vote up
@Produces
@Named("jpa")
public JpaComponent createJpaComponent(EntityManager entityManager, PlatformTransactionManager transactionManager) {
    JpaComponent jpaComponent = new JpaComponent();
    jpaComponent.setEntityManagerFactory(entityManager.getEntityManagerFactory());
    jpaComponent.setTransactionManager(transactionManager);
    return jpaComponent;
}
 
Example #5
Source File: OrdersServlet.java    From wildfly-camel-examples with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Gets all orders saved to the in-memory database 'orders' table
    JpaComponent component = camelContext.getComponent("jpa", JpaComponent.class);
    EntityManagerFactory entityManagerFactory = component.getEntityManagerFactory();

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> query = criteriaBuilder.createQuery(Order.class);
    query.select(query.from(Order.class));

    request.setAttribute("orders", entityManager.createQuery(query).getResultList());
    request.getRequestDispatcher("orders.jsp").forward(request, response);
}
 
Example #6
Source File: JpaRouteBuilder.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Override
public void configure() throws Exception {

    // Configure JPA component
    JpaComponent jpaComponent = new JpaComponent();
    jpaComponent.setEntityManagerFactory(em.getEntityManagerFactory());
    jpaComponent.setTransactionManager(transactionManager);
    getContext().addComponent("jpa", jpaComponent);

    onException(IllegalArgumentException.class)
        .maximumRedeliveries(1)
        .handled(true)
        .convertBodyTo(String.class)
        .to("file:{{jboss.server.data.dir}}/deadletter?fileName=deadLetters.xml")
        .markRollbackOnly();

    from("direct:start")
        .transacted()
        .setHeader("targetAccountId", simple("${body[targetAccountId]}"))
        .setHeader("amount", simple("${body[amount]}"))

    // Take amount from the source account and decrement balance
    .to("sql:update account set balance = balance - :#amount where id = :#sourceAccountId?dataSource=wildFlyExampleDS")
    .choice()
        .when(simple("${header.amount} > 500"))
            .log("Amount is too large! Rolling back transaction")
            .throwException(new IllegalArgumentException("Amount too large"))
        .otherwise()
            .to("direct:txsmall");

    from("direct:txsmall")
    .to("sql:select balance from account where id = :#targetAccountId?dataSource=wildFlyExampleDS")
    .process(new Processor() {
        @Override
        @SuppressWarnings("unchecked")
        public void process(Exchange exchange) throws Exception {
            List<Map<String, Object>> result = (List<Map<String, Object>>) exchange.getIn().getBody();
            int id = exchange.getIn().getHeader("targetAccountId", Integer.class);
            int amount = exchange.getIn().getHeader("amount", Integer.class);
            int balance = (int) result.get(0).get("BALANCE");

            // Update target account with new balance
            Account account = em.find(Account.class, id);

            account.setBalance(balance + amount);
            exchange.getOut().setBody(account);
        }
    })
    .to("jpa:org.wildfly.camel.test.jpa.subA.Account");
}