Java Code Examples for org.infinispan.client.hotrod.RemoteCacheManager#stop()
The following examples show how to use
org.infinispan.client.hotrod.RemoteCacheManager#stop() .
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: InfinispanScripting.java From infinispan-simple-tutorials with Apache License 2.0 | 6 votes |
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); // Connect to the server RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build()); // Retrieve the cache containing the scripts RemoteCache<String, String> scriptCache = cacheManager.getCache("___script_cache"); // Create a simple script which multiplies to numbers scriptCache.put("simple.js", "multiplicand * multiplier"); // Obtain the remote cache RemoteCache<String, Integer> cache = cacheManager.administration().getOrCreateCache("test", DefaultTemplate.DIST_SYNC); // Create the parameters for script execution Map<String, Object> params = new HashMap<>(); params.put("multiplicand", 10); params.put("multiplier", 20); // Run the script on the server, passing in the parameters Object result = cache.execute("simple.js", params); // Print the result System.out.printf("Result = %s\n", result); // Stop the cache manager and release resources cacheManager.stop(); }
Example 2
Source File: InfinispanRemote.java From infinispan-simple-tutorials with Apache License 2.0 | 6 votes |
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: InfinispanNearCache.java From infinispan-simple-tutorials with Apache License 2.0 | 6 votes |
public static void main(String[] args) { // Create a client configuration connecting to a local server ConfigurationBuilder builder = new ConfigurationBuilder(); builder.addServer().host("127.0.0.1").port(ConfigurationProperties.DEFAULT_HOTROD_PORT); builder.nearCache().mode(NearCacheMode.INVALIDATED).maxEntries(20).cacheNamePattern("near-.*"); // Connect to the server RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build()); // Create one remote cache with near caching disabled and one with near caching enabled RemoteCache<Integer, String> numbers = cacheManager.administration().getOrCreateCache("numbers", DefaultTemplate.DIST_SYNC); RemoteCache<Integer, String> nearNumbers = cacheManager.administration().getOrCreateCache("near-numbers", DefaultTemplate.DIST_SYNC); for (int i = 1; i<= 20; i++) { numbers.put(i, String.valueOf(i)); nearNumbers.put(i, String.valueOf(i)); } // Read both caches data readCache(numbers); readCache(nearNumbers); // Stop the cache manager and release all resources cacheManager.stop(); }
Example 4
Source File: InfinispanRemoteListen.java From infinispan-simple-tutorials with Apache License 2.0 | 5 votes |
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 5
Source File: InfinispanRemoteSecured.java From infinispan-simple-tutorials with Apache License 2.0 | 5 votes |
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 6
Source File: InfinispanRemoteMultimap.java From infinispan-simple-tutorials with Apache License 2.0 | 5 votes |
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 7
Source File: RemoteCacheProvider.java From keycloak with Apache License 2.0 | 5 votes |
public void stop() { logger.debugf("Shutdown %d registered secured remoteCache managers", managedManagers.size()); for (RemoteCacheManager mgr : managedManagers.values()) { mgr.stop(); } }
Example 8
Source File: InfinispanRemoteTx.java From infinispan-simple-tutorials with Apache License 2.0 | 4 votes |
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"); // Configure the RemoteCacheManager to use a transactional cache as default // Use the simple TransactionManager in hot rod client builder.transaction().transactionManagerLookup(RemoteTransactionManagerLookup.getInstance()); // The cache will be enlisted as Synchronization builder.transaction().transactionMode(TransactionMode.NON_XA); // Connect to the server RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build()); // Create a transactional cache in the server since there is none available by default. cacheManager.administration().createCache(CACHE_NAME, new XMLStringConfiguration(TEST_CACHE_XML_CONFIG)); RemoteCache<String, String> cache = cacheManager.getCache(CACHE_NAME); // Obtain the transaction manager TransactionManager transactionManager = cache.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 9
Source File: InfinispanRemoteCounter.java From infinispan-simple-tutorials with Apache License 2.0 | 4 votes |
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 cacheManager = new RemoteCacheManager(builder.build()); // Retrieve the CounterManager from the CacheManager. Each CacheManager has it own CounterManager CounterManager counterManager = RemoteCounterManagerFactory.asCounterManager(cacheManager); // Create 3 counters. // The first counter is bounded to 10 (upper-bound). counterManager.defineCounter("counter-1", CounterConfiguration.builder(CounterType.BOUNDED_STRONG) .upperBound(10) .initialValue(1) .build()); // The second counter is unbounded counterManager.defineCounter("counter-2", CounterConfiguration.builder(CounterType.UNBOUNDED_STRONG) .initialValue(2) .build()); // And finally, the third counter is a weak counter. counterManager.defineCounter("counter-3", CounterConfiguration.builder(CounterType.WEAK) .initialValue(3) .build()); // StrongCounter provides the higher consistency. Its value is known during the increment/decrement and it may be bounded. // Bounded counters are aimed for uses cases where a limit is needed. StrongCounter counter1 = counterManager.getStrongCounter("counter-1"); // All methods returns a CompletableFuture. So you can do other work while the counter value is being computed. counter1.getValue().thenAccept(value -> System.out.println("Counter-1 initial value is " + value)).get(); // Try to add more than the upper-bound counter1.addAndGet(10).handle((value, throwable) -> { // Value is null since the counter is bounded and we can add 10 to it. System.out.println("Counter-1 Exception is " + throwable.getMessage()); return 0; }).get(); // Check the counter value. It should be the upper-bound (10) counter1.getValue().thenAccept(value -> System.out.println("Counter-1 value is " + value)).get(); //Decrement the value. Should be 9. counter1.decrementAndGet().handle((value, throwable) -> { // No exception this time. System.out.println("Counter-1 new value is " + value); return value; }).get(); // Similar to counter-1, counter-2 is a strong counter but it is unbounded. It will never throw the CounterOutOfBoundsException StrongCounter counter2 = counterManager.getStrongCounter("counter-2"); // All counters allow a listener to be registered. // The handle can be used to remove the listener counter2.addListener(event -> System.out .println("Counter-2 event: oldValue=" + event.getOldValue() + " newValue=" + event.getNewValue())); // Adding MAX_VALUE won't throws an exception. But the all the increments won't have any effect since we can store //any value larger the MAX_VALUE counter2.addAndGet(Long.MAX_VALUE).thenAccept(aLong -> System.out.println("Counter-2 value is " + aLong)).get(); // Conditional operations are allowed in strong counters counter2.compareAndSet(Long.MAX_VALUE, 0) .thenAccept(aBoolean -> System.out.println("Counter-2 CAS result is " + aBoolean)).get(); counter2.getValue().thenAccept(value -> System.out.println("Counter-2 value is " + value)).get(); // Reset the counter to its initial value (2) counter2.reset().get(); counter2.getValue().thenAccept(value -> System.out.println("Counter-2 initial value is " + value)).get(); // Retrieve counter-3 WeakCounter counter3 = counterManager.getWeakCounter("counter-3"); // Weak counter doesn't have its value available during updates. This makes the increment faster than the StrongCounter // Its value is computed lazily and stored locally. // Its main use case is for uses-case where faster increments are needed. counter3.add(5).thenAccept(aVoid -> System.out.println("Adding 5 to counter-3 completed!")).get(); // Check the counter value. System.out.println("Counter-3 value is " + counter3.getValue()); // Stop the cache manager and release all resources cacheManager.stop(); }
Example 10
Source File: InfinispanRemoteQuery.java From infinispan-simple-tutorials with Apache License 2.0 | 4 votes |
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 11
Source File: InfinispanRemoteContinuousQuery.java From infinispan-simple-tutorials with Apache License 2.0 | 4 votes |
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(); }