org.infinispan.commons.api.CacheContainerAdmin Java Examples

The following examples show how to use org.infinispan.commons.api.CacheContainerAdmin. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InfinispanReplicated.java    From infinispan-simple-tutorials with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
   // Setup up a clustered cache manager
   GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder();
   // Initialize the cache manager
   DefaultCacheManager cacheManager = new DefaultCacheManager(global.build());
   // Create a replicated synchronous configuration
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.clustering().cacheMode(CacheMode.REPL_SYNC);
   Configuration cacheConfig = builder.build();
   // Create a cache
   Cache<String, String> cache = cacheManager.administration()
           .withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
           .getOrCreateCache("cache", cacheConfig);

   // Store the current node address in some random keys
   for(int i=0; i < 10; i++) {
      cache.put(UUID.randomUUID().toString(), cacheManager.getNodeAddress());
   }
   // Display the current cache contents for the whole cluster
   cache.entrySet().forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue()));
   // Display the current cache contents for this node
   cache.getAdvancedCache().withFlags(Flag.SKIP_REMOTE_LOOKUP)
      .entrySet().forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue()));
   // Stop the cache manager and release all resources
   cacheManager.stop();
}
 
Example #2
Source File: InfinispanRemote.java    From infinispan-simple-tutorials with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
   // Create a configuration for a locally-running server
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.addServer()
            .host("127.0.0.1")
            .port(ConfigurationProperties.DEFAULT_HOTROD_PORT)
          .security().authentication()
            //Add user credentials.
            .username("username")
            .password("password")
            .realm("default")
            .saslMechanism("DIGEST-MD5");
   // Connect to the server
   RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());
   // Create test cache, if such does not exist
   cacheManager.administration().withFlags(CacheContainerAdmin.AdminFlag.VOLATILE).getOrCreateCache("test", DefaultTemplate.DIST_SYNC);
   // Obtain the remote cache
   RemoteCache<String, String> cache = cacheManager.getCache("test");
   /// Store a value
   cache.put("key", "value");
   // 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();
}
 
Example #3
Source File: InfinispanDistributed.java    From infinispan-simple-tutorials with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
   // Setup up a clustered cache manager
   GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder();
   // Initialize the cache manager
   DefaultCacheManager cacheManager = new DefaultCacheManager(global.build());
   //Create cache configuration
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.clustering().cacheMode(CacheMode.DIST_SYNC);
   // Obtain a cache
   Cache<String, String> cache = cacheManager.administration().withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
         .getOrCreateCache("cache", builder.build());

   // Store the current node address in some random keys
   for (int i = 0; i < 10; i++) {
      cache.put(UUID.randomUUID().toString(), cacheManager.getNodeAddress());
   }
   // Display the current cache contents for the whole cluster
   cache.entrySet().forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue()));
   // Display the current cache contents for this node
   cache.getAdvancedCache().withFlags(Flag.SKIP_REMOTE_LOOKUP).entrySet()
         .forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue()));
   // Stop the cache manager and release all resources
   cacheManager.stop();
}
 
Example #4
Source File: InfinispanClusterManager.java    From vertx-infinispan with Apache License 2.0 5 votes vote down vote up
@Override
public <K, V> void getAsyncMap(String name, Promise<AsyncMap<K, V>> promise) {
  vertx.executeBlocking(prom -> {
    EmbeddedCacheManagerAdmin administration = cacheManager.administration().withFlags(CacheContainerAdmin.AdminFlag.VOLATILE);
    Cache<byte[], byte[]> cache = administration.getOrCreateCache(name, "__vertx.distributed.cache.configuration");
    prom.complete(new InfinispanAsyncMapImpl<>(vertx, cache));
  }, false, promise);
}
 
Example #5
Source File: InfinispanTx.java    From infinispan-simple-tutorials with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
   // Construct a local cache manager
   DefaultCacheManager cacheManager = new DefaultCacheManager();
   // Create a transaction cache config
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.transaction().transactionMode(TransactionMode.TRANSACTIONAL);
   Configuration cacheConfig = builder.build();
   // Create a cache with the config
   Cache<String, String> cache = cacheManager.administration()
           .withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
           .getOrCreateCache("cache", cacheConfig);
   // Obtain the transaction manager
   TransactionManager transactionManager = cache.getAdvancedCache().getTransactionManager();
   // Perform some operations within a transaction and commit it
   transactionManager.begin();
   cache.put("key1", "value1");
   cache.put("key2", "value2");
   transactionManager.commit();
   // Display the current cache contents
   System.out.printf("key1 = %s\nkey2 = %s\n", cache.get("key1"), cache.get("key2"));
   // Perform some operations within a transaction and roll it back
   transactionManager.begin();
   cache.put("key1", "value3");
   cache.put("key2", "value4");
   transactionManager.rollback();
   // Display the current cache contents
   System.out.printf("key1 = %s\nkey2 = %s\n", cache.get("key1"), cache.get("key2"));
   // Stop the cache manager and release all resources
   cacheManager.stop();
}
 
Example #6
Source File: InfinispanServerTasks.java    From infinispan-simple-tutorials with Apache License 2.0 5 votes vote down vote up
private static RemoteCache<String,String> getExecCache() {
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.addServer().host("127.0.0.1").port(11222);

   RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());
   return cacheManager.administration()
           .withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
           .getOrCreateCache("data", DefaultTemplate.DIST_SYNC);
}
 
Example #7
Source File: InfinispanQuery.java    From infinispan-simple-tutorials with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
   // Create cache manager
   DefaultCacheManager cacheManager = new DefaultCacheManager();

   // Create cache config
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.indexing().index(Index.ALL)
           .addProperty("default.directory_provider", "ram")
           .addProperty("lucene_version", "LUCENE_CURRENT");

   // Obtain the cache
   Cache<String, Person> cache = cacheManager.administration()
           .withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
           .getOrCreateCache("cache", builder.build());

   // Store some entries
   cache.put("person1", new Person("William", "Shakespeare"));
   cache.put("person2", new Person("William", "Wordsworth"));
   cache.put("person3", new Person("John", "Milton"));
   // Obtain a query factory for the cache
   QueryFactory queryFactory = Search.getQueryFactory(cache);
   // Construct a query
   Query query = queryFactory.from(Person.class).having("name").eq("William").toBuilder().build();
   // Execute the query
   List<Person> matches = query.list();
   // List the results
   matches.forEach(person -> System.out.printf("Match: %s", person));
   // Stop the cache manager and release all resources
   cacheManager.stop();
}
 
Example #8
Source File: SpringCaching.java    From infinispan-simple-tutorials with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);
    // Create a cache programmatically for the example. There is no default cache
    EmbeddedCacheManager cacheManager = applicationContext.getBean(SpringEmbeddedCacheManager.class).getNativeCacheManager();
    cacheManager.administration()
          .withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
          .getOrCreateCache("default", new ConfigurationBuilder().build());

    CachedObject cachePlayground = applicationContext.getBean(CachedObject.class);

    printWithTime(() -> cachePlayground.verySlowMethod());
    printWithTime(() -> cachePlayground.verySlowMethod());
}
 
Example #9
Source File: SpringAnnotationConfiguration.java    From infinispan-simple-tutorials with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
    // Create a cache programmatically for the example. There is no default cache
    EmbeddedCacheManager cacheManager = applicationContext.getBean(SpringEmbeddedCacheManager.class).getNativeCacheManager();
    cacheManager.administration()
          .withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
          .getOrCreateCache("default", new ConfigurationBuilder().build());

    CachePlayground cachePlayground = applicationContext.getBean(CachePlayground.class);

    cachePlayground.add("Infinispan", "Is cool!");
    System.out.println(cachePlayground.getContent("Infinispan"));
}
 
Example #10
Source File: InfinispanRemoteListen.java    From infinispan-simple-tutorials with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
   // Create a configuration for a locally-running server
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.addServer()
            .host("127.0.0.1")
            .port(ConfigurationProperties.DEFAULT_HOTROD_PORT)
          .security().authentication()
            //Add user credentials.
            .username("username")
            .password("password")
            .realm("default")
            .saslMechanism("DIGEST-MD5");
   // Connect to the server
   RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());
   // Get the cache, create it if needed with an existing template name
   RemoteCache<String, String> cache = cacheManager.administration()
           .withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
           .getOrCreateCache("listen", DefaultTemplate.DIST_SYNC);
   // Register a listener
   MyListener listener = new MyListener();
   cache.addClientListener(listener);
   // Store some values
   cache.put("key1", "value1");
   cache.put("key2", "value2");
   cache.put("key1", "newValue");
   // Remote events are asynchronous, so wait a bit
   Thread.sleep(1000);
   // Remove listener
   cache.removeClientListener(listener);
   // Stop the cache manager and release all resources
   cacheManager.stop();
}
 
Example #11
Source File: InfinispanRemoteSecured.java    From infinispan-simple-tutorials with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
   // Create a configuration for a locally-running server
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.addServer().host("127.0.0.1").port(ConfigurationProperties.DEFAULT_HOTROD_PORT);
   // Workaround for docker 4 mac
   builder.clientIntelligence(ClientIntelligence.BASIC);

   //Configure the security properties
   builder.security().authentication()
           .username("Titus Bramble")
           .password("Shambles")
           .saslMechanism("DIGEST-MD5")
           .realm("default")
           .serverName("infinispan");

   // Connect to the server
   RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());
   // Create test cache, if such does not exist
   cacheManager.administration().withFlags(CacheContainerAdmin.AdminFlag.VOLATILE).getOrCreateCache("test",
         DefaultTemplate.DIST_SYNC);
   // Obtain the remote cache
   RemoteCache<String, String> cache = cacheManager.getCache("test");
   /// Store a value
   cache.put("key", "value");
   // 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();
}
 
Example #12
Source File: InfinispanRemoteMultimap.java    From infinispan-simple-tutorials with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
   // Create a configuration for a locally-running server
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.addServer()
            .host("127.0.0.1")
            .port(ConfigurationProperties.DEFAULT_HOTROD_PORT)
          .security().authentication()
            //Add user credentials.
            .username("username")
            .password("password")
            .realm("default")
            .saslMechanism("DIGEST-MD5");
   // Connect to the server and create a cache
   RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());
   // Create people cache if needed with an existing template name
   cacheManager.administration()
           .withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
           .getOrCreateCache(PEOPLE_MULTIMAP, DefaultTemplate.DIST_SYNC);

   // Retrieve the MultimapCacheManager from the CacheManager.
   MultimapCacheManager multimapCacheManager = RemoteMultimapCacheManagerFactory.from(cacheManager);

   // Retrieve the multimap cache.
   RemoteMultimapCache<Integer, String> people = multimapCacheManager.get(PEOPLE_MULTIMAP);
   people.put(2016, "Alberto");
   people.put(2016, "Oihana");
   people.put(2016, "Roman");
   people.put(2016, "Ane");
   people.put(2017, "Paula");
   people.put(2017, "Aimar");
   people.put(2018, "Elaia");

   people.get(2016).whenComplete((v, ex) -> {
      System.out.println(v);
   }).join();

   // Stop the cache manager and release all resources
   cacheManager.stop();
}
 
Example #13
Source File: InfinispanCacheMetricBinderTest.java    From infinispan-spring-boot with Apache License 2.0 4 votes vote down vote up
@Override
public CacheMeterBinder binder() {
   cacheManager = new DefaultCacheManager();
   cache = cacheManager.administration().withFlags(CacheContainerAdmin.AdminFlag.VOLATILE).getOrCreateCache("mycache", new ConfigurationBuilder().jmxStatistics().enable().build());
   return new InfinispanCacheMeterBinder(cache, emptyList());
}
 
Example #14
Source File: InfinispanRemoteQuery.java    From infinispan-simple-tutorials with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
   // Create a configuration for a locally-running server
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.addServer()
            .host("127.0.0.1")
            .port(ConfigurationProperties.DEFAULT_HOTROD_PORT)
          .security().authentication()
            //Add user credentials.
            .username("username")
            .password("password")
            .realm("default")
            .saslMechanism("DIGEST-MD5");

   // Connect to the server
   RemoteCacheManager client = new RemoteCacheManager(builder.build());

   // Get the people cache, create it if needed with the default configuration
   RemoteCache<String, Person> peopleCache = client.administration()
           .withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
           .getOrCreateCache("people-remote-query", DefaultTemplate.DIST_SYNC);

   // Create the persons dataset to be stored in the cache
   Map<String, Person> people = new HashMap<>();
   people.put("1", new Person("Oihana", "Rossignol", 2016, "Paris"));
   people.put("2", new Person("Elaia", "Rossignol", 2018, "Paris"));
   people.put("3", new Person("Yago", "Steiner", 2013, "Saint-Mandé"));
   people.put("4", new Person("Alberto", "Steiner", 2016, "Paris"));

   // Create and add the Protobuf schema for Person class. Note Person is an annotated POJO
   addPersonSchema(client);

   // Put all the values in the cache
   peopleCache.putAll(people);

   // Get a query factory from the cache
   QueryFactory queryFactory = Search.getQueryFactory(peopleCache);

   // Create a query with lastName parameter
   Query query = queryFactory.create("FROM tutorial.Person p where p.lastName = :lastName");

   // Set the parameter value
   query.setParameter("lastName", "Rossignol");

   // Execute the query
   List<Person> rossignols = query.list();

   // Print the results
   System.out.println(rossignols);

   // Stop the client and release all resources
   client.stop();
}
 
Example #15
Source File: InfinispanRemoteContinuousQuery.java    From infinispan-simple-tutorials with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
   // Create a configuration for a locally-running server
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.addServer()
            .host("127.0.0.1")
            .port(ConfigurationProperties.DEFAULT_HOTROD_PORT)
          .security().authentication()
            //Add user credentials.
            .username("username")
            .password("password")
            .realm("default")
            .saslMechanism("DIGEST-MD5");

   // Connect to the server
   RemoteCacheManager client = new RemoteCacheManager(builder.build());

   // Get the cache, create it if needed with an existing template name
   RemoteCache<String, InstaPost> instaPostsCache = client.administration().withFlags(CacheContainerAdmin.AdminFlag.VOLATILE).getOrCreateCache(CACHE_NAME, DefaultTemplate.DIST_SYNC);

   // Create and add the Protobuf schema for InstaPost class. Note InstaPost is an annotated POJO
   addInstapostsSchema(client);

   // Get a query factory from the cache
   QueryFactory queryFactory = Search.getQueryFactory(instaPostsCache);

   // Create a query with lastName parameter
   Query query = queryFactory.create("FROM tutorial.InstaPost p where p.user = :userName");

   // Set the parameter value
   query.setParameter("userName", "belen_esteban");

   // Create the continuous query
   ContinuousQuery<String, InstaPost> continuousQuery = Search.getContinuousQuery(instaPostsCache);

   // Create the continuous query listener.
   List<InstaPost> queryPosts = new ArrayList<>();
   ContinuousQueryListener<String, InstaPost> listener =
         new ContinuousQueryListener<String, InstaPost>() {
            // 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));
               queryPosts.add(post);
            }
         };

   // And the listener corresponding the query to the continuous query
   continuousQuery.addContinuousQueryListener(query, listener);

   // Add 1000 random posts
   for (int i = 0; i < 1000; i++) {
      // Add a post
      addRandomPost(instaPostsCache);

      // Await a little to see results
      Thread.sleep(10);
   }

   System.out.println("Total posts " + instaPostsCache.size());
   System.out.println("Total posts by @belen_esteban " + queryPosts.size());

   // Remove the listener. Listeners should be removed when they are no longer needed to avoid memory leaks
   continuousQuery.removeContinuousQueryListener(listener);

   // Remove the cache
   client.administration().removeCache(CACHE_NAME);

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