当前位置 博文首页 > 程序员石磊:spring ehcache jms activemq 分布式实现方案

    程序员石磊:spring ehcache jms activemq 分布式实现方案

    作者:[db:作者] 时间:2021-08-08 22:22

    本文章来自国外一个博客,亲测可用,下面粘贴过来的是核心配置。
    访问地址:https://pravinchavan.wordpress.com/2013/01/17/ehcahe-replication-with-jms-in-spring/
    EHCACHE: It is widely used type of cache.

    Replication: If you are using mulitple application server and you need to have same copy of cache at every application server node, then you can use EHCAHE Replication.

    I am going to demostrate how EHCACHE replication achieved with Java Message Service in Spring Web Application.

    We have choice of using Open MQ and Active MQ. These are open source message queues. I am using Active MQ.

    Configuration:
    A)Active MQ Setup

    1. You need to download Active MQ server from this link.

    http://activemq.apache.org/download.html

    I have used -apache-activemq-4.1.0-incubator

    2.Extract it.

    3.go to follwing path-

    —\apache-activemq-4.1.0-incubator\bin

    run activemq.bat
    ActiveMQ’s default port is 61616.

    B)Spring Web App Configuration

    3.Spring-context.xml
    –We have to create bean -cacheManager, and set configuration file location in property.

    <!-- 启用Spring对基于注解的Cache的支持-->
        <cache:annotation-driven/>
        <bean id="cacheManager"
              class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" />
        <bean id="ehcache"
              class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="classpath:ehcache.xml" p:shared="true"/>
    
    1. ehcache.xml- This fiile actual configuration for Ehcache
    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.sf.net/ehcache.xsd">
    
    <diskStore path="java.io.tmpdir" />
    
    <cacheManagerPeerProviderFactory
    class="net.sf.ehcache.distribution.jms.JMSCacheManagerPeerProviderFactory"
    properties="initialContextFactoryName=com.arceler.ehcache.TestActiveMQInitialContextFactory,
    providerURL=tcp://10.35.34.229:61616, replicationTopicConnectionFactoryBindingName=topicConnectionFactory,
    replicationTopicBindingName=ehcache, getQueueConnectionFactoryBindingName=queueConnectionFactory,
    getQueueBindingName=ehcacheGetQueue, topicConnectionFactoryBindingName=topicConnectionFactory,
    topicBindingName=ehcache"
    propertySeparator="," />
    <defaultCache eternal="true" maxElementsInMemory="100"
    overflowToDisk="false" />
    
    <cache name="messageCache" maxElementsInMemory="1000" eternal="false"
    timeToIdleSeconds="1000" timeToLiveSeconds="1000" overflowToDisk="false">
    
    <cacheEventListenerFactory
    class="net.sf.ehcache.distribution.jms.JMSCacheReplicatorFactory"
    properties="replicateAsynchronously=true,
    replicatePuts=true,
    replicateUpdates=true,
    replicateUpdatesViaCopy=true,
    replicateRemovals=true,
    asynchronousReplicationIntervalMillis=1000"
    propertySeparator="," />
    
    <cacheLoaderFactory
    class="net.sf.ehcache.distribution.jms.JMSCacheLoaderFactory"
    properties="initialContextFactoryName=com.arceler.ehcache.TestActiveMQInitialContextFactory,
    providerURL=tcp://10.35.34.229:61616,
    replicationTopicConnectionFactoryBindingName=topicConnectionFactory,
    getQueueConnectionFactoryBindingName=queueConnectionFactory,
    replicationTopicBindingName=ehcache,
    getQueueBindingName=ehcacheGetQueue,
    timeoutMillis=10000" />
    </cache>
    
    </ehcache>

    5.TestActiveMQInitialContextFactory.java
    We have specified

    initialContextFactoryName=com.arceler.ehcache.TestActiveMQInitialContextFactory

    So we have give following file.

    import java.net.URISyntaxException;
    import java.util.Hashtable;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    
    import javax.naming.Context;
    
    import javax.naming.NamingException;
    
    import net.sf.ehcache.distribution.jms.JMSUtil;
    
    import org.apache.activemq.jndi.ActiveMQInitialContextFactory;
    
    public class TestActiveMQInitialContextFactory extends ActiveMQInitialContextFactory {
    
    /**
    * Creates an initial context with
    * {@inheritDoc}
    */
    @Override
    @SuppressWarnings("unchecked")
    public Context getInitialContext(Hashtable environment) throws NamingException {
    
    Map<String, Object> data = new ConcurrentHashMap<String, Object>();
    
    String replicationTopicConnectionFactoryBindingName = (String)environment.get(JMSUtil.TOPIC_CONNECTION_FACTORY_BINDING_NAME);
    if(replicationTopicConnectionFactoryBindingName!=null)
    {
    try {
    data.put(replicationTopicConnectionFactoryBindingName, createConnectionFactory(environment));
    } catch (URISyntaxException e) {
    throw new NamingException("Error initialisating TopicConnectionFactory with message " + e.getMessage());
    }
    }String getQueueConnectionfactoryBindingName = (String)environment.get(JMSUtil.GET_QUEUE_CONNECTION_FACTORY_BINDING_NAME);
    
    try {
    data.put(getQueueConnectionfactoryBindingName, createConnectionFactory(environment));
    } catch (URISyntaxException e) {
    throw new NamingException("Error initialisating TopicConnectionFactory with message " + e.getMessage());
    }
    
    String replicationTopicBindingName = (String)environment.get(JMSUtil.REPLICATION_TOPIC_BINDING_NAME);
    String getQueueBindingName = (String)environment.get(JMSUtil.GET_QUEUE_BINDING_NAME);
    if(replicationTopicBindingName!=null)
    {
    data.put(replicationTopicBindingName, createTopic(replicationTopicBindingName));
    }
    data.put(getQueueBindingName, createQueue(getQueueBindingName));
    return createContext(environment, data);
    }
    }
    cs