|
|
|
@@ -1,6 +1,8 @@ |
|
|
|
package com.iformall.mq.impl; |
|
|
|
|
|
|
|
import java.util.Date; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Properties; |
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
@@ -18,6 +20,7 @@ import com.aliyun.openservices.ons.api.Producer; |
|
|
|
import com.aliyun.openservices.ons.api.PropertyKeyConst; |
|
|
|
import com.aliyun.openservices.ons.api.SendResult; |
|
|
|
import com.iformall.domain.po.msg.BaseMsg; |
|
|
|
import com.iformall.enums.EnumMsgMqTopic; |
|
|
|
import com.iformall.mq.MQConfig; |
|
|
|
import com.iformall.mq.MqBaseProducer; |
|
|
|
import com.iformall.utils.JsonUtil; |
|
|
|
@@ -52,28 +55,37 @@ public class AliyunRocketMqMessageProducer implements MqBaseProducer { |
|
|
|
//@Autowired |
|
|
|
//private WxMsgRecordService wxMsgRecordService; |
|
|
|
|
|
|
|
private static Producer producer = null; |
|
|
|
private static Map<String,Producer> producerMap = null; |
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
public void init() { |
|
|
|
Properties properties = new Properties(); |
|
|
|
properties.setProperty(PropertyKeyConst.GROUP_ID, groupId); |
|
|
|
// AccessKey 阿里云身份验证,在阿里云服务器管理控制台创建 |
|
|
|
properties.put(PropertyKeyConst.AccessKey,accessKeyId); |
|
|
|
// SecretKey 阿里云身份验证,在阿里云服务器管理控制台创建 |
|
|
|
properties.put(PropertyKeyConst.SecretKey, accessKeySecret); |
|
|
|
//设置发送超时时间,单位毫秒 |
|
|
|
properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, "3000"); |
|
|
|
// 设置 TCP 接入域名,到控制台的实例基本信息中查看 |
|
|
|
properties.put(PropertyKeyConst.NAMESRV_ADDR,namesrvAddr); |
|
|
|
producer = ONSFactory.createProducer(properties); |
|
|
|
// 在发送消息前,必须调用 start 方法来启动 Producer,只需调用一次即可 |
|
|
|
producer.start(); |
|
|
|
log.info("aliyunrocketmq producer info :"+JSON.toJSONString(producer)); |
|
|
|
producerMap = new HashMap<String,Producer>(); |
|
|
|
for (EnumMsgMqTopic topic: EnumMsgMqTopic.values()) { |
|
|
|
Properties properties = new Properties(); |
|
|
|
properties.setProperty(PropertyKeyConst.GROUP_ID, topic.getGroupId()); |
|
|
|
// AccessKey 阿里云身份验证,在阿里云服务器管理控制台创建 |
|
|
|
properties.put(PropertyKeyConst.AccessKey,accessKeyId); |
|
|
|
// SecretKey 阿里云身份验证,在阿里云服务器管理控制台创建 |
|
|
|
properties.put(PropertyKeyConst.SecretKey, accessKeySecret); |
|
|
|
//设置发送超时时间,单位毫秒 |
|
|
|
properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, "3000"); |
|
|
|
// 设置 TCP 接入域名,到控制台的实例基本信息中查看 |
|
|
|
properties.put(PropertyKeyConst.NAMESRV_ADDR,namesrvAddr); |
|
|
|
Producer producer = ONSFactory.createProducer(properties); |
|
|
|
// 在发送消息前,必须调用 start 方法来启动 Producer,只需调用一次即可 |
|
|
|
producer.start(); |
|
|
|
log.info("aliyunrocketmq producer info :"+JSON.toJSONString(producer)); |
|
|
|
producerMap.put(topic.getCode(), producer); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void sendMessage(BaseMsg data, String topic, String tags, String keys) { |
|
|
|
Producer producer = producerMap.get(topic); |
|
|
|
if (null == producer) { |
|
|
|
log.error("topic :"+topic+" has no AliyunRocketMqMessageProducer."); |
|
|
|
return; |
|
|
|
} |
|
|
|
if (producer.isClosed()) { |
|
|
|
producer.start(); |
|
|
|
} |
|
|
|
|