阿里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万以上的持久化队列,前提是足够的内存.硬盘空间,过期数据数 ...
随机推荐
- Python中xlrd模块解析
xlrd 导入模块 import xlrd 2.打开指定的excel文件,返回一个data对象 data = xlrd.open_workbook(file) ...
- Golden Eggs HDU - 3820(最小割)
Golden Eggs Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- codeforces 997C.Sky Full of Stars
题目链接:codeforces 997C.Sky Full of Stars 一道很简单(?)的推式子题 直接求显然不现实,我们考虑容斥 记\(f(i,j)\)为该方阵中至少有\(i\)行和\(j\) ...
- 「HDU - 2857」Mirror and Light(点关于直线的对称点)
题目链接 Mirror and Light 题意 一条直线代表镜子,一个入射光线上的点,一个反射光线上的点,求反射点.(都在一个二维平面内) 题解 找出入射光线关于镜子直线的对称点,然后和反射光线连边 ...
- Atcoder 乱做
最近感觉自己思维僵化,啥都不会做了-- ARC103 F Distance Sums 题意 给定第 \(i\) 个点到所有点的距离和 \(D_i\) ,要求构造一棵合法的树.满足第 \(i\) 个点到 ...
- Hdoj 1905.Pseudoprime numbers 题解
Problem Description Fermat's theorem states that for any prime number p and for any integer a > 1 ...
- luogu3621 城池攻占 (倍增)
好像所有人都写的左偏树 但我不会啊233 首先发现乘的时候 系数不会为负,所以能得到一个关键条件:变化后的战斗力随变化前的战斗力大小单调 所以我们考虑倍增 设hp[x][i]是从x开始一路攻克$2^i ...
- noiac64 sort (二分答案)
首先如果L=1,那就可以直接用一个优先队列来做 但它并不是1 所以要换个做法 假设我们已经知道第L的数是x,第R的数是y 那其实就只需要找到[x+1,y+1]这一段,然后再加上一定数量的x和y就是答案 ...
- 英语考试 FZU - 2254 (最小生成树)
先选一个单词出来完全自己背,然后从这个单词到其他各个单词所需要的精力看成距离,然后用最小生成树把这些单词连接起来,就是通过我现在选的这个单词到其他各个单词的最小精力,然后再加上把这个单词背起来的精力, ...
- A1128. N Queens Puzzle
The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboar ...