Skip to content

Commit

Permalink
ISPN-15856 Updates to Infinispan 15.0 Final
Browse files Browse the repository at this point in the history
  • Loading branch information
karesti committed Mar 15, 2024
1 parent 0da8522 commit fea56fd
Show file tree
Hide file tree
Showing 29 changed files with 126 additions and 338 deletions.
3 changes: 3 additions & 0 deletions documentation/asciidoc/topics/ref_remote_tutorials.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
= Remote cache tutorials

To run these tutorials you need at least one locally running instance of {brandname} Server.
Each tutorial will try to connect to a running server in `localhost:11222` with
`admin/password` credentials. However, if a Docker instance is found, and the server is not running,
tutorials will spin up a local server with `Testcontainers`.

ifdef::community[]
To run the Server as a container image, visit the "Get Started" page
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.infinispan.tutorial.simple.remote.admin;

import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.commons.configuration.XMLStringConfiguration;
import org.infinispan.commons.configuration.StringConfiguration;
import org.infinispan.tutorial.simple.connect.TutorialsConnectorHelper;

import java.io.IOException;
Expand Down Expand Up @@ -36,18 +36,18 @@ public static void main(String[] args) throws Exception {
}

private void stop() {
manager.stop();
TutorialsConnectorHelper.stop(manager);
}

/**
* Creates a cache named CacheWithXMLConfiguration and uses the
* XMLStringConfiguration() method to pass the cache definition as
* StringConfiguration() method to pass the cache definition as
* valid infinispan.xml.
*/
private void createCacheWithXMLConfiguration() throws IOException {
String cacheName = "CacheWithXMLConfiguration";
String xml = Files.readString(Paths.get(this.getClass().getClassLoader().getResource("CacheWithXMLConfiguration.xml").getPath()));
manager.administration().getOrCreateCache(cacheName, new XMLStringConfiguration(xml));
manager.administration().getOrCreateCache(cacheName, new StringConfiguration(xml));
System.out.println("Cache with configuration exists or is created.");
}

Expand All @@ -59,7 +59,7 @@ private void cacheWithTemplate() throws IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("cacheTemplate.xml").getPath());
String xmlTemplate = Files.readString(path);
try {
manager.administration().createTemplate("template", new XMLStringConfiguration(xmlTemplate));
manager.administration().createTemplate("template", new StringConfiguration(xmlTemplate));
} catch (Exception ce) {
// If the
System.out.println(ce.getMessage());
Expand Down
9 changes: 9 additions & 0 deletions infinispan-remote/cache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,14 @@
<groupId>org.infinispan</groupId>
<artifactId>infinispan-client-hotrod</artifactId>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-server-testdriver-core</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.19.1</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void main(String[] args) {
// Retrieve the value and print it out
System.out.printf("key = %s\n", cache.get("key"));
// Stop the cache manager and release all resources
cacheManager.stop();
TutorialsConnectorHelper.stop(cacheManager);
}

}
9 changes: 9 additions & 0 deletions infinispan-remote/connect-to-infinispan-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,14 @@
<groupId>org.infinispan</groupId>
<artifactId>infinispan-client-hotrod</artifactId>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-server-testdriver-core</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.19.1</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.infinispan.client.hotrod.configuration.ClientIntelligence;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.client.hotrod.impl.ConfigurationProperties;
import org.infinispan.server.test.core.InfinispanContainer;

/**
* Utility class for the simple tutorials in client server mode.
Expand All @@ -30,14 +31,15 @@ public class TutorialsConnectorHelper {
*/
public static final ConfigurationBuilder connectionConfig() {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.addServer().host(HOST).port(SINGLE_PORT).security()
builder.security()
.authentication()
//Add user credentials.
.username(USER)
.password(PASSWORD);

// Docker 4 Mac Workaround. Don't use BASIC intelligence in production
builder.clientIntelligence(ClientIntelligence.BASIC);
// ### Docker 4 Mac Workaround. Don't use BASIC intelligence in production
/* ### ALERT!! Don't add this line in production by default */ builder.clientIntelligence(ClientIntelligence.BASIC);
// ### Docker 4 Mac Workaround. Don't use BASIC intelligence in production

// Make sure the remote cache is available.
// If the cache does not exist, the cache will be created
Expand All @@ -59,14 +61,48 @@ public static final RemoteCacheManager connect() {
return connect(connectionConfig());
}

static InfinispanContainer infinispanContainer;

public static final RemoteCacheManager connect(ConfigurationBuilder builder) {
RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());

// Clear the cache in case it already exists from a previous running tutorial
cacheManager.getCache(TUTORIAL_CACHE_NAME).clear();
RemoteCacheManager cacheManager = null;
try {
builder.addServer().host(HOST).port(SINGLE_PORT);
cacheManager = new RemoteCacheManager(builder.build());
// Clear the cache in case it already exists from a previous running tutorial
cacheManager.getCache(TUTORIAL_CACHE_NAME).clear();
} catch (Exception ex) {
System.out.println("Unable to connect to a running server in localhost:11222. Try test containers");
if (cacheManager != null) {
cacheManager.stop();
}
cacheManager = null;
}

if (cacheManager == null) {
try {
infinispanContainer = new InfinispanContainer();
infinispanContainer.withUser(USER);
infinispanContainer.withPassword(PASSWORD);
infinispanContainer.start();
builder.addServer().host(HOST).port(infinispanContainer.getFirstMappedPort());
cacheManager = new RemoteCacheManager(builder.build());
// Clear the cache in case it already exists from a previous running tutorial
cacheManager.getCache(TUTORIAL_CACHE_NAME).clear();
} catch (Exception ex) {
System.out.println("Infinispan Server start with Testcontainers failed. Exit");
System.exit(0);
}
}
// Return the connected cache manager
return cacheManager;
}

public static void stop(RemoteCacheManager cacheManager) {
cacheManager.stop();
if (infinispanContainer != null) {
infinispanContainer.stop();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static void main(String[] args) throws Exception {
ConfigurationBuilder builder = TutorialsConnectorHelper.connectionConfig();
InstaSchemaImpl schema = new InstaSchemaImpl();
builder.addContextInitializer(schema);
RemoteCacheManager client = new RemoteCacheManager(builder.build());
RemoteCacheManager client = TutorialsConnectorHelper.connect(builder);
// Create and add the Protobuf schema for InstaPost class. Note InstaPost is an annotated POJO
register(schema, client);

Expand All @@ -91,7 +91,7 @@ public static void main(String[] args) throws Exception {
// This method will be executed every time new items that correspond with the query arrive
@Override
public void resultJoining(String key, InstaPost post) {
System.out.println(String.format("@%s has posted again! Hashtag: #%s", post.user, post.hashtag));
System.out.println(String.format("@%s has posted again! Hashtag: #%s", post.user(), post.hashtag()));
queryPosts.add(post);
}
};
Expand All @@ -118,7 +118,7 @@ public void resultJoining(String key, InstaPost post) {
client.administration().removeCache(CACHE_NAME);

// Stop the client and release all resources
client.stop();
TutorialsConnectorHelper.stop(client);
}

private static void addRandomPost(RemoteCache<String, InstaPost> cache) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,12 @@
package org.infinispan.tutorial.simple.remote.query;

import java.util.Objects;

import org.infinispan.protostream.annotations.ProtoFactory;
import org.infinispan.protostream.annotations.ProtoField;
import org.infinispan.protostream.annotations.Proto;

/**
* This class is annotated with the infinispan Protostream support annotations. With this method, you don't need to
* define a protobuf file and a marshaller for the object. See this tutorial for additional explanations:
* <p>
* https://blog.infinispan.org/2018/06/making-java-objects-queryable-by.html
*/
public final class InstaPost {

@ProtoField(number = 1)
String id;

@ProtoField(number = 2)
String user;

@ProtoField(number = 3)
String hashtag;

public InstaPost() {
// An accessible no-arg constructor is required for Protostream to be able to instantiate this
}

@ProtoFactory
public InstaPost(String id, String user, String hashtag) {
this.id = id;
this.user = user;
this.hashtag = hashtag;
}

@Override
public String toString() {
return "InstaPost{" +
"id='" + id + '\'' +
", user='" + user + '\'' +
", hashtag='" + hashtag + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
InstaPost instaPost = (InstaPost) o;
return Objects.equals(id, instaPost.id) &&
Objects.equals(user, instaPost.user) &&
Objects.equals(hashtag, instaPost.hashtag);
}

@Override
public int hashCode() {
return Objects.hash(id, user, hashtag);
}
}
@Proto
public record InstaPost(String id, String user, String hashtag){}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.infinispan.tutorial.simple.remote.query;

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

@AutoProtoSchemaBuilder(schemaFileName = "instapost.proto",
@ProtoSchema(schemaFileName = "instapost.proto",
schemaPackageName = "tutorial",
includeClasses = InstaPost.class)
public interface InstaSchema extends GeneratedSchema {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static void main(String[] args) throws Exception {
System.out.println("Counter-3 value is " + counter3.getValue());

// Stop the cache manager and release all resources
cacheManager.stop();
TutorialsConnectorHelper.stop(cacheManager);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void main(String[] args) {
// Connecting to NYC http://localhost:31222/console/cache/xsiteCache
// hello (copied from LON) and hello-nyc both exist in NYC
// hello exists in LON, but hello-nyc is absent because replication is active-passive from LON to NYC
client.stop();
TutorialsConnectorHelper.stop(client);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void main(String[] args) throws Exception {
builder.remoteCache("jsonCache").configurationURI(jsonCacheURI);
builder.remoteCache("xmlCache").configurationURI(xmlCacheURI);

RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());
RemoteCacheManager cacheManager = TutorialsConnectorHelper.connect(builder);
RemoteCache<String, String> textCache = cacheManager.getCache("textCache");
RemoteCache<String, String> jsonCache = cacheManager.getCache("jsonCache")
.withDataFormat(DataFormat.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void main(String[] args) throws InterruptedException {
// Remove listener
cache.removeClientListener(listener);
// Stop the cache manager and release all resources
cacheManager.stop();
TutorialsConnectorHelper.stop(cacheManager);
}

@ClientListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void main(String[] args) throws Exception {
}).join();

// Stop the cache manager and release all resources
cacheManager.stop();
TutorialsConnectorHelper.stop(cacheManager);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void main(String[] args) {
.nearCacheUseBloomFilter(true);

// Connect to the server with the near cache configuration for the test cache
RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());
RemoteCacheManager cacheManager = TutorialsConnectorHelper.connect(builder);

RemoteCache<Integer, String> testCache = cacheManager.getCache(TutorialsConnectorHelper.TUTORIAL_CACHE_NAME);
RemoteCache<Integer, String> withNearCaching = cacheManager.getCache(CACHE_WITH_NEAR_CACHING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void main(String[] args) throws Exception {
InfinispanRemotePerCache.class.getClassLoader().getResource("cacheConfig.xml").toURI());

// Connect to the server
try (RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build())) {
try (RemoteCacheManager cacheManager = TutorialsConnectorHelper.connect(builder)) {
// Obtain a remote cache that does not exist.
// Rather than return null, create the cache from a template.
RemoteCache<String, String> cache = cacheManager.getCache("my-cache");
Expand Down

0 comments on commit fea56fd

Please sign in to comment.