You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Abstract

Micronaut is a microservice microservice framework. This develops a module to configure and setup ignite to work with Micronaut and it's associated API's.

AutoConfiguration

IgniteClientConfiguration

Configuration to load Ignite instance.

IgniteClientConfiguration
/**
 * Ignite cache configuration.
 */
@EachProperty(value = IgniteClientConfiguration.PREFIX + ".clients", primary = "default")
public class IgniteClientConfiguration implements Named {
    public static final String PREFIX = "ignite";

    private final String name;
    private String path;

    /**
     * @param name Name or key of the client.
     */
    public IgniteClientConfiguration(@Parameter String name) {
        this.name = name;
    }

    /**
     * path to load in bean configuration.
     *
     * @param path bean config to load.
     */
    public void setPath(String path) {
        this.path = path;
    }

    /**
     * path to load in bean configuration.
     *
     * @return bean config to load.
     */
    public String getPath() {
        return path;
    }

    /**
     * @return name or key for client
     */
    @NonNull
    @Override
    public String getName() {
        return this.name;
    }
}

Application yaml configuration.

example config
ignite: 
	enabled: true
	clients:
		default:
			path: "classpath:a.cfg"         
		other:            
			path: "classpath:b.cfg"

  • default is the primary bean
  • path loads in a spring bean definition

Application factory configuration

A bean for the IgniteConfiguration can be provided from a factory.

Example:


@Factory
public class IgniteInitial {
    @Singleton
    @Bean
    @Primary
    @Named("default")
    IgniteConfiguration defaultConfiguration() {
        IgniteConfiguration cfg = new IgniteConfiguration();

        // The node will be started as a client node.
        cfg.setClientMode(true);
        // Setting up an IP Finder to ensure the client can locate the servers.
        TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
        ipFinder.setAddresses(Collections.singletonList("localhost:47500..47509"));
        cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));
        return cfg;
    }
}

Micronaut Caching API

Micronaut cache configuration is covered under snapshot documentation. By default the primary Ignite instance is used for Micronaut cache and the key from for each cache operation will use name of the IgniteCache instance.

IgniteCacheManager
@Singleton
public class IgniteCacheManager implements DynamicCacheManager<IgniteCache> {
    private final Ignite ignite;
    private final ConversionService<?> service;
    private final ExecutorService executorService;

    public IgniteCacheManager(@Primary Ignite ignite,
                              ConversionService<?> service,
                              @Named(TaskExecutors.IO) ExecutorService executorService) {
        this.ignite = ignite;
        this.service = service;
        this.executorService = executorService;
    }

    @NonNull
    @Override
    public SyncCache<IgniteCache> getCache(String name) {
        return new IgniteSyncCache(service, ignite.getOrCreateCache(name), executorService);
    }
}

Example

This is an example of a service using Micronaut Cache.

Example Cacheable Object
  	@Singleton
    @CacheConfig('counter')
    static class CounterService {
        Map<String, Integer> counters = new LinkedHashMap<>()
        Map<String, Integer> counters2 = new LinkedHashMap<>()

        int incrementNoCache(String name) {
            int value = counters.computeIfAbsent(name, { 0 })
            counters.put(name, ++value)
            return value
        }

        @CachePut
        int increment(String name) {
            int value = counters.computeIfAbsent(name, { 0 })
            counters.put(name, ++value)
            return value
        }

        @PutOperations([
                @CachePut('counter'),
                @CachePut('counter2')

        ])
        int increment2(String name) {
            int value = counters2.computeIfAbsent(name, { 0 })
            counters2.put(name, ++value)
            return value
        }

        @Cacheable
        CompletableFuture<Integer> futureValue(String name) {
            return CompletableFuture.completedFuture(counters.computeIfAbsent(name, { 0 }))
        }

        @Cacheable
        @SingleResult
        Flowable<Integer> flowableValue(String name) {
            return Flowable.just(counters.computeIfAbsent(name, { 0 }))
        }

        @Cacheable
        Single<Integer> singleValue(String name) {
            return Single.just(counters.computeIfAbsent(name, { 0 }))
        }

        @CachePut
        CompletableFuture<Integer> futureIncrement(String name) {
            int value = counters.computeIfAbsent(name, { 0 })
            counters.put(name, ++value)
            return CompletableFuture.completedFuture(value)
        }

        @Cacheable
        int getValue(String name) {
            return counters.computeIfAbsent(name, { 0 })
        }

        @Cacheable('counter2')
        int getValue2(String name) {
            return counters2.computeIfAbsent(name, { 0 })
        }

        @Cacheable
        Optional<Integer> getOptionalValue(String name) {
            return Optional.ofNullable(counters.get(name))
        }

        @CacheInvalidate(all = true)
        void reset() {
            counters.clear()
        }

        @CacheInvalidate
        void reset(String name) {
            counters.remove(name)
        }

        @InvalidateOperations([
                @CacheInvalidate('counter'),
                @CacheInvalidate('counter2')
        ])
        void reset2(String name) {
            counters.remove(name)
        }

        @CacheInvalidate(parameters = 'name')
        void set(String name, int val) {
            counters.put(name, val)
        }
    }




  • No labels