AMQP--rabbitmq--1

1.基本安装

分为 server  + client 

 

server的安装:

 

1.添加 deb http://www.rabbitmq.com/debian/ testing main 到 /etc/apt/sources.list

 

2.apt-get update.

 

3.sudo apt-get install rabbitmq-server

这个步骤会自动启动 rabbitmq-server 服务。

 

常用命令:

 

rabbitmqctl -h 

rabbitmqctl status 

rabbitmqctl stop

rabbitmqctl start_app

 

 

客户端安装:

maven:

<dependency>

  <groupId>com.rabbitmq</groupId>

  <artifactId>amqp-client</artifactId>

  <version>2.8.4</version>

</dependency>

 

或是下载链接:

wget http://www.rabbitmq.com/releases/rabbitmq-java-client/v2.8.4/rabbitmq-java-client-bin-2.8.4.tar.gz

 

客户端编码---发送者:

 

package com.jieting.mq.rabbit.send;

import java.io.IOException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class MessageSend {

    private static final String QUENE_NAME = "hello";

    public static void main(String[] args) throws IOException {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");

        Connection newConnection = connectionFactory.newConnection();
        Channel createChannel = newConnection.createChannel();

        createChannel.queueDeclare(QUENE_NAME, true, false, false, null);
        String message = "hello rabbitmq world!";
        createChannel.basicPublish("", QUENE_NAME, null, message.getBytes());

        System.out.println(" [x] Sent '" + message + "'");

        createChannel.close();
        newConnection.close();

    }
}

 

消费者代码:

 

package com.jieting.mq.rabbit.receive;

import java.io.IOException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.ShutdownSignalException;

public class MessageReceive {

    private static final String QUENE_NAME = "hello";

    public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException,
                                          InterruptedException {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        Connection newConnection = connectionFactory.newConnection();
        Channel createChannel = newConnection.createChannel();

        createChannel.queueDeclare(QUENE_NAME, true, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        QueueingConsumer queueingConsumer = new QueueingConsumer(createChannel);
        createChannel.basicConsume(QUENE_NAME, true, queueingConsumer);
        while (true) {
            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" [x] Received '" + message + "'");
        }
    }
}

 

以上资料都可从 一下地址找到:

http://www.rabbitmq.com/java-client.html

http://www.rabbitmq.com/getstarted.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值