Skip to content

Commit

Permalink
[resolves wildfly-extras#1784] Add camel spring JPA example
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnetherton committed May 17, 2017
1 parent ca25a34 commit ff6bd6a
Show file tree
Hide file tree
Showing 19 changed files with 575 additions and 28 deletions.
52 changes: 52 additions & 0 deletions examples/camel-jpa-spring/README.md
@@ -0,0 +1,52 @@
Camel JPA Spring example
------------------------

This example demonstrates using the camel-jpa component with Spring and the WildFly Camel susbsystem to persist entities to an in-memory database.

In this example, a Camel route consumes XML files from ${JBOSS_JOME}/standalone/data/customers. Camel then uses JAXB to
unmarshal the data to a Customer entity. This entity is then passed to a jpa endpoint and is persisted to a 'customer' database
table.

Prerequisites
-------------

* Maven
* An application server with the wildfly-camel subsystem installed

Running the example
-------------------

To run the example.

1. Start the application server in standalone mode `${JBOSS_HOME}/bin/standalone.sh -c standalone-full-camel.xml`
2. Build and deploy the project `mvn install -Pdeploy`
3. Browse to http://localhost:8080/example-camel-jpa-spring/customers

Testing Camel JMS
-----------------

There are some example customer XML files within the `src/main/resources/customers` directory. To make Camel
consume them and use JPA to persist the data to an in-memory database, simply copy them to the customers input
directory.

For Linux / Mac users:

cp src/main/resources/customers/*.xml ${JBOSS_HOME}/standalone/data/customers/

For Windows users:

copy src\main\resources\customers\*.xml %JBOSS_HOME%/standalone\data\customers\

The console will output messages detailing what happened to each of the orders. The output
will look something like this.

```
2:09:39,385 INFO [input] (Camel (camel-jpa-context) thread #5 - file:///wildfly/standalone/data/customers) Exchange[Id: ID-localhost-localdomain-35597-1438166553663-11-4, ExchangePattern: InOnly, Properties: {CamelBatchComplete=true, CamelBatchIndex=0, CamelBatchSize=1, CamelCreatedTimestamp=Wed Jul 29 12:09:39 BST 2015, CamelEntityManager=org.hibernate.jpa.internal.EntityManagerImpl@466a65b7, CamelFileExchangeFile=GenericFile[/wildfly/standalone/data/customers/customer.xml], CamelFileLockFileAcquired=true, CamelFileLockFileName=/wildfly/standalone/data/customers/customer.xml.camelLock, CamelMessageHistory=[DefaultMessageHistory[routeId=route7, node=unmarshal3], DefaultMessageHistory[routeId=route7, node=to6], DefaultMessageHistory[routeId=route7, node=to7]], CamelToEndpoint=log://input?showAll=true}, Headers: {breadcrumbId=ID-localhost-localdomain-35597-1438166553663-11-3, CamelFileAbsolute=true, CamelFileAbsolutePath=/wildfly/standalone/data/customers/customer.xml, CamelFileContentType=application/xml, CamelFileLastModified=1438168179000, CamelFileLength=418, CamelFileName=customer.xml, CamelFileNameConsumed=customer.xml, CamelFileNameOnly=customer.xml, CamelFileParent=/wildfly/standalone/data/customers, CamelFilePath=/wildfly/standalone/data/customers/customer.xml, CamelFileRelativePath=customer.xml}, BodyType: org.wildfly.camel.examples.jpa.model.Customer, Body: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><customer xmlns="http://org/wildfly/camel/examples/jpa/model/Customer"> <id>2</id> <firstName>John</firstName> <lastName>Doe</lastName> <dateOfBirth>1975-12-25T00:00:00Z</dateOfBirth></customer>, Out: null: ]
```

Browse http://localhost:8080/example-camel-jpa-spring/customers and observe the list of customers that have been extracted from the 'customer' in-memory database table.

Undeploy
--------

To undeploy the example run `mvn clean -Pdeploy`.
101 changes: 101 additions & 0 deletions examples/camel-jpa-spring/pom.xml
@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
#%L
Wildfly Camel :: Example :: Camel JPA
%%
Copyright (C) 2013 - 2014 RedHat
%%
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#L%
-->


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.wildfly.camel</groupId>
<artifactId>wildfly-camel-example</artifactId>
<version>4.7.0-SNAPSHOT</version>
</parent>

<name>Wildfly Camel :: Example :: Camel JPA Spring</name>

<artifactId>example-camel-jpa-spring</artifactId>
<packaging>war</packaging>

<dependencies>

<!-- Provided -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jpa</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.servlet</groupId>
<artifactId>jboss-servlet-api_3.1_spec</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.2_spec</artifactId>
<scope>provided</scope>
</dependency>

</dependencies>

<!-- Build -->
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<skip>${deploy.skip}</skip>
</configuration>
<executions>
<execution>
<id>wildfly-deploy</id>
<phase>install</phase>
<goals>
<goal>deploy-only</goal>
</goals>
</execution>
<execution>
<id>wildfly-undeploy</id>
<phase>clean</phase>
<goals>
<goal>undeploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<!-- Profiles -->
<profiles>
<profile>
<id>deploy</id>
<properties>
<deploy.skip>false</deploy.skip>
</properties>
</profile>
</profiles>
</project>
@@ -0,0 +1,64 @@
/*
* #%L
* Wildfly Camel :: Example :: Camel JPA
* %%
* Copyright (C) 2013 - 2014 RedHat
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.wildfly.camel.examples.jpa;

import java.io.IOException;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.camel.CamelContext;
import org.apache.camel.component.jpa.JpaComponent;
import org.wildfly.camel.examples.jpa.model.Customer;

@SuppressWarnings("serial")
@WebServlet(name = "HttpServiceServlet", urlPatterns = { "/customers/*" }, loadOnStartup = 1)
public class CustomerServlet extends HttpServlet {

@Resource(name = "java:jboss/camel/context/jpa-camel-context")
private CamelContext camelContext;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* Simple servlet to retrieve all customers from the in memory database for
* output and display on customers.jsp
*/
JpaComponent component = camelContext.getComponent("jpa", JpaComponent.class);
EntityManagerFactory entityManagerFactory = component.getEntityManagerFactory();

EntityManager em = entityManagerFactory.createEntityManager();

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

request.setAttribute("customers", em.createQuery(query).getResultList());
request.getRequestDispatcher("customers.jsp").forward(request, response);
}
}
@@ -0,0 +1,78 @@
/*
* #%L
* Wildfly Camel :: Example :: Camel CDI
* %%
* Copyright (C) 2013 - 2014 RedHat
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.wildfly.camel.examples.jpa.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.Date;

@XmlRootElement(name = "customer")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "customer")
public class Customer implements Serializable {

private static final long serialVersionUID = -2372350530824400584L;

@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private Date dateOfBirth;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Date getDateOfBirth() {
return dateOfBirth;
}

public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
}
@@ -0,0 +1,2 @@
@javax.xml.bind.annotation.XmlSchema(namespace = "http://org/wildfly/camel/examples/jpa/model/Customer", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package org.wildfly.camel.examples.jpa.model;
@@ -0,0 +1,16 @@
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name="camel">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<class>org.wildfly.camel.examples.jpa.model.Customer</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.transaction.jta.platform" value="JBossAS" />
</properties>
</persistence-unit>
</persistence>
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<cus:customer xmlns:cus="http://org/wildfly/camel/examples/jpa/model/Customer"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://org/wildfly/camel/examples/jpa/model/Customer">
<cus:firstName>Bob</cus:firstName>
<cus:lastName>Doe</cus:lastName>
<cus:dateOfBirth>1975-12-25</cus:dateOfBirth>
</cus:customer>
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<cus:customer xmlns:cus="http://org/wildfly/camel/examples/jpa/model/Customer"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://org/wildfly/camel/examples/jpa/model/Customer">
<cus:firstName>John</cus:firstName>
<cus:lastName>Doe</cus:lastName>
<cus:dateOfBirth>1975-12-25</cus:dateOfBirth>
</cus:customer>
@@ -0,0 +1 @@
Customer

0 comments on commit ff6bd6a

Please sign in to comment.