How to view and edit Infinispan cached data remotely

北慕城南 提交于 2019-12-05 06:58:33

First of all, my condolences on having to choose the road less traveled.

It's possible to access an embedded Infinispan cache remotely. You need to set up a org.infinispan.server.hotrod.HotRodServer in your server process, essentially reverse engineering the pre-packaged Infinispan Server distribution. This approach is not documented, so proceed at your own risk.

You need these dependencies:

<dependency>
    <groupId>org.infinispan</groupId>
    <artifactId>infinispan-server-hotrod</artifactId>
    <version>7.1.0.Final</version>
</dependency>
<dependency>
    <groupId>org.infinispan</groupId>
    <artifactId>infinispan-client-hotrod</artifactId>
    <version>7.1.0.Final</version>
</dependency>
<dependency>
    <groupId>org.infinispan</groupId>
    <artifactId>infinispan-remote-query-server</artifactId>
    <version>7.1.0.Final</version>
</dependency>

Configure an example cache (infinispan.xml):

<infinispan>
    <cache-container default-cache="default">
        <local-cache name="dumpster">
            <compatibility />
        </local-cache>
    </cache-container>
</infinispan>

The server process:

// Start a cache manager as usual
EmbeddedCacheManager cacheManager;
try (InputStream in = ClassLoader.getSystemResourceAsStream("infinispan.xml")) {
    cacheManager = new DefaultCacheManager(in);
}

// Start a server to allow remote access to the cache manager
HotRodServerConfiguration serverConfig = new HotRodServerConfigurationBuilder()
        .host("127.0.0.1").port(9999).build();
HotRodServer server = new HotRodServer();
server.start(serverConfig, cacheManager);

// Start the example cache
Cache<String, String> cache = cacheManager.getCache("dumpster", true);
cache.put("K", "V");
System.out.println(cache.get("K")); // V

The client process:

Configuration config = new ConfigurationBuilder().addServer()
        .host("127.0.0.1").port(9999).build();
RemoteCacheManager cacheManager = new RemoteCacheManager(config);
RemoteCache<String, String> cache = cacheManager.getCache("dumpster");
System.out.println(cache.get("K")); // V
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!