消息队列MQ
常用的几个消息队列框架和术语
RPC模式:远程调用过程的简写。A通过RPC调用B进行处理,可以看作一次RPC。
MQ:A和B之间的消息池。传统A直接找B,使用MQ后,B将消息放入池子中,A在池子中找消息并处理,处理完毕后不返回结果。
RabbitMQ:
Redis:
ZeroMQ:
ActiveMQ:
Jafka/Kafka:
消息队列的理解
消息从某一端发出后,首先进入一个容器进行临时存储,当达到某种条件后,再由这个容器发送给另一端。 这个容器的一种具体实现就是==消息队列==。
应用场景
异步处理,应用解耦,流量削锋和消息通讯四个场景。
消息模式
P2P模式
消息队列(Queue),发送者(Sender),接收者(Receiver)
Pub/sub模式
主题(Topic),发布者(Publisher),订阅者(Subscriber)。
多个发布者将消息发送到Topic,系统将这些消息传递给多个订阅者。
项目中的mq消息队列的应用
由于本项目中需要申请mq服务资源。 通过wiki文档,可知需要配置哪些资源。 通过Controller层中是这样发送消息的
是==mqMessageSend()发送消息==。
@RequestMapping("/dept/update")
@ResponseBody
public Result update(UcarAbsDepartment dept) {
// 参数校验
if(null == dept || null == dept.getId()) {
LOGGER.error("请求数据对象中部门编号不能为空! ");
return Result.getBusinessException("请求数据对象中部门编号不能为空! ", "-1");
}
try {
dept.setModifyTime(new java.util.Date());
UcarAbsDepMessageProducer.mqMessageSend(dept);
return Result.getSuccessResult("更新成功");
} catch (Exception e) {
LOGGER.error("更新异常", e);
return Result.getBusinessException("更新异常", "-2");
}
}
配置metaConsumer.properties
配置好后接受者后,接收者接受消息并处理。
[@Override](https://my.oschina.net/u/1162528)
public void handlerMessage(MessageVO message) {
try {
byte[] messageByte = message.getData();
if(null == messageByte) {
LOGGER.error("消息Top-->[{}]数据为空, 请检查请求数据......", message.getTopic());
return;
}
Object obj = HessianSerializerUtils.deserialize(messageByte);
if(obj instanceof UcarAbsDepartment){
UcarAbsDepartment ucarAbsDepartment = (UcarAbsDepartment)obj;
LOGGER.info("消息Top-->[{}]获取消息信息: {}", ucarAbsDepartment.toString());
// 参数校验
if(null == ucarAbsDepartment.getId() ) {
LOGGER.error("队列请求数据对象中部门编号不能为空! ");
return;
}
// 业务数据更新处理)
UcarAbsDepartmentCacheServiceImpl carAbsDepartmentCacheService = SpringUtil.getBean(UcarAbsDepartmentCacheServiceImpl.class);
carAbsDepartmentCacheService.putDepartmentToCache(ucarAbsDepartment);
}
}catch (Exception e){
// 不抛出任何异常,避免服务器消息重发
LOGGER.error("消息Top-->[{}]处理发生异常......", message.getTopic(), e);
}
}