spring ehcache jms activemq 分布式实现方案

简介: spring ehcache jms activemq 分布式实现方案

本文章来自国外一个博客,亲测可用,下面粘贴过来的是核心配置。

访问地址: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


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);
}
}
相关文章
|
23天前
|
NoSQL 数据库 Redis
分布式锁的实现方案有哪些
分布式锁用于协调跨多个节点的任务执行。基于数据库的分布式锁利用唯一性约束或悲观锁确保锁的唯一性;Redis 实现则依赖 SETNX 指令或 redisson 客户端,通过原子操作保证互斥性;ZooKeeper 通过临时顺序节点与 Watch 机制,实现锁的竞争、释放及获取。
35 4
|
1月前
|
缓存 Java Spring
实战指南:四种调整 Spring Bean 初始化顺序的方案
本文探讨了如何调整 Spring Boot 中 Bean 的初始化顺序,以满足业务需求。文章通过四种方案进行了详细分析: 1. **方案一 (@Order)**:通过 `@Order` 注解设置 Bean 的初始化顺序,但发现 `@PostConstruct` 会影响顺序。 2. **方案二 (SmartInitializingSingleton)**:在所有单例 Bean 初始化后执行额外的初始化工作,但无法精确控制特定 Bean 的顺序。 3. **方案三 (@DependsOn)**:通过 `@DependsOn` 注解指定 Bean 之间的依赖关系,成功实现顺序控制,但耦合性较高。
实战指南:四种调整 Spring Bean 初始化顺序的方案
|
25天前
|
缓存 NoSQL Java
Spring Boot中的分布式缓存方案
Spring Boot提供了简便的方式来集成和使用分布式缓存。通过Redis和Memcached等缓存方案,可以显著提升应用的性能和扩展性。合理配置和优化缓存策略,可以有效避免常见的缓存问题,保证系统的稳定性和高效运行。
43 3
|
1月前
|
消息中间件 监控 Java
您是否已集成 Spring Boot 与 ActiveMQ?
您是否已集成 Spring Boot 与 ActiveMQ?
53 0
|
3月前
|
前端开发 Java Spring
【非降版本解决】高版本Spring boot Swagger 报错解决方案
【非降版本解决】高版本Spring boot Swagger 报错解决方案
109 2
|
2月前
|
存储 NoSQL Java
Spring Boot项目中使用Redis实现接口幂等性的方案
通过上述方法,可以有效地在Spring Boot项目中利用Redis实现接口幂等性,既保证了接口操作的安全性,又提高了系统的可靠性。
60 0
|
5月前
|
存储 缓存 安全
Spring初始化加速的思路和方案问题之手动指定要异步初始化的bean中的问题如何解决
Spring初始化加速的思路和方案问题之手动指定要异步初始化的bean中的问题如何解决
|
5月前
|
Java Spring
Spring初始化加速的思路和方案问题之在BeanFactory#doGetBean方法中,栈状态的变化影响bean的初始化的问题如何解决
Spring初始化加速的思路和方案问题之在BeanFactory#doGetBean方法中,栈状态的变化影响bean的初始化的问题如何解决
|
5月前
|
存储 Java Spring
Spring初始化加速的思路和方案问题之替换默认的Spring Bean初始化逻辑中的问题如何解决
Spring初始化加速的思路和方案问题之替换默认的Spring Bean初始化逻辑中的问题如何解决
|
5月前
|
Java Spring
Spring初始化加速的思路和方案问题之DAG分析在Spring并行初始化中面临哪些困难
Spring初始化加速的思路和方案问题之DAG分析在Spring并行初始化中面临哪些困难