Skip to content

Commit bd2f47d

Browse files
authored
Merge pull request #218 from yersan/CLOUD-3470-step1
[CLOUD-3470] Add ejb and jpa 2lc test applications
2 parents 2b424a9 + 8c88b37 commit bd2f47d

File tree

11 files changed

+277
-0
lines changed

11 files changed

+277
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>openshift-test-examples</groupId>
6+
<artifactId>test-app-ejb</artifactId>
7+
<version>1.0</version>
8+
<packaging>war</packaging>
9+
<name>test-app-ejb</name>
10+
<description>Example application that tests EJBs via JAX-RS resource</description>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
<failOnMissingWebXml>false</failOnMissingWebXml>
18+
<jakarta.jakartaee-api.version>8.0.0</jakarta.jakartaee-api.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>jakarta.platform</groupId>
24+
<artifactId>jakarta.jakartaee-api</artifactId>
25+
<version>${jakarta.jakartaee-api.version}</version>
26+
<scope>provided</scope>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<finalName>test-app-ejb</finalName>
32+
</build>
33+
34+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.jboss.test.ejb;
2+
3+
import javax.ws.rs.ApplicationPath;
4+
import javax.ws.rs.core.Application;
5+
6+
/**
7+
* @author <a href="mailto:[email protected]">Yeray Borges</a>
8+
*/
9+
@ApplicationPath("/")
10+
public class JAXRSConfig extends Application {
11+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.jboss.test.ejb;
2+
3+
import java.io.Serializable;
4+
5+
import javax.ejb.EJB;
6+
import javax.enterprise.context.SessionScoped;
7+
import javax.ws.rs.GET;
8+
import javax.ws.rs.Path;
9+
import javax.ws.rs.PathParam;
10+
import javax.ws.rs.Produces;
11+
import javax.ws.rs.core.MediaType;
12+
13+
/**
14+
* @author <a href="mailto:[email protected]">Yeray Borges</a>
15+
*/
16+
@Path("/")
17+
@SessionScoped
18+
@Produces(MediaType.TEXT_PLAIN)
19+
public class RestResource implements Serializable {
20+
private static final long serialVersionUID = 1L;
21+
22+
@EJB
23+
StatefulSessionBean sfsb;
24+
25+
@GET
26+
@Path("/")
27+
public String availability() {
28+
return "OK";
29+
}
30+
31+
@GET
32+
@Path("/messages/{message}")
33+
public String createNew(@PathParam(value = "message") String message) {
34+
return sfsb.getMessage(message);
35+
}
36+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.jboss.test.ejb;
2+
3+
import java.io.Serializable;
4+
5+
import javax.ejb.Stateful;
6+
7+
/**
8+
* @author <a href="mailto:[email protected]">Yeray Borges</a>
9+
*/
10+
@Stateful
11+
public class StatefulSessionBean implements Serializable {
12+
private static final long serialVersionUID = 1L;
13+
14+
public String getMessage(String message) {
15+
return "sfsb_"+message;
16+
}
17+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<beans xmlns="http://java.sun.com/xml/ns/javaee"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
6+
</beans>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>openshift-test-examples</groupId>
6+
<artifactId>test-app-jpa2lc</artifactId>
7+
<version>1.0</version>
8+
<packaging>war</packaging>
9+
<name>test-app-jpa2lc</name>
10+
<description>Example application that tests via JAXRS resource a JPA second level cache</description>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
<failOnMissingWebXml>false</failOnMissingWebXml>
18+
<jakarta.jakartaee-api.version>8.0.0</jakarta.jakartaee-api.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>jakarta.platform</groupId>
24+
<artifactId>jakarta.jakartaee-api</artifactId>
25+
<version>${jakarta.jakartaee-api.version}</version>
26+
<scope>provided</scope>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<finalName>test-app-jpa2lc</finalName>
32+
</build>
33+
34+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.jboss.jpa2lc;
2+
3+
import java.io.Serializable;
4+
5+
import javax.persistence.Cacheable;
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
9+
/**
10+
* @author <a href="mailto:[email protected]">Yeray Borges</a>
11+
*/
12+
@Entity
13+
@Cacheable
14+
public class DummyEntity implements Serializable {
15+
private static final long serialVersionUID = 1L;
16+
17+
@Id
18+
private Long id;
19+
20+
public Long getId() {
21+
return id;
22+
}
23+
24+
public void setId(Long id) {
25+
this.id = id;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "DummyEntity{" +
31+
"id=" + id +
32+
'}';
33+
}
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.jboss.jpa2lc;
2+
3+
import javax.ws.rs.ApplicationPath;
4+
import javax.ws.rs.core.Application;
5+
6+
/**
7+
* @author <a href="mailto:[email protected]">Yeray Borges</a>
8+
*/
9+
@ApplicationPath("/")
10+
public class JAXRSConfig extends Application {
11+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.jboss.jpa2lc;
2+
3+
import javax.enterprise.context.RequestScoped;
4+
import javax.inject.Inject;
5+
import javax.persistence.EntityManager;
6+
import javax.persistence.PersistenceContext;
7+
import javax.transaction.UserTransaction;
8+
import javax.ws.rs.GET;
9+
import javax.ws.rs.Path;
10+
import javax.ws.rs.PathParam;
11+
import javax.ws.rs.Produces;
12+
import javax.ws.rs.core.MediaType;
13+
14+
/**
15+
* @author <a href="mailto:[email protected]">Yeray Borges</a>
16+
*/
17+
@Path("/")
18+
@RequestScoped
19+
@Produces(MediaType.TEXT_PLAIN)
20+
public class RestResource {
21+
22+
@PersistenceContext(unitName = "MainPU")
23+
private EntityManager em;
24+
25+
@Inject
26+
private UserTransaction tx;
27+
28+
@GET
29+
@Path("/")
30+
public String availability() {
31+
return "OK";
32+
}
33+
34+
@GET
35+
@Path("/create/{id}")
36+
public String createNew(@PathParam(value = "id") Long id) throws Exception {
37+
final DummyEntity entity = new DummyEntity();
38+
entity.setId(id);
39+
tx.begin();
40+
em.persist(entity);
41+
tx.commit();
42+
43+
return String.format("%d created", id);
44+
}
45+
46+
@GET
47+
@Path("/cache/{id}")
48+
public String addToCacheByQuerying(@PathParam(value = "id") Long id) {
49+
DummyEntity result = em.createQuery("select b from DummyEntity b where b.id=:id", DummyEntity.class)
50+
.setParameter("id", id)
51+
.getSingleResult();
52+
53+
return String.format("%d found", result.getId());
54+
}
55+
56+
@GET
57+
@Path("/evict/{id}")
58+
public String evict(@PathParam(value = "id") Long id) {
59+
em.getEntityManagerFactory().getCache().evict(DummyEntity.class, id);
60+
61+
return String.format("%d evicted", id);
62+
}
63+
64+
@GET
65+
@Path("/isInCache/{id}")
66+
public boolean isEntityInCache(@PathParam(value = "id") Long id) {
67+
return em.getEntityManagerFactory().getCache().contains(DummyEntity.class, id);
68+
}
69+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
5+
<persistence-unit name="MainPU">
6+
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
7+
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
8+
<shared-cache-mode>ALL</shared-cache-mode>
9+
<properties>
10+
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
11+
<property name="hibernate.show_sql" value="true"/>
12+
<property name="hibernate.format_sql" value="true"/>
13+
<property name="hibernate.cache.use_second_level_cache" value="true"/>
14+
<property name="hibernate.cache.use_query_cache" value="true"/>
15+
<property name="hibernate.cache.infinispan.entity.cfg" value="timestamps"/>
16+
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
17+
</properties>
18+
</persistence-unit>
19+
</persistence>

0 commit comments

Comments
 (0)