Search in sources :

Example 1 with JpaComponent

use of org.apache.camel.component.jpa.JpaComponent in project camel by apache.

the class JpaRouteTest method testRouteJpa.

@Test
public void testRouteJpa() throws Exception {
    // should auto setup transaction manager and entity factory
    JpaComponent jpa = context.getComponent("jpa", JpaComponent.class);
    assertNotNull("Should have been auto assigned", jpa.getEntityManagerFactory());
    assertNotNull("Should have been auto assigned", jpa.getTransactionManager());
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    ValueBuilder header = mock.message(0).header(JpaConstants.ENTITYMANAGER);
    header.isNotNull();
    header.isInstanceOf(EntityManager.class);
    template.sendBody("direct:start", new SendEmail("someone@somewhere.org"));
    assertMockEndpointsSatisfied();
    assertEntityInDB(1);
}
Also used : ValueBuilder(org.apache.camel.builder.ValueBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SendEmail(org.apache.camel.examples.SendEmail) JpaComponent(org.apache.camel.component.jpa.JpaComponent) Test(org.junit.Test)

Example 2 with JpaComponent

use of org.apache.camel.component.jpa.JpaComponent in project camel by apache.

the class JpaWireTapTest method testRouteJpa.

@Test
public void testRouteJpa() throws Exception {
    // should auto setup transaction manager and entity factory
    JpaComponent jpa = context.getComponent("jpa", JpaComponent.class);
    assertNotNull("Should have been auto assigned", jpa.getEntityManagerFactory());
    assertNotNull("Should have been auto assigned", jpa.getTransactionManager());
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(2);
    template.sendBody("direct:start", new SendEmail("someone@somewhere.org"));
    assertMockEndpointsSatisfied();
    assertEntityInDB(2);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SendEmail(org.apache.camel.examples.SendEmail) JpaComponent(org.apache.camel.component.jpa.JpaComponent) Test(org.junit.Test)

Example 3 with JpaComponent

use of org.apache.camel.component.jpa.JpaComponent in project camel by apache.

the class JpaComponentAutoConfiguration method configureJpaComponent.

@Lazy
@Bean(name = "jpa-component")
@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);
    for (Map.Entry<String, Object> entry : parameters.entrySet()) {
        Object value = entry.getValue();
        Class<?> paramClass = value.getClass();
        if (paramClass.getName().endsWith("NestedConfiguration")) {
            Class nestedClass = null;
            try {
                nestedClass = (Class) paramClass.getDeclaredField("CAMEL_NESTED_CLASS").get(null);
                HashMap<String, Object> nestedParameters = new HashMap<>();
                IntrospectionSupport.getProperties(value, nestedParameters, null, false);
                Object nestedProperty = nestedClass.newInstance();
                IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), nestedProperty, nestedParameters);
                entry.setValue(nestedProperty);
            } catch (NoSuchFieldException e) {
            }
        }
    }
    IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), component, parameters);
    return component;
}
Also used : HashMap(java.util.HashMap) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) HashMap(java.util.HashMap) Map(java.util.Map) JpaComponent(org.apache.camel.component.jpa.JpaComponent) Lazy(org.springframework.context.annotation.Lazy) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

JpaComponent (org.apache.camel.component.jpa.JpaComponent)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 SendEmail (org.apache.camel.examples.SendEmail)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ValueBuilder (org.apache.camel.builder.ValueBuilder)1 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)1 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1 Lazy (org.springframework.context.annotation.Lazy)1