@Test public void testJpaTransactionManagerRoute() throws Exception { CamelContext camelctx = getContextRegistry().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(); em.getTransaction().begin(); Account result = em.getReference(Account.class, 1); em.getTransaction().commit(); Assert.assertEquals(account, result); }
@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); }
@Produces @ApplicationScoped @Named("jpa") public JpaComponent jpaComponent(PlatformTransactionManager transactionManager, EntityManager entityManager) { JpaComponent component = new JpaComponent(); component.setTransactionManager(transactionManager); component.setEntityManagerFactory(entityManager.getEntityManagerFactory()); return component; }
@Produces @Named("jpa") public JpaComponent createJpaComponent(EntityManager entityManager, PlatformTransactionManager transactionManager) { JpaComponent jpaComponent = new JpaComponent(); jpaComponent.setEntityManagerFactory(entityManager.getEntityManagerFactory()); jpaComponent.setTransactionManager(transactionManager); return jpaComponent; }
@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); }
@Bean @ConditionalOnClass(CamelContext.class) @ConditionalOnMissingBean(JpaComponent.class) public JpaComponent configureJpaComponent(CamelContext camelContext, JpaComponentConfiguration configuration) throws Exception { JpaComponent component = new JpaComponent(); component.setCamelContext(camelContext); Map<String, Object> parameters = new HashMap<>(); IntrospectionSupport.getProperties(configuration, parameters, null, false); IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), component, parameters); return component; }
@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"); }