部分博客中描述的使用拦截器怎么用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 高速缓存同步问题的更多相关文章

  1. JMS异步消息机制

    企业消息系统 Java Message Service 是由 Sun Microsystems 开发的,它为 Java 程序提供一种访问 企业消息系统 的方法.在讨论 JMS 之前,我们分来析一下企业 ...

  2. 1.异步消息Jms及其JmsTemplate的源代码分析,消息代理ActiveMQ

    一. 介绍 借助Spring,有多种异步消息的可选方案,本章使用Jms.Jms的消息模型有两种,点对点消息模型(队列实现)和发布-订阅消息模型(主题). 图1.点对点消息模型(一对一) 图2.发布-订 ...

  3. (十七)SpringBoot之使用异步消息服务jms之ActiveMQ

    一.引入maven依赖 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...

  4. 基于.NET平台的分布式应用程序的研究

    摘 要:.NET框架是Microsoft用于生成分布式Web应用程序和Web服务的下一代平台.概述了用于生成分布式应用程序的.NET框架的基本原理.重点讲述了.NET框架的基础:公共语言运行时(CLR ...

  5. Android应用程序线程消息循环模型分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6905587 我们知道,Android应用程序是 ...

  6. Android异步消息机制

    Android中的异步消息机制分为四个部分:Message.Handler.MessageQueue和Looper. 其中,Message是线程之间传递的消息,其what.arg1.arg2字段可以携 ...

  7. 使用Spring JMS轻松实现异步消息传递

    异步进程通信是面向服务架构(SOA)一个重要的组成部分,因为企业里很多系统通信,特别是与外部组织间的通信,实质上都是异步的.Java消息服务(JMS)是用于编写使用异步消息传递的JEE应用程序的API ...

  8. #研发中间件介绍#异步消息可靠推送Notify

    郑昀 基于朱传志的设计文档 最后更新于2014/11/11 关键词:异步消息.订阅者集群.可伸缩.Push模式.Pull模式 本文档适用人员:研发   电商系统为什么需要 NotifyServer? ...

  9. 三.RabbitMQ之异步消息队列(Work Queue)

    上一篇文章简要介绍了RabbitMQ的基本知识点,并且写了一个简单的发送和接收消息的demo.这一篇文章继续介绍关于Work Queue(工作队列)方面的知识点,用于实现多个工作进程的分发式任务. 一 ...

随机推荐

  1. Django写的投票系统4(转)

    原文地址:http://www.cnblogs.com/djangochina/archive/2013/06/04/3114269.html 现在已经可以在后台管理投票了,现在就差怎么在前台显示和如 ...

  2. 风起看云涌,叶落品人生 - Google 搜索

    风起看云涌,叶落品人生 - Google 搜索 风起看云涌,叶落品人生

  3. uva 10066 The Twin Towers (最长公共子)

    uva 10066 The Twin Towers 标题效果:最长公共子. 解题思路:最长公共子. #include<stdio.h> #include<string.h> # ...

  4. python面向对象的继承

    无话可说,继承主要是一些父类继承,代码是非常具体的 #!/usr/bin/env python #coding:utf-8 class Father(object):#新式类 def __init__ ...

  5. Linux系统部署规范v1.0

    Linux系统部署规范v1.0 目的: 1.尽可能减少线上操作: 2.尽可能实现自动化部署: 3.尽可能减少安装服务和启动的服务: 4.尽可能使用安全协议提供服务: 5.尽可能让业务系统单一: 6.尽 ...

  6. JVM最多支持多少个线程?

    JVM最多支持多少个线程? McGovernTheory在StackOverflow提了这样一个问题: Java虚拟机最多支持多少个线程?跟虚拟机开发商有关么?跟操作系统呢?还有其他的因素吗? Edd ...

  7. iOS很重要的 block回调

    刚刚进入ios开发行业,发现开发中要用到大量的block回调,由此可见它的重要性.学习它之前我也是网上找的资料,推荐这篇文章http://blog.csdn.net/mobanchengshuang/ ...

  8. CheckBoxList的操作查询是否被选中设置或者得到

    在项目中我们可能会经常遇到一收集多选信息的情况,比如做注册的时候要收集个人爱好,那时候大家第一个想到的肯定是CheckBoxList.那我们怎么来获取到CheckBoxList的值并且存入数据库呢?? ...

  9. 我的CSDN之路

    经营CSDN博客刚满一年,能够说CSDN给我们带来了非常大的帮助,这一年里CSDN见证了我们的成长.以下两篇博客就是我这一年在CSDN的缩影. 这篇是我一年前写的,那时候刚申请CSDN不久:http: ...

  10. Codeforces Round #253 (Div. 1) B. Andrey and Problem

    B. Andrey and Problem time limit per test 2 seconds memory limit per test 256 megabytes input standa ...