JMS分布式应用程序异步消息解决方案EhCache 高速缓存同步问题
部分博客中描述的使用拦截器怎么用EJB公布的WebService加入缓存,这样能够提高WebService的响应效率。但是即使是这样做,还是要经历网络的传输的。于是决定在调用WebService的程序本地也加入EJB方法缓存。假设WebService调用的结果已经存在于本地缓存中,就直接从内存中拿数据,不用再訪问WebService了。
架构图例如以下所看到的
可是还有一个问题又出现了,那就是WebService中的缓存和客户程序本地缓存的同步问题。这个问题能够详细描写叙述例如以下:
当提供WebService的程序的数据库中的数据发生改变后(程序运行了增删改方法后),就须要将WebService的缓存清空,由于那些是脏数据。但是调用WebService的客户程序本地的缓存却没有清空。
如何解决问题呢?如何才干清空WebService缓存的同一时候也清空调用client本地的缓存呢?利用JMS的消息机制就能够解决这一问题
详细思路
在WebService服务端创建一个JMS
Topic,起名CacheTopic
当服务端运行增删改方法后,向CacheTopic中发一条消息
客户程序在自己的server中部署Message Driven Bean,监听CacheTopic中的消息,收到消息后清空本地缓存
架构图例如以下所看到的
项目中使用的AS都是JBoss,在JBoss中加入JMS
Topic的方法是在deploy文件夹下部署一个Destination描写叙述文件,文件名称符合*-service.xml。
本项目中使用的CacheTopic的部署文件内容例如以下
<?xml version="1.0" encoding="UTF-8"? > <server> <!--使用jboss messaging定义topic-->
<mbean code="org.jboss.jms.server.destination.TopicService"
name="jboss.messaging.destination:service=Topic,name=CacheTopic"
xmbean-dd="xmdesc/Topic-xmbean.xml">
<depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
<depends>jboss.messaging:service=PostOffice</depends>
<attribute name="JNDIName" >topic/CacheTopic</attribute>
</mbean> </server>
服务端程序在运行增删改方法后,不仅要清除WebService中的缓存。还要向CacheTopic中发送消息
上篇博客中的拦截器改动例如以下(主要是加入了发送消息的功能):
public class CacheClearSyncInterceptor {
@AroundInvoke
public Object clearCache(InvocationContext context) throws Exception{
//运行目标方法
Object returnObj =context.proceed();
/**************************清空本地缓存 begin**************************************/
System.out.println("清空前的缓存数:"+CacheHandler.getInstance().getCache().getSize());
//清空本地缓存
CacheHandler.getInstance().clearCache();
System.out.println("清空后的缓存数:"+CacheHandler.getInstance().getCache().getSize());
/**************************清空本地缓存 end**************************************/
//发送消息到CacheTopic,实现缓存同步
StringBuilder txtMsgBuilder = new StringBuilder();
txtMsgBuilder.append("【gxpt-jc】系统运行了【")
.append(context.getTarget().getClass().getName())
.append(".")
.append(context.getMethod().getName())
.append("】")
.append("方法,须要同步缓存");
MessageSender.send(txtMsgBuilder.toString(), DestinationType.TOPIC,"topic/CacheTopic","192.168.24.48:1199");
return returnObj;
}
}
上面用到的消息发送者类MessageSender的代码例如以下
public class MessageSender {
/**
* @MethodName : send
* @Description : 发送消息
* @param msg 消息
* @param type 目的地类型:TOPIC或QUEUE
* @param destinationJndi 目的地的jndi名称
* @param url 目的地url
*/
public static void send(String msg,DestinationType type,String destinationJndi,String url) throws Exception{
//定义连接对象和session
TopicConnection topicConnection=null;
TopicSession topicSession = null;
QueueConnection queueConnection=null;
QueueSession queueSession = null;
try {
//创建context
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", url);
Context ctx = new InitialContext(props);
/************************************发消息给TOPIC begin******************************************************/
if(type==DestinationType.TOPIC){
TopicConnectionFactory topicFactory=(TopicConnectionFactory)ctx.lookup("ConnectionFactory");
//获取Connection
topicConnection=topicFactory.createTopicConnection();
//获取Session
topicSession=topicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
//获取destination
Topic topic=(Topic)ctx.lookup(destinationJndi);
//创建消息发送者
TopicPublisher publisher=topicSession.createPublisher(topic);
//创建消息
TextMessage txtMsg = topicSession.createTextMessage(msg);
//发送消息
publisher.publish(txtMsg);
}
/************************************发消息给TOPIC end******************************************************/
/************************************发消息给QUEUE begin******************************************************/
if(type==DestinationType.QUEUE){
QueueConnectionFactory queueFactory=(QueueConnectionFactory)ctx.lookup("ConnectionFactory");
//获取Connection
queueConnection=queueFactory.createQueueConnection();
//获取Session
queueSession=queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
//获取destination
Queue queue=(Queue)ctx.lookup(destinationJndi);
//创建消息发送者
QueueSender sender=queueSession.createSender(queue);
//创建消息
TextMessage txtMsg = queueSession.createTextMessage(msg);
//发送消息
sender.send(txtMsg);
}
/************************************发消息给QUEUE end******************************************************/
} finally{
//关闭对象
if(topicConnection!=null && topicSession!=null){
topicSession.close();
topicConnection.close();
}
if(queueConnection!=null && queueSession!=null){
queueSession.close();
queueConnection.close();
}
}
}
}
client接收消息的MDB的代码例如以下
@MessageDriven(
activationConfig={
@ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Topic"),
@ActivationConfigProperty(propertyName="destination",propertyValue="topic/CacheTopic"),
@ActivationConfigProperty(propertyName="providerAdapterJNDI", propertyValue="java:/RemoteJMSProvider")
}
)
public class KSCacheSyncMdb implements MessageListener{ public void onMessage(Message msg){
try {
//获取消息文本
TextMessage txtMsg = (TextMessage)msg;
//显示文本消息
System.out.println("因为"+txtMsg.getText()); /**************************清空本地缓存 begin**************************************/
System.out.println("清空前的缓存数:"+CacheHandler.getInstance().getCache().getSize());
//清空本地缓存
CacheHandler.getInstance().clearCache();
System.out.println("清空后的缓存数:"+CacheHandler.getInstance().getCache().getSize());
/**************************清空本地缓存 end**************************************/ } catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
} }
由于在JBoss5.1.0中部署的MDB默认仅仅能监听本地Destination中的消息。为了让MDB能够监听远程Destination中的消息。client仍需再部署一个RemoteJMSProvider描写叙述文件,文件名称相同需符合*-service.xml。文件内容例如以下
<? xml version="1.0" encoding="UTF-8"?>
<server>
<mbean code="org.jboss.jms.jndi.JMSProviderLoader" name="jboss.messaging:service=JMSProviderLoader,name=RemoteJMSProvider">
<attribute name="ProviderName">RemoteJMSProvider</attribute>
<attribute name="ProviderAdapterClass">
org.jboss.jms.jndi.JNDIProviderAdapter
</attribute>
<!-- The combined connection factory -->
<attribute name="FactoryRef">XAConnectionFactory</attribute>
<!-- The queue connection factory -->
<attribute name="QueueFactoryRef">XAConnectionFactory</attribute>
<!-- The topic factory -->
<attribute name="TopicFactoryRef">XAConnectionFactory</attribute>
<!-- Uncomment to use HAJNDI to access JMS-->
<attribute name="Properties">
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=192.168.24.48:1199
</attribute>
</mbean> </server>
这样就实现了分布式应用程序缓存同步的
版权声明:本文博主原创文章,博客,未经同意不得转载。
JMS分布式应用程序异步消息解决方案EhCache 高速缓存同步问题的更多相关文章
- JMS异步消息机制
企业消息系统 Java Message Service 是由 Sun Microsystems 开发的,它为 Java 程序提供一种访问 企业消息系统 的方法.在讨论 JMS 之前,我们分来析一下企业 ...
- 1.异步消息Jms及其JmsTemplate的源代码分析,消息代理ActiveMQ
一. 介绍 借助Spring,有多种异步消息的可选方案,本章使用Jms.Jms的消息模型有两种,点对点消息模型(队列实现)和发布-订阅消息模型(主题). 图1.点对点消息模型(一对一) 图2.发布-订 ...
- (十七)SpringBoot之使用异步消息服务jms之ActiveMQ
一.引入maven依赖 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...
- 基于.NET平台的分布式应用程序的研究
摘 要:.NET框架是Microsoft用于生成分布式Web应用程序和Web服务的下一代平台.概述了用于生成分布式应用程序的.NET框架的基本原理.重点讲述了.NET框架的基础:公共语言运行时(CLR ...
- Android应用程序线程消息循环模型分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6905587 我们知道,Android应用程序是 ...
- Android异步消息机制
Android中的异步消息机制分为四个部分:Message.Handler.MessageQueue和Looper. 其中,Message是线程之间传递的消息,其what.arg1.arg2字段可以携 ...
- 使用Spring JMS轻松实现异步消息传递
异步进程通信是面向服务架构(SOA)一个重要的组成部分,因为企业里很多系统通信,特别是与外部组织间的通信,实质上都是异步的.Java消息服务(JMS)是用于编写使用异步消息传递的JEE应用程序的API ...
- #研发中间件介绍#异步消息可靠推送Notify
郑昀 基于朱传志的设计文档 最后更新于2014/11/11 关键词:异步消息.订阅者集群.可伸缩.Push模式.Pull模式 本文档适用人员:研发 电商系统为什么需要 NotifyServer? ...
- 三.RabbitMQ之异步消息队列(Work Queue)
上一篇文章简要介绍了RabbitMQ的基本知识点,并且写了一个简单的发送和接收消息的demo.这一篇文章继续介绍关于Work Queue(工作队列)方面的知识点,用于实现多个工作进程的分发式任务. 一 ...
随机推荐
- HTTP协议中的短轮询、长轮询、长连接和短连接
HTTP协议中的短轮询.长轮询.长连接和短连接 引言 最近刚到公司不到一个月,正处于熟悉项目和源码的阶段,因此最近经常会看一些源码.在研究一个项目的时候,源码里面用到了HTTP的长轮询.由于之前没太接 ...
- 【PLSQL】绑定变量,活跃SQL,软硬解析解析
************************************************************************ ****原文:blog.csdn.net/clar ...
- 为了找到自己的路——leo锦书62
<Leo锦书(文章1编辑)>百度已经降落阅读,今后将继续更新.免费下载:http://t.cn/RvawZEx 柯克•卡梅隆是谁呢?在中国听过这名字的人预计不多.但看下封面我们马下就会说: ...
- 完美攻略心得之圣魔大战3(Castle Fantisia)艾伦希亚战记(艾伦西亚战记)包含重做版(即新艾伦希亚战记)
(城堡幻想曲3,纠正大家个错误哦,不是圣魔大战3,圣魔大战是城堡幻想曲2,圣魔大战不是个系列,艾伦西亚战记==艾伦希亚战记,一个游戏日文名:タイトル キャッスルファンタジア -エレンシア戦記-リニュー ...
- apache +php +php curl 模块设置
2.2 linux 下面 2.2.1 web服务器安装 1目前采用的web服务器是apache2,在ubuntu 下安装 apt-getupdate apt-get installapache2 测试 ...
- 21天教你学会C++
- 关于多线程的一个例子(UI实时显示)
在开发Window应用程序的时候,经常需要在界面上显示出已经执行到什么步骤了,拿一个简单例子来说,创建一个Winform程序,在窗体上访一个Button和一个Label,点击Button时做100次循 ...
- Asp.Net2.0下C#环境 Login控件实现用户登录
原文:Asp.Net2.0下C#环境 Login控件实现用户登录 一.前台显示效果 二.前台代码 <asp:Login ID="Login1" run ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级) 企业库提供了一个很强大的验证应用程序模 ...
- RMQ之ST算法
#include <stdio.h> #include <string.h> ; int a[N]; ]; inline int min(const int &a, c ...