AMQP--rabbitmq--1

3740 단어 rabbitmq
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:

  com.rabbitmq
  amqp-client
  2.8.4

 
링크 다운로드:
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

좋은 웹페이지 즐겨찾기