不愿意做出改变 ≈ 坐吃等死!!

JAVA通过rabbitMQ发送消息

上一篇 / 下一篇  2017-06-21 16:33:07 / 个人分类:测试工具


rabbitMQ工具不知不觉有10年的历史,不得不说她在消息持久化,低耦合,高并发等方面非常优秀。具体说明可以自行谷歌

下面主要是介绍
1. 通过queue发送消息
2. 通过exchange, routingKey发送消息

上代码:

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
package com.xx;
 
/**
 * Created by chenjun on 2017/6/19.
 */
 
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
 
import java.io.IOException;
import java.util.concurrent.TimeoutException;
 
public class SendMQ {
 
    private ParseConfig config = new ParseConfig();
 
 
    // 通过队列发送消息
    public void sendByQueue(String queue, String Message)
            throws IOException {
        try {
            ConnectionFactory factory = new ConnectionFactory();
            // MQ 地址 端口默认
            factory.setHost(config.host);
            // MQ 端口 如果是默认 可以不用写
//            factory.setPort(5432);
            // 登陆MQ用户名
            factory.setUsername(config.userName);
            // 登陆MQ密码
            factory.setPassword(config.password);
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            channel.queueDeclare(queue, truefalsefalsenull);
            String message = Message;
            channel.basicPublish("", queue, null, message.getBytes());
            System.out.println("[x] Send '" + message + "'");
 
            channel.close();
            connection.close();
        catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
 
    // 通过exchange routingKey发送消息
    public void sendByExchange(String exchange, String routingKey, String type, String Message)
            throws IOException {
 
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(config.host);
            factory.setUsername(config.userName);
            factory.setPassword(config.password);
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            // type:  exchange的处理消息类型:1.Direct 2.Fanout 3.Topic
            // 用topic验证消息发送成功, 其他两种类型大家可以自行测试
            channel.exchangeDeclare(exchange, type, true);
            String message = Message;
            channel.basicPublish(exchange, routingKey, null, message.getBytes());
            System.out.println("[x] Send '" + message + "'");
 
            channel.close();
            connection.close();
        catch (TimeoutException e) {
            e.printStackTrace();
        }
 
    }
 
    public static void main(String[] args) throws IOException {
 
        SendMQ s = new SendMQ();
 
//        s.sendByQueue(s.config.queue_asset, "hello world!");
        s.sendByExchange(s.config.assetExchange, s.config.assetRouteKey,
                s.config.assetType, "hello world");
 
    }
 
}


ParseConfig 是一个配置文件读取类 ,MQ地址 用户名 密码从配置文件读取




TAG: java JAVA

Rambo_jia的个人空间 引用 删除 Rambo_jia   /   2017-06-23 21:05:33
不错
 

评分:0

我来说两句

Open Toolbar