Skip to content

Instantly share code, notes, and snippets.

@bsingr
Last active June 24, 2019 15:21
Show Gist options
  • Save bsingr/a33bc0336522329d4f163efbf0fafe05 to your computer and use it in GitHub Desktop.
Save bsingr/a33bc0336522329d4f163efbf0fafe05 to your computer and use it in GitHub Desktop.
Infinispan Cache JDBC Store Playground

Usage

Prereq

Infinispan JAR you'll need to download yourself"

  1. go to https://infinispan.org/download/"
  2. download infinispan embedded infinispan-9.4.15"
  3. unzip and place the content here in a folder named 'infinispan-9.4.15.Final-all'"

Then run

make deps # fetch necessary JAR files

Run example

make mysql # run mysql server used for cache persistence
# open mysql localhost:3307 user=root pass=root database=infinispan and see how table gets populated

make run # this auto-creates cache entrie

make trigger_hup # this removes some cache entry
<?xml version="1.0" encoding="UTF-8"?>
<infinispan>
<cache-container default-cache="default">
<transport cluster="mycluster"/>
<distributed-cache name="default" mode="SYNC" owners="2">
<string-keyed-jdbc-store
xmlns="urn:infinispan:config:store:jdbc:9.2"
shared="false"
fetch-state="true"
read-only="false"
purge="false"
max-batch-size="1"
preload="false">
<connection-pool
connection-url="jdbc:mysql://localhost:3307/infinispan"
driver="com.mysql.jdbc.Driver" password="root" username="root" />
<string-keyed-table drop-on-exit="true" create-on-start="true" prefix="ISPN_STRING_TABLE">
<id-column name="ID_COLUMN" type="VARCHAR(255)" />
<data-column name="DATA_COLUMN" type="VARBINARY(1000)" />
<timestamp-column name="TIMESTAMP_COLUMN" type="BIGINT" />
</string-keyed-table>
</string-keyed-jdbc-store>
</distributed-cache>
</cache-container>
</infinispan>
// infinispan
import org.infinispan.Cache;
import org.infinispan.configuration.global.*;
import org.infinispan.configuration.cache.*;
import org.infinispan.manager.*;
// os signals
import sun.misc.Signal;
import sun.misc.SignalHandler;
import java.lang.reflect.*;
@SuppressWarnings("deprecation")
public class InfinispanHelloWorld
{
public static void main (String[] args)
{
DebugSignalHandler.listenTo("HUP");
System.out.println("Hello World!");
try {
DefaultCacheManager m = new DefaultCacheManager(System.getProperty("user.dir") + "/config.xml");
Cache<String, String> cache = m.getCache();
System.out.println("---");
System.out.println("get hello1 " + cache.get("hello1"));
System.out.println("get hello2 " + cache.get("hello2"));
System.out.println("get hello3 " + cache.get("hello3"));
System.out.println("get hello4 " + cache.get("hello4"));
System.out.println("put hello1 " + cache.put("hello1", "world"));
System.out.println("put hello2 " + cache.put("hello2", "world"));
System.out.println("put hello3 " + cache.put("hello3", "world"));
System.out.println("put hello4 " + cache.put("hello4", "world"));
System.out.println("rm hello4 " + cache.remove("hello4"));
System.out.println("get hello1 " + cache.get("hello1"));
System.out.println("get hello2 " + cache.get("hello2"));
System.out.println("get hello3 " + cache.get("hello3"));
System.out.println("get hello4 " + cache.get("hello4"));
} catch (Exception e) {
System.out.println("error " + e.getMessage());
}
}
}
@SuppressWarnings("deprecation")
class DebugSignalHandler implements SignalHandler
{
public static void listenTo(String name) {
Signal signal = new Signal(name);
Signal.handle(signal, new DebugSignalHandler());
}
public void handle(Signal signal) {
System.out.println("Signal: " + signal.toString().trim());
try {
DefaultCacheManager m = new DefaultCacheManager(System.getProperty("user.dir") + "/config.xml");
Cache<String, String> cache = m.getCache();
System.out.println("---");
System.out.println("rm hello3 " + cache.remove("hello3"));
System.out.println("get hello1 " + cache.get("hello1"));
System.out.println("get hello2 " + cache.get("hello2"));
System.out.println("get hello3 " + cache.get("hello3"));
System.out.println("get hello4 " + cache.get("hello4"));
} catch (Exception e) {
System.out.println("error " + e.getMessage());
}
}
}
run:
rm -f InfinispanHelloWorld.class
javac -cp ./infinispan-9.4.15.Final-all/infinispan-embedded-9.4.15.Final.jar InfinispanHelloWorld.java
CLASSPATH=.:./lib:./javaee-api-8.0.jar:./mysql-connector-java-5.1.18.jar:./infinispan-9.4.15.Final-all/infinispan-embedded-9.4.15.Final.jar java InfinispanHelloWorld
trigger_hup:
kill -1 `pgrep java`
mysql:
docker run --name infinispan_mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=infinispan -p 3307:3306 mysql:5
mysql_stop:
docker rm -f infinispan_mysql
deps:
ls javaee-api-8.0.jar || wget https://repo1.maven.org/maven2/javax/javaee-api/8.0/javaee-api-8.0.jar
ls mysql-connector-java-5.1.18.jar || wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.18/mysql-connector-java-5.1.18.jar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment