admin
2020-07-02 a62d474f024332dde7616054c549b7794f120151
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.yeshi.fanli.service.manger.msg;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Component;
 
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.transaction.LocalTransactionExecuter;
import com.aliyun.openservices.ons.api.transaction.TransactionProducer;
import com.aliyun.openservices.ons.api.transaction.TransactionStatus;
 
/**
 * RocketMQ管理器
 * @author Administrator
 *
 */
@Component
public class RocketMQManager {
    @Resource(name = "producer")
    private Producer producer;
    @Resource
    private TransactionProducer orderTransactionProducer;
 
    /**
     * 发送普通消息
     * @Title: sendNormalMsg
     * @Description: 
     * @param message 
     * void 返回类型
     * @throws
     */
    public SendResult sendNormalMsg(Message message, String key) {
        return sendNormalMsg(message, null, key);
    }
 
    /**
     * 发送普通定时消息
     * @Title: sendNormalMsg
     * @Description: 
     * @param message
     * @param delayTimeMS 延时ms
     * void 返回类型
     * @throws
     */
    public SendResult sendNormalMsg(Message message, Long delayTimeMS, String key) {
        if (message == null)
            return null;
        if (key != null)
            message.setKey(key);
        if (delayTimeMS != null)
            message.setStartDeliverTime(System.currentTimeMillis() + delayTimeMS);// 10s后发送活跃消息
        return producer.send(message);
    }
 
    /**
     * 发送事务消息
     * @Title: sendTransactionalMsg
     * @Description: 
     * @param message
     * @param delayTimeMS  延时ms
     * @param key
     * @param mqEvent
     * @throws Exception 
     * void 返回类型
     * @throws
     */
    public void sendTransactionalMsg(Message message, Long delayTimeMS, String key, ITransactionalMQEvent mqEvent)
            throws Exception {
        if (key != null)
            message.setKey(key);
        if (delayTimeMS != null)
            message.setStartDeliverTime(System.currentTimeMillis() + delayTimeMS);
 
        orderTransactionProducer.send(message, new LocalTransactionExecuter() {
            @Override
            public TransactionStatus execute(Message arg0, Object arg1) {
                if (mqEvent != null)
                    return mqEvent.excute( arg0,  arg1);
                return TransactionStatus.CommitTransaction;
            }
        }, null);
    }
 
    /**
     * 发送事务消息
     * @Title: sendTransactionalMsg
     * @Description: 
     * @param message
     * @param key
     * @param mqEvent
     * @throws Exception 
     * void 返回类型
     * @throws
     */
    public void sendTransactionalMsg(Message message, String key, ITransactionalMQEvent mqEvent) throws Exception {
        sendTransactionalMsg(message, null, key, mqEvent);
    }
 
    public interface ITransactionalMQEvent {
        public TransactionStatus excute(Message arg0, Object arg1);
    }
}