io.github.quickmsg.interate.IgniteIntegrate Maven / Gradle / Ivy
package io.github.quickmsg.interate;
import io.github.quickmsg.common.integrate.IgniteCacheRegion;
import io.github.quickmsg.common.integrate.Integrate;
import io.github.quickmsg.common.integrate.SubscribeTopic;
import io.github.quickmsg.common.integrate.cache.ConnectCache;
import io.github.quickmsg.common.integrate.cache.IntegrateCache;
import io.github.quickmsg.common.integrate.channel.IntegrateChannels;
import io.github.quickmsg.common.integrate.cluster.IntegrateCluster;
import io.github.quickmsg.common.integrate.job.JobExecutor;
import io.github.quickmsg.common.integrate.msg.IntegrateMessages;
import io.github.quickmsg.common.integrate.topic.IntegrateTopics;
import io.github.quickmsg.common.protocol.ProtocolAdaptor;
import io.github.quickmsg.common.topic.FixedTopicFilter;
import io.github.quickmsg.common.topic.TreeTopicFilter;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteAtomicLong;
import org.apache.ignite.IgniteSet;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheAtomicityMode;
import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.cache.CacheRebalanceMode;
import org.apache.ignite.cache.CacheWriteSynchronizationMode;
import org.apache.ignite.cluster.ClusterState;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.CollectionConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author luxurong
*/
public class IgniteIntegrate implements Integrate {
private final Ignite ignite;
private final ProtocolAdaptor protocolAdaptor;
private final IgniteChannels igniteChannels;
private final IgniteIntegrateCluster cluster;
private final IgniteIntegrateTopics integrateTopics;
private final IgniteMessages igniteMessages;
private final IgniteExecutor igniteExecutor;
public IgniteIntegrate(IgniteConfiguration configuration, ProtocolAdaptor protocolAdaptor) {
this.ignite = Ignition.start(configuration);
this.ignite.cluster().state(ClusterState.ACTIVE);
this.protocolAdaptor = protocolAdaptor;
this.igniteChannels = new IgniteChannels(this, new ConcurrentHashMap<>());
this.cluster = new IgniteIntegrateCluster(this);
this.integrateTopics = new IgniteIntegrateTopics(this);
this.igniteMessages = new IgniteMessages(new FixedTopicFilter<>(), new TreeTopicFilter<>(), this);
this.igniteExecutor = new IgniteExecutor(ignite.compute(ignite.cluster()));
}
@Override
public IntegrateChannels getChannels() {
return this.igniteChannels;
}
@Override
public IntegrateCluster getCluster() {
return this.cluster;
}
@Override
public IntegrateCache getCache(String cacheName) {
CacheConfiguration configuration =
new CacheConfiguration()
.setName(cacheName);
return new IgniteIntegrateCache<>(ignite.getOrCreateCache(configuration));
}
@Override
public IntegrateCache getLocalCache(String cacheName) {
return getLocalCache(cacheName, false);
}
@Override
public IntegrateCache getLocalCache(String cacheName, boolean local) {
CacheMode cacheMode = local ? CacheMode.LOCAL : CacheMode.PARTITIONED;
CacheConfiguration configuration =
new CacheConfiguration()
.setName(cacheName).setCacheMode(cacheMode);
return new IgniteIntegrateCache<>(ignite.getOrCreateCache(configuration));
}
@Override
public IntegrateCache getCache(IgniteCacheRegion igniteCacheRegion) {
CacheMode cacheMode = igniteCacheRegion.local() ? CacheMode.LOCAL : CacheMode.PARTITIONED;
CacheConfiguration configuration =
new CacheConfiguration()
.setName(igniteCacheRegion.getCacheName())
.setCacheMode(cacheMode)
.setDataRegionName(igniteCacheRegion.getRegionName())
.setCacheMode(igniteCacheRegion.getCacheMode())
.setAtomicityMode(CacheAtomicityMode.ATOMIC)
.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC)
.setBackups(1)
.setIndexedTypes(Integer.class, ConnectCache.class)
.setRebalanceMode(CacheRebalanceMode.ASYNC);
return new IgniteIntegrateCache<>(ignite.getOrCreateCache(configuration));
}
@Override
public IntegrateTopics getTopics() {
return this.integrateTopics;
}
@Override
public IntegrateMessages getMessages() {
return this.igniteMessages;
}
@Override
public JobExecutor getJobExecutor() {
return this.igniteExecutor;
}
@Override
public ProtocolAdaptor getProtocolAdaptor() {
return this.protocolAdaptor;
}
@Override
public Ignite getIgnite() {
return this.ignite;
}
@Override
public IgniteAtomicLong getGlobalCounter(String name) {
return ignite.atomicLong(name, 0, true);
}
}