Skip to content

Commit

Permalink
Disable Vert.x TCCL management
Browse files Browse the repository at this point in the history
This breaks dev mode, and in general is not needed as Quarkus can
perform it's own TCCL management when required. It also provides a
slight performance boost.

Fixes quarkusio#18299
  • Loading branch information
stuartwdouglas committed Jul 22, 2021
1 parent 5cf94dc commit 815b69e
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 1 deletion.
2 changes: 1 addition & 1 deletion extensions/hibernate-reactive/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-deployment</artifactId>
<artifactId>quarkus-resteasy-reactive-jsonb-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.quarkus.hibernate.reactive.dev;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "known_fruits")
@NamedQuery(name = "Fruits.findAll", query = "SELECT f FROM Fruit f ORDER BY f.name")
public class Fruit {

@Id
@SequenceGenerator(name = "fruitsSequence", sequenceName = "known_fruits_id_seq", allocationSize = 1, initialValue = 10)
@GeneratedValue(generator = "fruitsSequence")
private Integer id;

@Column(length = 40, unique = true)
private String name;

public Fruit() {
}

public Fruit(String name) {
this.name = name;
}

public Integer getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Fruit{" + id + "," + name + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package io.quarkus.hibernate.reactive.dev;

import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import static javax.ws.rs.core.Response.Status.NO_CONTENT;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.hibernate.reactive.mutiny.Mutiny;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.RestPath;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import io.smallrye.mutiny.Uni;

@Path("fruits")
@ApplicationScoped
@Produces("application/json")
@Consumes("application/json")
public class FruitMutinyResource {
private static final Logger LOGGER = Logger.getLogger(FruitMutinyResource.class);

@Inject
Mutiny.SessionFactory sf;

@GET
public Uni<List<Fruit>> get() {
return sf.withTransaction((s, t) -> s
.createNamedQuery("Fruits.findAll", Fruit.class)
.getResultList());
}

@GET
@Path("{id}")
public Uni<Fruit> getSingle(@RestPath Integer id) {
return sf.withTransaction((s, t) -> s.find(Fruit.class, id));
}

@POST
public Uni<Response> create(Fruit fruit) {
if (fruit == null || fruit.getId() != null) {
throw new WebApplicationException("Id was invalidly set on request.", 422);
}

return sf.withTransaction((s, t) -> s.persist(fruit))
.replaceWith(() -> Response.ok(fruit).status(CREATED).build());
}

@PUT
@Path("{id}")
public Uni<Response> update(@RestPath Integer id, Fruit fruit) {
if (fruit == null || fruit.getName() == null) {
throw new WebApplicationException("Fruit name was not set on request.", 422);
}

return sf.withTransaction((s, t) -> s.find(Fruit.class, id)
// If entity exists then update it
.onItem().ifNotNull().invoke(entity -> entity.setName(fruit.getName()))
.onItem().ifNotNull().transform(entity -> Response.ok(entity).build())
// If entity not found return the appropriate response
.onItem().ifNull()
.continueWith(() -> Response.ok().status(NOT_FOUND).build()));
}

@DELETE
@Path("{id}")
public Uni<Response> delete(@RestPath Integer id) {
return sf.withTransaction((s, t) -> s.find(Fruit.class, id)
// If entity exists then delete it
.onItem().ifNotNull()
.transformToUni(entity -> s.remove(entity)
.replaceWith(() -> Response.ok().status(NO_CONTENT).build()))
// If entity not found return the appropriate response
.onItem().ifNull().continueWith(() -> Response.ok().status(NOT_FOUND).build()));
}

/**
* Create a HTTP response from an exception.
*
* Response Example:
*
* <pre>
* HTTP/1.1 422 Unprocessable Entity
* Content-Length: 111
* Content-Type: application/json
*
* {
* "code": 422,
* "error": "Fruit name was not set on request.",
* "exceptionType": "javax.ws.rs.WebApplicationException"
* }
* </pre>
*/
@Provider
public static class ErrorMapper implements ExceptionMapper<Exception> {

@Inject
ObjectMapper objectMapper;

@Override
public Response toResponse(Exception exception) {
LOGGER.error("Failed to handle request", exception);

int code = 500;
if (exception instanceof WebApplicationException) {
code = ((WebApplicationException) exception).getResponse().getStatus();
}

ObjectNode exceptionJson = objectMapper.createObjectNode();
exceptionJson.put("exceptionType", exception.getClass().getName());
exceptionJson.put("code", code);

if (exception.getMessage() != null) {
exceptionJson.put("error", exception.getMessage());
}

return Response.status(code)
.entity(exceptionJson)
.build();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.quarkus.hibernate.reactive.dev;

import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.Arrays;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusDevModeTest;
import io.restassured.response.Response;

/**
* Checks that public field access is correctly replaced with getter/setter calls,
* regardless of the field type.
*/
public class HibernateReactiveDevModeTest {

@RegisterExtension
static QuarkusDevModeTest runner = new QuarkusDevModeTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(Fruit.class, FruitMutinyResource.class).addAsResource("application.properties")
.addAsResource(new StringAsset("INSERT INTO known_fruits(id, name) VALUES (1, 'Cherry');\n" +
"INSERT INTO known_fruits(id, name) VALUES (2, 'Apple');\n" +
"INSERT INTO known_fruits(id, name) VALUES (3, 'Banana');\n"), "import.sql"));

@Test
public void testListAllFruits() {
Response response = given()
.when()
.get("/fruits")
.then()
.statusCode(200)
.contentType("application/json")
.extract().response();
assertThat(response.jsonPath().getList("name")).isEqualTo(Arrays.asList("Apple", "Banana", "Cherry"));

runner.modifySourceFile(Fruit.class, s -> s.replace("ORDER BY f.name", "ORDER BY f.name desk"));
given()
.when()
.get("/fruits")
.then()
.statusCode(500);

runner.modifySourceFile(Fruit.class, s -> s.replace("desk", "desc"));
response = given()
.when()
.get("/fruits")
.then()
.statusCode(200)
.contentType("application/json")
.extract().response();
assertThat(response.jsonPath().getList("name")).isEqualTo(Arrays.asList("Cherry", "Banana", "Apple"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ EventLoopCountBuildItem eventLoopCount(VertxCoreRecorder recorder, VertxConfigur
return new EventLoopCountBuildItem(recorder.calculateEventLoopThreads(vertxConfiguration));
}

@BuildStep
LogCleanupFilterBuildItem cleanupVertxWarnings() {
return new LogCleanupFilterBuildItem("io.vertx.core.impl.ContextImpl", "You have disabled TCCL checks");
}

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
IOThreadDetectorBuildItem ioThreadDetector(VertxCoreRecorder recorder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
@Recorder
public class VertxCoreRecorder {

static {
System.setProperty("vertx.disableTCCL", "true");
}

private static final Logger LOGGER = Logger.getLogger(VertxCoreRecorder.class.getName());
public static final String VERTX_CACHE = "vertx-cache";

Expand Down

0 comments on commit 815b69e

Please sign in to comment.