自己封装的RocketUtil
public class RocketMQUtils {
public static Producer getProducer(String producerId){
Properties properties = new Properties();
//您在控制台创建的Producer ID
properties.put(PropertyKeyConst.ProducerId,"PID_T_PaymentCallback_CMB");
// AccessKey 阿里云身份验证,在阿里云服务器管理控制台创建
properties.put(PropertyKeyConst.AccessKey,"LTAIIxdTlDBAT7N3");
// SecretKey 阿里云身份验证,在阿里云服务器管理控制台创建
properties.put(PropertyKeyConst.SecretKey, "HYeesDmlmFadiYCZ7TA0pIiCGu4PGk");
//设置发送超时时间,单位毫秒
properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, "3000");
// 设置 TCP 接入域名(此处以公共云生产环境为例)
properties.put(PropertyKeyConst.ONSAddr, "http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet");
Producer producer = ONSFactory.createProducer(properties);
// 在发送消息前,必须调用start方法来启动Producer,只需调用一次即可
producer.start();
System.out.println("haha");
return producer;
}
//创建一个消息
public static Message createMsg(String topic, String tags, Object msg){
return new Message(topic,tags,msg.toString().getBytes());
}
//客户端订阅消息
public static Consumer getCoustomer(String consumerId, String topic){
Properties properties = new Properties();
// 您在控制台创建的 Consumer ID
properties.put(PropertyKeyConst.ConsumerId, "CID_T_PaymentCallback_CMB");
// AccessKey 阿里云身份验证,在阿里云服务器管理控制台创建
properties.put(PropertyKeyConst.AccessKey, "LTAIIxdTlDBAT7N3");
// SecretKey 阿里云身份验证,在阿里云服务器管理控制台创建
properties.put(PropertyKeyConst.SecretKey, "HYeesDmlmFadiYCZ7TA0pIiCGu4PGk");
// 设置 TCP 接入域名(此处以公共云生产环境为例)
properties.put(PropertyKeyConst.ONSAddr,
"http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet");
// 集群订阅方式 (默认)
// properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.CLUSTERING);
// 广播订阅方式
// properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.BROADCASTING);
Consumer consumer = ONSFactory.createConsumer(properties);
consumer.subscribe(topic, "*", new MessageListener() { //订阅多个Tag
public Action consume(Message message, ConsumeContext context) {
System.out.println("Receive: " + message);
System.out.println(new String(message.getBody()));
return Action.CommitMessage;
}
});
consumer.start();
System.out.println("Consumer Started");
return consumer;
}
测试类
@RestController
public class WchatPayController {
@RequestMapping(value = "/payResult", method = RequestMethod.POST)
public void PayCallback(HttpServletRequest request, String producerId) {
Map<String, String> params = new TreeMap<String, String>();
// 取出所有参数是为了验证签名
Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String parameterName = parameterNames.nextElement();
params.put(parameterName, request.getParameter(parameterName));
}
JSONObject paramsObject = JSONObject.fromObject(params);
TLinx2Util.verifySign(paramsObject);
String ord_no = paramsObject.getString("ord_no");
String out_no = paramsObject.getString("out_no");
String rand_str = paramsObject.getString("rand_str");
String status = paramsObject.getString("status");
String timestamp = paramsObject.getString("timestamp");
String open_key = paramsObject.getString("open_key");
Map<String, Object> map = new HashMap<String, Object>();
map.put("ord_no", ord_no);
map.put("out_no", out_no);
map.put("rand_str", rand_str);
map.put("status", status);
map.put("timestamp", timestamp);
map.put("open_key", open_key);
Producer producer = RocketMQUtils.getProducer(producerId);
Message msg = RocketMQUtils.createMsg("T_PaymentCallback_CMB", "tags", map);
System.out.println(new String(msg.getBody()));
SendResult result = producer.send(msg);
System.out.println(result);
System.err.println("============回调参数是=============" + paramsObject.toString());
}
@RequestMapping(value = "/getCustorm", method = RequestMethod.POST)
public void getCustorm(String consumerId, String topic) {
Consumer consumer = RocketMQUtils.getCoustomer(consumerId, topic);
System.err.print(consumer + "已经启动");
}
compile group: 'com.aliyun.openservices', name: 'ons-client', version: '1.7.1.Final'