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);
}
}
相关文章
|
19天前
|
安全 Java 数据安全/隐私保护
|
19天前
|
Java 编译器 数据库
Spring Boot 整合 redisson 实现分布式锁
Spring Boot 整合 redisson 实现分布式锁
40 1
|
19天前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
241 0
|
19天前
|
SpringCloudAlibaba Java 网络架构
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
144 0
|
19天前
|
消息中间件 Java 关系型数据库
Spring事务与分布式事务
这篇文档介绍了事务的概念和数据库事务的ACID特性:原子性、一致性、隔离性和持久性。在并发环境下,事务可能出现更新丢失、脏读和不可重复读等问题,这些问题通过设置事务隔离级别(如读未提交、读已提交、可重复读和序列化)来解决。Spring事务传播行为有七种模式,影响嵌套事务的执行方式。`@Transactional`注解用于管理事务,其属性包括传播行为、隔离级别、超时和只读等。最后提到了分布式事务,分为跨库和跨服务两种情况,跨服务的分布式事务通常通过最终一致性策略,如消息队列实现。
|
19天前
|
消息中间件 人工智能 Java
Spring Boot+RocketMQ 实现多实例分布式环境下的事件驱动
Spring Boot+RocketMQ 实现多实例分布式环境下的事件驱动
32 1
|
19天前
|
Dubbo Java 应用服务中间件
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架
224 0
|
19天前
|
存储 Java 开发工具
轻松搭建分布式对象存储:Spring Boot整合MinIO的快速指南
轻松搭建分布式对象存储:Spring Boot整合MinIO的快速指南
133 0
轻松搭建分布式对象存储:Spring Boot整合MinIO的快速指南
|
19天前
|
敏捷开发 监控 前端开发
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
105 0
|
19天前
|
消息中间件 SpringCloudAlibaba Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
805 0