Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[resolves #1784] Add camel spring JPA example
- Loading branch information
1 parent
ca25a34
commit faabe00
Showing
20 changed files
with
576 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 JPA Spring | ||
------------------------ | ||
|
||
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
64 changes: 64 additions & 0 deletions
64
examples/camel-jpa-spring/src/main/java/org/wildfly/camel/examples/jpa/CustomerServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* #%L | ||
* Wildfly Camel :: Example :: Camel JPA Spring | ||
* %% | ||
* Copyright (C) 2013 - 2017 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); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
examples/camel-jpa-spring/src/main/java/org/wildfly/camel/examples/jpa/model/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* #%L | ||
* Wildfly Camel :: Example :: Camel JPA Spring | ||
* %% | ||
* Copyright (C) 2013 - 2017 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; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...les/camel-jpa-spring/src/main/java/org/wildfly/camel/examples/jpa/model/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
16 changes: 16 additions & 0 deletions
16
examples/camel-jpa-spring/src/main/resources/META-INF/persistence.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
8 changes: 8 additions & 0 deletions
8
examples/camel-jpa-spring/src/main/resources/customers/customer-bob.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
8 changes: 8 additions & 0 deletions
8
examples/camel-jpa-spring/src/main/resources/customers/customer-john.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
1 change: 1 addition & 0 deletions
1
examples/camel-jpa-spring/src/main/resources/org/wildfly/camel/examples/jpa/model/jaxb.index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Customer |
Oops, something went wrong.