Skip to content

Commit

Permalink
Example with protostream marshalling fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
karesti committed Dec 12, 2024
1 parent 9592eab commit 775304e
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/includes/attributes.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
:project-version: 1.0.0
:infinispan-version: 15.0.8.Final
:infinispan-version: 15.0.11.Final
:examples-dir: ./../examples/
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.infinispan.quarkus.embedded;

import org.infinispan.protostream.GeneratedSchema;
import org.infinispan.protostream.annotations.Proto;
import org.infinispan.protostream.annotations.ProtoSchema;

@Proto
public record Book(String name, String author) {

@ProtoSchema(includeClasses = Book.class, schemaPackageName = "it.quarkus")
public interface BookSchema extends GeneratedSchema {

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.infinispan.quarkus.embedded;

import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_PROTOSTREAM;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand All @@ -10,13 +12,15 @@
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;

import org.infinispan.Cache;
import org.infinispan.commons.api.CacheContainerAdmin;
import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.manager.DefaultCacheManager;
Expand Down Expand Up @@ -117,4 +121,29 @@ public String simpleCluster() throws IOException {
}
return "Success";
}

@Path("PROTO/GET/books/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Book get(@PathParam("id") String id) {
Cache<String, Book> books = emc.administration()
.withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
.getOrCreateCache("books", new ConfigurationBuilder()
.encoding().mediaType(APPLICATION_PROTOSTREAM)
.build());
return books.get(id);
}

@Path("PROTO/POST/books/{id}")
@POST
@Produces(MediaType.TEXT_PLAIN)
public String create(@PathParam("id") String id, Book book) {
Cache<String, Book> books = emc.administration()
.withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
.getOrCreateCache("books", new ConfigurationBuilder()
.encoding().mediaType(APPLICATION_PROTOSTREAM)
.build());
books.put(id, book);
return id;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.infinispan.quarkus.embedded;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.is;

Expand All @@ -9,12 +10,14 @@
import java.nio.file.Paths;
import java.util.stream.Stream;

import org.hamcrest.CoreMatchers;
import org.infinispan.commons.util.Util;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;

import io.quarkus.logging.Log;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;

/**
* @author William Burns
Expand Down Expand Up @@ -101,4 +104,25 @@ public void testSimpleCluster() {
Log.info("Running cluster test");
when().get("/test/CLUSTER").then().body(is("Success"));
}

@Test
public void testProtostreamCache() {
Log.info("Protostream marshalling class");
given()
.contentType(ContentType.JSON)
.body("{\"name\":\"Infinispan Book\",\"author\":\"Jack Nicholson\"}")
.when()
.post("/test/PROTO/POST/books/123")
.then()
.statusCode(200)
.body(CoreMatchers.is("123"));

given()
.when().get("/test/PROTO/GET/books/123")
.then()
.statusCode(200)
.body(CoreMatchers.is(
"{\"name\":\"Infinispan Book\",\"author\":\"Jack Nicholson\"}"));

}
}
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
<module>docs</module>
</modules>
</profile>

<profile>
<id>it</id>
<activation>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
package io.quarkiverse.infinispan.embedded.sample;

import org.infinispan.protostream.GeneratedSchema;
import org.infinispan.protostream.annotations.Proto;
import org.infinispan.protostream.annotations.ProtoSchema;

@Proto
public record Greeting(String name, String message) {
@ProtoSchema(includeClasses = { Greeting.class }, schemaPackageName = "io.quarkiverse.infinispan")
public interface GameSchema extends GeneratedSchema {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

import org.infinispan.Cache;
import org.infinispan.commons.api.CacheContainerAdmin;
import org.infinispan.commons.dataconversion.MediaType;
import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.health.ClusterHealth;
import org.infinispan.manager.EmbeddedCacheManager;

import io.quarkus.logging.Log;
Expand All @@ -28,13 +30,19 @@ public class InfinispanGreetingResource {
@Startup
void init() {
Configuration config = new ConfigurationBuilder()
.encoding().mediaType(MediaType.APPLICATION_PROTOSTREAM)
.clustering().cacheMode(CacheMode.DIST_ASYNC).build();
Log.info("Create mycache with config " + config);
cacheManager.administration()
.withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
.getOrCreateCache(CACHE_NAME, config);
}

@GET
public ClusterHealth clusterHealth() {
return cacheManager.getHealth().getClusterHealth();
}

@POST
@Path("/{id}")
public CompletionStage<String> postGreeting(String id, Greeting greeting) {
Expand Down

0 comments on commit 775304e

Please sign in to comment.