Browse Source

msgframe的rabbitmq测试代码

liutong3 6 years ago
parent
commit
a6eb637e07

+ 15 - 0
ipu-rabbitmq-example/src/main/java/com/ai/ipu/example/rabbitmq/RabbitMQCustomer.java

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

+ 22 - 0
ipu-rabbitmq-example/src/main/java/com/ai/ipu/example/rabbitmq/RabbitMQCustomerExample.java

@ -0,0 +1,22 @@
1
package com.ai.ipu.example.rabbitmq;
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 16:20
12
 * @desc rabbitmq消费者
13
 */
14
public class RabbitMQCustomerExample 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
}

+ 92 - 0
ipu-rabbitmq-example/src/main/java/com/ai/ipu/example/rabbitmq/RabbitMQProducerExample.java

@ -0,0 +1,92 @@
1
package com.ai.ipu.example.rabbitmq;
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
/**
14
 * @author liutong3
15
 * @team IPU
16
 * @date 2019/6/6 16:07
17
 * @desc rabbitmq生产者
18
 */
19
public class RabbitMQProducerExample {
20
    private static final Logger LOG = Logger.getLogger(RabbitMQProducerExample.class);
21
    private final String TOPIC = "test";
22
    private final String MESSAGE = "test";
23
    private final String ORDERID = "1000";
24
25
    @Test
26
    public void testSendMessage() {
27
        String message = getMessage();
28
        sendMessage(TOPIC, message);
29
    }
30
31
    @Test
32
    public void testSendAsyncMessage() {
33
        String message = getMessage();
34
        sendAsyncMessage(TOPIC, message);
35
    }
36
37
    @Test
38
    public void testSendOrderMessage() {
39
        String message = getMessage();
40
        sendOrderMessage(TOPIC, message, ORDERID);
41
    }
42
43
    private String getMessage() {
44
        return MESSAGE + "_" + new Date().getTime();
45
    }
46
47
    private void sendMessage(String topic, String text) {
48
        try {
49
            MfProducerClient client = new MfProducerClient();
50
            MsgFTextMessage message = new MsgFTextMessage();
51
            message.setText(text);
52
            client.send(topic, message);
53
            LOG.debug("Sent:" + message);
54
            Thread.currentThread().sleep(1000);
55
        } catch (Exception e) {
56
            throw new RuntimeException(e);
57
        }
58
    }
59
60
    private void sendAsyncMessage(String topic, final String text) {
61
        try {
62
            MfProducerClient client = new MfProducerClient();
63
            final MsgFTextMessage message = new MsgFTextMessage();
64
            message.setText(text);
65
            client.asyncSend(topic, message, new CompletionListener() {
66
                public void onCompletion(MsgFMessage msgFMessage) {
67
                    LOG.debug("Sent Success:" + msgFMessage.getMsgId() + "-" + text);
68
                }
69
70
                public void onException(MsgFMessage msgFMessage, Exception e) {
71
                    LOG.debug("Sent Error:" + msgFMessage.getMsgId() + "-" + text);
72
                }
73
            });
74
            Thread.currentThread().sleep(1000);
75
        } catch (Exception e) {
76
            throw new RuntimeException(e);
77
        }
78
    }
79
80
    private void sendOrderMessage(String topic, final String text, String orderId) {
81
        try {
82
            MfProducerClient client = new MfProducerClient();
83
            final MsgFTextMessage message = new MsgFTextMessage();
84
            message.setText(text);
85
            client.sendOrderMsg(topic, message, orderId);
86
            LOG.debug("Sent:" + message);
87
            Thread.currentThread().sleep(1000);
88
        } catch (Exception e) {
89
            throw new RuntimeException(e);
90
        }
91
    }
92
}

+ 8 - 0
ipu-rabbitmq-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-rabbitmq-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-rabbitmq-example</name>
5
        <destinations>
6
            <queue name="test" belong="myCenter"/>
7
        </destinations>
8
        <subscribes>
9
            <subscribe subDestination="test">
10
                <implclass>com.ai.ipu.example.rabbitmq.RabbitMQCustomerExample</implclass>
11
            </subscribe>
12
        </subscribes>
13
        <centers>
14
            <center name="myCenter" containClusters="rabbitmq-cluster"/>
15
        </centers>
16
        <clusters>
17
            <cluster name="rabbitmq-cluster" type="RabbitMQ">
18
                <url user="admin" password="admin">47.105.160.21</url>
19
            </cluster>
20
        </clusters>
21
    </centerCfg>
22
</msgframeCfg>