针对公司业务逻辑,向阿里云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模式)的更多相关文章

  1. 【运维技术】CentOS7上从零开始安装阿里RocketMQ版本:release-4.0.1【亲测哈哈】

    CentOS7上从零开始安装阿里RocketMQ版本:release-4.0.1[亲测哈哈] 安装git # 更新包 $ yum update # 安装git $ yum install git # ...

  2. (转)阿里 RocketMQ 安装与简介

    原文:阿里 RocketMQ 安装与简介 一.简介 官方简介: l  RocketMQ是一款分布式.队列模型的消息中间件,具有以下特点: l  能够保证严格的消息顺序 l  提供丰富的消息拉取模式 l ...

  3. 阿里RocketMq试用记录+简单的Spring集成

    CSDN学院招募微信小程序讲师啦 程序猿全指南,让[移动开发]更简单! [观点]移动原生App开发 PK HTML 5开发 云端应用征文大赛,秀绝招,赢无人机! 阿里RocketMq试用记录+简单的S ...

  4. keepalived绑定单播地址、非抢占模式及LVS的TCP模式的高可用

    背景:keepalived默认是组播地址进行播放,且默认地址是224.0.0.18,如果配置多个keepalived主机,会导致虚拟IP地址存在冲突问题,这种问题怎么解决呢? 解决办法:就是将keep ...

  5. RabbitMQ,Apache的ActiveMQ,阿里RocketMQ,Kafka,ZeroMQ,MetaMQ,Redis也可实现消息队列,RabbitMQ的应用场景以及基本原理介绍,RabbitMQ基础知识详解,RabbitMQ布曙

    消息队列及常见消息队列介绍 2017-10-10 09:35操作系统/客户端/人脸识别 一.消息队列(MQ)概述 消息队列(Message Queue),是分布式系统中重要的组件,其通用的使用场景可以 ...

  6. Alibaba(阿里) RocketMQ入门实例

    摘自:码友18年(www.mayou18.com) what is rocketMQ? RocketMQ作为一款分布式的消息中间件(阿里的说法是不遵循任何规范的,所以不能完全用JMS的那一套东西来看它 ...

  7. 阿里 RocketMQ 安装与简介

    一.简介 官方简介: l  RocketMQ是一款分布式.队列模型的消息中间件,具有以下特点: l  能够保证严格的消息顺序 l  提供丰富的消息拉取模式 l  高效的订阅者水平扩展能力 l  实时的 ...

  8. UDP模式与TCP模式的区别

    1.TCP有连接状态,而UDP没有. 2.TCP应用层使用无需考虑包的大小及发送情况,而UDP需要. 3.TCP中IP包大小的决定者是Socket,而UDP为应用层.

  9. (转)阿里RocketMQ Quick Start

    转:http://blog.csdn.net/a19881029/article/details/34446629 RocketMQ单机支持1万以上的持久化队列,前提是足够的内存.硬盘空间,过期数据数 ...

随机推荐

  1. 【XSY1295】calc n个点n条边无向连通图计数 prufer序列

    题目大意 求\(n\)个点\(n\)条边的无向连通图的个数 \(n\leq 5000\) 题解 显然是一个环上有很多外向树. 首先有一个东西:\(n\)个点选\(k\)个点作为树的根的生成森林个数为: ...

  2. readlink: command not found 解决方案

    /c/Program Files (x86)/Yarn/bin/yarn: line 3: readlink: command not found 用gitbash运行yarn时提示这个错误,但没有直 ...

  3. 【题解】 bzoj2982: combination (Lucas定理)

    题面戳我 Solution 板子题 Code //It is coded by ning_mew on 7.25 #include<bits/stdc++.h> #define LL lo ...

  4. Hdoj 2109.Fighting for HDU 题解

    Problem Description 在上一回,我们让你猜测海东集团用地的形状,你猜对了吗?不管结果如何,都没关系,下面我继续向大家讲解海东集团的发展情况: 在最初的两年里,HDU发展非常迅速,综合 ...

  5. 【COGS2652】秘术「天文密葬法」(长链剖分,分数规划)

    [COGS2652]秘术「天文密葬法」(长链剖分,分数规划) 题面 Cogs 上面废话真多,建议直接拉到最下面看一句话题意吧: 给个树,第i个点有两个权值ai和bi,现在求一条长度为m的路径,使得Σa ...

  6. 持久化和公平分发.py

    1.消息持久化在实际应用中,可能会发生消费者收到Queue中的消息,但没有处理完成就宕机(或出现其他意外)的情况,这种情况下就可能会导致消息丢失.为了避免这种情况发生,我们可以要求消费者在消费完消息后 ...

  7. cf1076E Vasya and a Tree (线段树)

    我的做法: 给询问按$deep[v]+d$排序,每次做到某一深度的时候,先给这个深度所有点的值清0,然后直接改v的子树 官方做法比较妙妙: dfs,进入v的时候给$[deep[v],deep[v]+d ...

  8. [WC2010]重建计划(分数规划+点分治+单调队列)

    题目大意:给定一棵树,求一条长度在L到R的一条路径,使得边权的平均值最大. 题解 树上路径最优化问题,不难想到点分治. 如果没有长度限制,我们可以套上01分数规划的模型,让所有边权减去mid,求一条路 ...

  9. Mysql 从入门到遗忘

    高级数据过滤: WHERE AND OR NOT 总是与其他操作符一起使用,用在要过滤的前面. 通配符过滤: LIKE: %相当于正则中的.*?,_相当于正则中的.. $ select id from ...

  10. 51Nod--1076 2条不相交的路径(强连通分量)

    电波 #include<bits/stdc++.h> using namespace std; #define LL long long #define maxn 30000 vector ...