浏览代码

msgframe的rocketmq测试代码

liutong3 6 年之前
父节点
当前提交
2876b3a8f4

+ 15 - 0
ipu-rocketmq-example/src/main/java/com/ai/ipu/example/rocketmq/RocketMQCustomer.java

@ -0,0 +1,15 @@
1
package com.ai.ipu.example.rocketmq;
2
3
import com.ai.aif.msgframe.consumer.MfServiceStartup;
4
5
/**
6
 * @author liutong3
7
 * @team IPU
8
 * @date 2019/6/10 14:13
9
 * @desc 启动RocketMQ消费者
10
 */
11
public class RocketMQCustomer {
12
    public static void main(String[] args){
13
        MfServiceStartup.main();
14
    }
15
}

+ 23 - 0
ipu-rocketmq-example/src/main/java/com/ai/ipu/example/rocketmq/RocketMQCustomerExample.java

@ -0,0 +1,23 @@
1
package com.ai.ipu.example.rocketmq;
2
3
import com.ai.aif.msgframe.common.IConsumerProcessor;
4
import com.ai.aif.msgframe.common.exception.ConsumerException;
5
import com.ai.aif.msgframe.common.message.MsgFMessage;
6
import com.ai.aif.msgframe.common.message.MsgFTextMessage;
7
8
/**
9
 * @author liutong3
10
 * @team IPU
11
 * @date 2019/6/10 14:15
12
 * @desc RocketMQ消费者
13
 */
14
public class RocketMQCustomerExample implements IConsumerProcessor {
15
    @SuppressWarnings("rawtypes")
16
    public Object process(MsgFMessage message) throws ConsumerException {
17
        if (message instanceof MsgFTextMessage) {
18
            System.out.println("{topic:" + message.getTopic() + ",value:" + ((MsgFTextMessage) message).getText() + "}");
19
        }
20
        return true;
21
    }
22
}
23

+ 114 - 0
ipu-rocketmq-example/src/main/java/com/ai/ipu/example/rocketmq/RocketMQProducerExample.java

@ -0,0 +1,114 @@
1
package com.ai.ipu.example.rocketmq;
2
3
import com.ai.aif.msgframe.MfProducerClient;
4
import com.ai.aif.msgframe.common.CompletionListener;
5
import com.ai.aif.msgframe.common.message.MsgFMessage;
6
import com.ai.aif.msgframe.common.message.MsgFTextMessage;
7
import org.apache.log4j.Logger;
8
import org.junit.Test;
9
10
import java.util.Date;
11
12
/**
13
 * @author liutong3
14
 * @team IPU
15
 * @date 2019/6/10 14:16
16
 * @desc RocketMQ生产者
17
 */
18
public class RocketMQProducerExample {
19
    private static final Logger LOG = Logger.getLogger(RocketMQProducerExample.class);
20
    private final String TOPIC = "test";
21
    private final String MESSAGE = "test";
22
    private final String ORDERID = "1000";
23
24
    @Test
25
    public void testSendMessage() {
26
        String message = getMessage();
27
        sendMessage(TOPIC, message);
28
    }
29
30
    @Test
31
    public void testSendAsyncMessage() {
32
        String message = getMessage();
33
        sendAsyncMessage(TOPIC, message);
34
    }
35
36
    @Test
37
    public void testSendOrderMessage() {
38
        String message = getMessage();
39
        sendOrderMessage(TOPIC, message, ORDERID);
40
    }
41
42
    @Test
43
    public void testSendOneWay() {
44
        String message = getMessage();
45
        sendOneWay(TOPIC, message);
46
    }
47
48
    private String getMessage() {
49
        return MESSAGE + "_" + new Date().getTime();
50
    }
51
52
    private void sendMessage(String topic, String text) {
53
        try {
54
            MfProducerClient client = new MfProducerClient();
55
            MsgFTextMessage message = new MsgFTextMessage();
56
            message.setText(text);
57
            client.send(topic, message);
58
            LOG.debug("Sent:" + message);
59
            Thread.currentThread().sleep(1000);
60
        } catch (Exception e) {
61
            LOG.debug(e);
62
            throw new RuntimeException(e);
63
        }
64
    }
65
66
    private void sendAsyncMessage(String topic, final String text) {
67
        try {
68
            MfProducerClient client = new MfProducerClient();
69
            final MsgFTextMessage message = new MsgFTextMessage();
70
            message.setText(text);
71
            client.asyncSend(topic, message, new CompletionListener() {
72
                public void onCompletion(MsgFMessage msgFMessage) {
73
                    LOG.debug("Sent Success:" + msgFMessage.getMsgId() + "-" + text);
74
                }
75
76
                public void onException(MsgFMessage msgFMessage, Exception e) {
77
                    LOG.debug("Sent Error:" + msgFMessage.getMsgId() + "-" + text);
78
                }
79
            });
80
            Thread.currentThread().sleep(1000);
81
        } catch (Exception e) {
82
            LOG.debug(e);
83
            throw new RuntimeException(e);
84
        }
85
    }
86
87
    private void sendOrderMessage(String topic, final String text, String orderId) {
88
        try {
89
            MfProducerClient client = new MfProducerClient();
90
            final MsgFTextMessage message = new MsgFTextMessage();
91
            message.setText(text);
92
            client.sendOrderMsg(topic, message, orderId);
93
            LOG.debug("Sent:" + message);
94
            Thread.currentThread().sleep(1000);
95
        } catch (Exception e) {
96
            LOG.debug(e);
97
            throw new RuntimeException(e);
98
        }
99
    }
100
101
    private void sendOneWay(String topic, final String text) {
102
        try {
103
            MfProducerClient client = new MfProducerClient();
104
            final MsgFTextMessage message = new MsgFTextMessage();
105
            message.setText(text);
106
            client.sendOneway(topic, message);
107
            LOG.debug("Sent:" + message);
108
            Thread.currentThread().sleep(1000);
109
        } catch (Exception e) {
110
            LOG.debug(e);
111
            throw new RuntimeException(e);
112
        }
113
    }
114
}

+ 8 - 0
ipu-rocketmq-example/src/main/resources/log4j.properties

@ -0,0 +1,8 @@
1
log4j.rootLogger=error, console
2
log4j.logger.org.apache.zookeeper=error
3
log4j.logger.com.ai.ipu.example=debug
4
5
log4j.appender.console=org.apache.log4j.ConsoleAppender
6
log4j.appender.console.target=System.out
7
log4j.appender.console.layout=org.apache.log4j.PatternLayout
8
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %5p [%t] (%F:%L) - %m%n

+ 22 - 0
ipu-rocketmq-example/src/main/resources/msgframe-config.xml

@ -0,0 +1,22 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<msgframeCfg xmlns="http://www.asiainfo.com/msgframe">
3
    <centerCfg>
4
        <name>ipu-rocketmq-example</name>
5
        <destinations>
6
            <queue name="test" belong="myCenter"/>
7
        </destinations>
8
        <subscribes>
9
            <subscribe subDestination="test">
10
                <implclass>com.ai.ipu.example.rocketmq.RocketMQCustomerExample</implclass>
11
            </subscribe>
12
        </subscribes>
13
        <centers>
14
            <center name="myCenter" containClusters="rocketmq-cluster"/>
15
        </centers>
16
        <clusters>
17
            <cluster name="rocketmq-cluster" type="RocketMQ">
18
                <url>47.105.160.21:9876</url>
19
            </cluster>
20
        </clusters>
21
    </centerCfg>
22
</msgframeCfg>