infinispan项目中的配置

2022-04-14 00:00:00 专区 订阅 代码 付费 交易

java:

import java.io.IOException;

import java.util.Map;

public interface CacheManager   {
    static final String lbsNameCache= "lbsNameCache";

    Object put(Object key, Object value);
    
    Object get(Object key);
    
    void remove(Object key);
    
    void putAll(Map<Object, Object> map);
    
    
    Object put(String namedCache,Object key, Object value);

    
    Object get(String namedCache,Object key);
    
    void remove(String namedCache,Object key);
    
    void putAll(String namedCache,Map<Object, Object> map);
    
    void init()  throws IOException;
    
    
    void destroy();
}

java:

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.infinispan.Cache;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class InfinispanCacheManager implements CacheManager {
    private Logger logger = LoggerFactory
            .getLogger(InfinispanCacheManager.class);

    private EmbeddedCacheManager embeddedCacheManager;

    private Map<String, Cache<Object, Object>> caches = new HashMap<String, Cache<Object, Object>>();

    private String configurationFile;

    public String getConfigurationFile() {
        return configurationFile;
    }

    public void setConfigurationFile(String configurationFile) {
        this.configurationFile = configurationFile;
    }

    public void setEmbeddedCacheManager(
            EmbeddedCacheManager embeddedCacheManager) {
        this.embeddedCacheManager = embeddedCacheManager;
    }

    public InfinispanCacheManager() {
    }

    public void init() throws IOException {
        if (embeddedCacheManager == null) {
            embeddedCacheManager = new DefaultCacheManager(configurationFile);
        }
        Set<String> nameCaches = embeddedCacheManager.getCacheNames();
        Iterator<String> iterator = nameCaches.iterator();
        while (iterator.hasNext()) {
            String nameCahe = iterator.next();
            caches.put(nameCahe, embeddedCacheManager.getCache(nameCahe));
        }
        if (logger.isInfoEnabled()) {
            logger.info("cache start");
        }
    }

    public void destroy() {
        Set<String> nameCaches = caches.keySet();
        Iterator<String> iterator = nameCaches.iterator();
        while (iterator.hasNext()) {
            String nameCahe = iterator.next();
            Cache<Object, Object> cache = embeddedCacheManager
                    .getCache(nameCahe);
            if (cache != null) {
                cache.stop();
            }
        }
        if (logger.isInfoEnabled()) {
            logger.info("cache stop");
        }
        embeddedCacheManager.stop();
    }

    public Cache<Object, Object> getCache() {
        return caches.get(EmbeddedCacheManager.DEFAULT_CACHE_NAME);
    }

    private Cache<Object, Object> getNameCache(String cacheName) {
        return caches.get(cacheName);
    }

    @Override
    public Object put(Object key, Object value) {
        return getCache().put(key, value);
    }

    @Override
    public void putAll(Map<Object, Object> map) {
        getCache().putAll(map);
    }

    @Override
    public Object get(Object key) {
        return getCache().get(key);
    }

    @Override
    public Object get(String namedCache, Object key) {
        Cache<Object, Object> cache = getNameCache(namedCache);
        if (cache == null) {
            return null;
        }
        if (logger.isInfoEnabled()) {
            logger.info("get key"+key);
        }
        return cache.get(key);
    }

    @Override
    public Object put(String namedCache, Object key, Object value) {
        Cache<Object, Object> cache = getNameCache(namedCache);
        if (cache == null) {
            return null;
        }
        if (logger.isInfoEnabled()) {
            logger.info("put key"+key);
        }
        return cache.put(key, value);
    }

    @Override
    public void putAll(String namedCache, Map<Object, Object> map) {
        Cache<Object, Object> cache = getNameCache(namedCache);
        if (cache == null) {
            return;
        }
        cache.putAll(map);
    }

    @Override
    public void remove(Object key) {
        getCache().remove(key);
    }

    @Override
    public void remove(String namedCache, Object key) {
        Cache<Object, Object> cache = getNameCache(namedCache);
        if (cache == null) {
            return;
        }
        if (logger.isInfoEnabled()) {
            logger.info("remove key"+key);
        }
        cache.remove(key);
    }
}
Xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<infinispan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="urn:infinispan:config:4.2
    http://www.infinispan.org/schemas/infinispan-config-4.2.xsd"
    xmlns="urn:infinispan:config:4.2">
    <global>
        <transport>
            <properties>
                <property name="configurationFile" value="jgroups-udp.xml"/>
            </properties>
        </transport>
    </global>
    <default>
        <clustering mode="replication">
            <stateRetrieval fetchInMemoryState="true" timeout="20000" />
            <async />
        </clustering>
        <locking isolationLevel="READ_COMMITTED" concurrencyLevel="1000"
            lockAcquisitionTimeout="15000" useLockStriping="false" />
        <deadlockDetection enabled="true" spinDuration="1000" />
        <expiration lifespan="86400" maxIdle="43200" />
        <lazyDeserialization enabled="true" />
    </default>
    <namedCache name="lbsNameCache">

    </namedCache>
</infinispan>

xml代码:

<config xmlns="urn:org:jgroups" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="urn:org:jgroups file:schema/JGroups-2.8.xsd">
    <TCP bind_port="7800" loopback="true" port_range="30"
        recv_buf_size="20000000" send_buf_size="640000"
        discard_incompatible_packets="true" max_bundle_size="64000"
        max_bundle_timeout="30" enable_bundling="true" use_send_queues="true"
        sock_conn_timeout="300" enable_diagnostics="false"
        skip_suspected_members="true" thread_pool.enabled="true"
        thread_pool.min_threads="2" thread_pool.max_threads="8"
        thread_pool.keep_alive_time="5000" thread_pool.queue_enabled="false"
        thread_pool.queue_max_size="100" thread_pool.rejection_policy="Run"

        oob_thread_pool.enabled="true" oob_thread_pool.min_threads="2"
        oob_thread_pool.max_threads="8" oob_thread_pool.keep_alive_time="5000"
        oob_thread_pool.queue_enabled="false" oob_thread_pool.queue_max_size="100"
        oob_thread_pool.rejection_policy="Run" />

    <!-- you need add initial hosts list in initial_hosts -->
    <TCPPING timeout="3000" initial_hosts="localhost[7800],localhost[7801]}"
        port_range="5" num_initial_members="1" />
    <!-- use multicast here -->
    <!--<MPING bind_addr="127.0.0.1" break_on_coord_rsp="true" -->
    <!--mcast_addr="230.8.8.8" mcast_port="17890" ip_ttl="2" -->
    <!--num_initial_members="3"/> -->

    <MERGE2 max_interval="30000" min_interval="10000" />
    <FD_SOCK />
    <!-- Note that this is an atypically short timeout and a small number of
        retries configured this way to speed up unit testing, since we know all nodes
        run in the same JVM and hence failure detections will be very quick. -->
    <FD timeout="3000" max_tries="3" />
    <VERIFY_SUSPECT timeout="1500" />
    <pbcast.NAKACK use_mcast_xmit="false" gc_lag="0"
        retransmit_timeout="300,600,1200,2400,4800" discard_delivered_msgs="false" />
    <UNICAST timeout="300,600,1200" />
    <pbcast.STABLE stability_delay="1000" desired_avg_gossip="50000"
        max_bytes="400000" />
    <pbcast.GMS print_local_addr="false" join_timeout="7000"
        view_bundling="true" />
    <FC max_credits="2000000" min_threshold="0.10" />
    <FRAG2 frag_size="60000" />
    <pbcast.STREAMING_STATE_TRANSFER />
    <!-- <pbcast.STATE_TRANSFER/> -->
    <pbcast.FLUSH timeout="0" />
</config>
java代码:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd   
    http://www.springframework.org/schema/jee
    http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <bean
        id="cacheManager" init-method="init" destroy-method="destroy">
        <property name="configurationFile" value="META-INF/cache/infinispan-config.xml" />
    </bean>
</beans>

相关文章