阿里RocketMq(TCP模式)
针对公司业务逻辑,向阿里云MQ发送指定数据,消费端根据数据来做具体的业务,分两个项目,一个生产端(Producer)、一个消费端(Consumer)
生产端通过定时任务执行sql向阿里云MQ发送数据,消费端消费指定Topic上的数据
1:定时任务列表:

2:生产端表结构:

aliasName:定时任务别名;
cronExpression:定时任务轮询规则;
jobGroup:定时任务分组;
jobName:定时任务名称;
jobTrigger:定时任务触发器;
packageUrl:定时任务扫描具体封装类;
excuteSql:扫描类中执行的获取数据的脚本;
lastPramaryKey:最后一次获取数据时最大的主键;
topic:阿里云MQ的topic;
producerId:生产端的Id;
accessKey、securityKey:账号跟秘钥
dataBaseType:操作数据库类型(公司数据库类型比较多,执行脚本时,需要根据类型来指定具体的Service)

3:Java端核心代码,定时任务扫描如下配置的任务类来向阿里云MQ发送数据
public class SendPrimaryKeyListToMqTask implements Job{
private final Logger logger = LoggerFactory.getLogger(SendPrimaryKeyListToMqTask.class);
public void execute(JobExecutionContext context) throws JobExecutionException{
JobDataMap data = context.getJobDetail().getJobDataMap();
ScheduleJob scheduleJob = (ScheduleJob)data.get("jobParam");
//最后一次获取数据时最大的主键
int lastPramaryKey = scheduleJob.getLastPramaryKey();
//执行sql
String excuteSql = scheduleJob.getExcuteSql();
excuteSql = excuteSql.replace("lastPramaryKey", String.valueOf(lastPramaryKey));
//操作数据库类型(数据库配置)
int dataBaseType = scheduleJob.getDataBaseType();
//从游戏库获取数据
LinkedList<ExcuteResultData> resultData = new LinkedList<ExcuteResultData>();
if( dataBaseType == 1 ){
GameService gameService = (GameService)SpringBeanFactory.getBean(GameService.class);
resultData = gameService.getExcuteResultData(excuteSql);
//从网站库获取数据
}else if( dataBaseType == 2 ){
SiteService siteService = (SiteService)SpringBeanFactory.getBean(SiteService.class);
resultData = siteService.getExcuteResultData(excuteSql);
}
if ( resultData.size() > 0 ){
scheduleJob.setPrimaryKeyList(resultData);
QuartzService quartzService = (QuartzService)SpringBeanFactory.getBean(QuartzService.class);
//将数据集中最大的主键更新
scheduleJob.setLastPramaryKey(resultData.getLast().getPrimaryKey());
quartzService.updateLastPramaryKey(scheduleJob);
String topic = scheduleJob.getTopic();
String producerId = scheduleJob.getProducerId();
String ak = scheduleJob.getAccessKey();
String sk = scheduleJob.getSecurityKey();
//添加日志
ScheduleJobLog scjLog = new ScheduleJobLog();
scjLog.setDataSize(resultData.size());
scjLog.setJobName(scheduleJob.getJobName());
scjLog.setTopic(topic);
int scjLogId = quartzService.addMqScheduleJobLog(scjLog);
//消费端根据此日志主键更新日志状态
scheduleJob.setScjLogId(scjLogId);
Properties properties = new Properties();
properties.put("ProducerId", producerId);
properties.put("AccessKey", ak);
properties.put("SecretKey", sk);
Producer producer = ONSFactory.createProducer(properties);
producer.start();
Message msg = new Message(topic, "PRIMARY_KEY_" + String.valueOf(scjLogId), ObjectsTranscoder.serialize(scheduleJob));
msg.setKey("PRIMARY_KEY_" + String.valueOf(scjLogId));
SendResult sendResult = producer.send(msg);
if ( ( sendResult != null ) && ( sendResult.getMessageId() != null ) ){
scjLog.setMessageId(sendResult.getMessageId());
scjLog.setStatus(2);
quartzService.updateMqScheduleJobLog(scjLog);
}
producer.shutdown();
logger.debug("=====>任务名称:" + scheduleJob.getJobName());
logger.debug("=====>发送条数:" + resultData.size());
logger.debug("=====>发送主键内容:" + resultData.toString());
}
}
}
4:消费端表结构:

5:消费端Java核心代码(通过监听器来做):
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.odao.common.utils.ObjectsTranscoder;
import com.odao.entity.ScheduleJob;
import com.odao.entity.ScheduleJobLog;
import com.odao.service.consumer.ConsumerService;
import com.odao.service.message.MessageService; /**
* 阿里云游戏、网站 主键数据集消费监听器
*/
public class ConsumePrimaryKeyFromMqListener implements ServletContextListener { @Override
public void contextInitialized(ServletContextEvent sce) {
WebApplicationContext appctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
MessageService messageService = (MessageService) appctx.getBean(MessageService.class);
List<ScheduleJob> consumeList = messageService.getScheduleJobList();
for(final ScheduleJob sjc : consumeList){ String topic = sjc.getTopic();
String consumerId= sjc.getConsumerId();
String ak = sjc.getAccessKey();
String sk = sjc.getSecurityKey(); Properties properties = new Properties();
properties.put(PropertyKeyConst.ConsumerId,consumerId);
properties.put(PropertyKeyConst.AccessKey,ak);
properties.put(PropertyKeyConst.SecretKey,sk);
//properties.put(PropertyKeyConst.ONSAddr,"http://onsaddr-internal.aliyun.com:8080/rocketmq/nsaddr4client-internal"); Consumer consumer = ONSFactory.createConsumer(properties); consumer.subscribe(topic, "*", new MessageListener() {
@Override
public Action consume(Message message, ConsumeContext context) {
ScheduleJob scheduleJob = (ScheduleJob) ObjectsTranscoder.deserialize(message.getBody());
if( scheduleJob !=null ){
//更新消息状态为3:消费消息成功
ScheduleJobLog scjLog = new ScheduleJobLog();
scjLog.setStatus(3);
scjLog.setMqScheduleJobLogId(scheduleJob.getScjLogId());
messageService.updateMqScheduleJobLog(scjLog);
try {
ConsumerService consumerService = (ConsumerService) Class.forName(sjc.getImplementClass()).newInstance();
boolean isSuccess = consumerService.consume(scheduleJob.getPrimaryKeyList());
if(isSuccess){
//更新消息状态为4:业务逻辑处理成功
scjLog.setStatus(4);
messageService.updateMqScheduleJobLog(scjLog);
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return Action.CommitMessage;
}
}); consumer.start();
}
} @Override
public void contextDestroyed(ServletContextEvent sce) { }
}
阿里RocketMq(TCP模式)的更多相关文章
- 【运维技术】CentOS7上从零开始安装阿里RocketMQ版本:release-4.0.1【亲测哈哈】
CentOS7上从零开始安装阿里RocketMQ版本:release-4.0.1[亲测哈哈] 安装git # 更新包 $ yum update # 安装git $ yum install git # ...
- (转)阿里 RocketMQ 安装与简介
原文:阿里 RocketMQ 安装与简介 一.简介 官方简介: l RocketMQ是一款分布式.队列模型的消息中间件,具有以下特点: l 能够保证严格的消息顺序 l 提供丰富的消息拉取模式 l ...
- 阿里RocketMq试用记录+简单的Spring集成
CSDN学院招募微信小程序讲师啦 程序猿全指南,让[移动开发]更简单! [观点]移动原生App开发 PK HTML 5开发 云端应用征文大赛,秀绝招,赢无人机! 阿里RocketMq试用记录+简单的S ...
- keepalived绑定单播地址、非抢占模式及LVS的TCP模式的高可用
背景:keepalived默认是组播地址进行播放,且默认地址是224.0.0.18,如果配置多个keepalived主机,会导致虚拟IP地址存在冲突问题,这种问题怎么解决呢? 解决办法:就是将keep ...
- RabbitMQ,Apache的ActiveMQ,阿里RocketMQ,Kafka,ZeroMQ,MetaMQ,Redis也可实现消息队列,RabbitMQ的应用场景以及基本原理介绍,RabbitMQ基础知识详解,RabbitMQ布曙
消息队列及常见消息队列介绍 2017-10-10 09:35操作系统/客户端/人脸识别 一.消息队列(MQ)概述 消息队列(Message Queue),是分布式系统中重要的组件,其通用的使用场景可以 ...
- Alibaba(阿里) RocketMQ入门实例
摘自:码友18年(www.mayou18.com) what is rocketMQ? RocketMQ作为一款分布式的消息中间件(阿里的说法是不遵循任何规范的,所以不能完全用JMS的那一套东西来看它 ...
- 阿里 RocketMQ 安装与简介
一.简介 官方简介: l RocketMQ是一款分布式.队列模型的消息中间件,具有以下特点: l 能够保证严格的消息顺序 l 提供丰富的消息拉取模式 l 高效的订阅者水平扩展能力 l 实时的 ...
- UDP模式与TCP模式的区别
1.TCP有连接状态,而UDP没有. 2.TCP应用层使用无需考虑包的大小及发送情况,而UDP需要. 3.TCP中IP包大小的决定者是Socket,而UDP为应用层.
- (转)阿里RocketMQ Quick Start
转:http://blog.csdn.net/a19881029/article/details/34446629 RocketMQ单机支持1万以上的持久化队列,前提是足够的内存.硬盘空间,过期数据数 ...
随机推荐
- hdu 5877 Weak Pair (Treap)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5877 题面; Weak Pair Time Limit: 4000/2000 MS (Java/Other ...
- 解决nginx发布网站跨目录访问
解决nginx发布网站跨目录访问(thinkphp5+lnmp) 到:usr/local/nginx/conf/vim fastcgi.cof 把最后一行加上井号#注释掉保存重启 restart 参考 ...
- 【比赛】NOIP2018 铺设道路
原题,而且还是CCF自己的 考虑对于一段最长不上升序列,无论如何都至少有序列第一个数的贡献,可以知道,这个贡献是可以做到且最少的 然后对于序列最后一位,也就是最小的那一个数,可以和后面序列拼起来的就拼 ...
- 安卓Q | 诸多本地文件找不到?应用文件存储空间沙箱化适配指导
上期我们针对Android Q 版本中对设备存储空间进行的限制.新特性变更引发的兼容性问题及原因分析推出了<安卓 Q | 8大场景全面解析应用存储沙箱化>文章,本期文章我们将手把手指导各位 ...
- Android 播放Gif 动画
在Android 中是不支持直接使用Gif 图片关联播放帧动画,如下动画在Android 中是无法播放的: Android 提供了另外一种解决的办法,就是使用AnimationDrawable 这一函 ...
- hexo博客添加功能
设置Hexo主题模式 Hexo主题中,有三种不同的模式,通过切换模式,让NexT主题显示不一样的样式.在NexT根目录下有一个同样名称为_config.yml,为了区分hexo根目录下的_config ...
- kvm虚拟化管理
虚拟化 KVM (kernel-based virtual machine) 常见的一些虚拟化的软件xen kvm vmware esx openVZ Oracle VM VirtualBox vsp ...
- Linux下启动,停止,重启Nginx、Mysql、PHP
LINUX启动Nginx的命令: 一.查询是否启动 [root@jiang php-fpm.d]# ps -ef | grep nginx root 25225 1 0 19:26 ? 00:00:0 ...
- 牛客练习赛31 D神器大师泰兹瑞与威穆
双链表搞完了 #include<bits/stdc++.h> using namespace std; #define maxn 1000005 int tot,bac[maxn],fa[ ...
- hibernate 5的二级缓存案例讲解
hibernate 5的二级缓存案例讲解 本帖最后由 鱼丸儿 于 2018-1-20 11:44 编辑 大家好,今天来记录讲解一下磕磕绊绊的hibernate5 的二级缓存配置,一条路摸到黑 那么在这 ...