rabbitmq

1.download rabbit-mq server:wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.1.3/rabbitmq-server-3.1.3-1.noarch.rpm

   download erlang:wget https://download3.vmware.com/software/vfabric/erlang-R15B-02.1.el6.x86_64.rpm?HashKey=b6a74072f19af46dacf4533c6be1643d&params=%7B%22custnumber%22%3A%22ZGRAQCV0anRkcA%3D%3D%22%2C%22sourcefilesize%22%3A%2216M%22%2C%22dlgcode%22%3A%22VFEL_15B02%22%2C%22languagecode%22%3A%22en%22%2C%22source%22%3A%22DOWNLOADS%22%2C%22downloadtype%22%3A%22manual%22%2C%22eula%22%3A%22N%22%2C%22downloaduuid%22%3A%2265d95cb5-ddb4-4faf-9a71-50189cec0485%22%2C%22purchased%22%3A%22N%22%2C%22dlgtype%22%3A%22Drivers+%26+Tools%22%2C%22productversion%22%3A%2215B02%22%2C%22productfamily%22%3A%22VMware+vFabric+RabbitMQ%22%7D&AuthKey=1374669718_be6724adbbdd6775a666b3736c42a5bb


2.install

   rpm -ivh erlang

   rpm -ivh rabbit-mq-server


3.start rabbitmq:

   chkconfig rabbitmq-server on

  /sbin/service rabbitmq-server stop/start/etc


4.test:

  

package cn.ac.ict.rabbitmq;

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

/**

 * User: qibaoyuan
 * Date: 13-7-24
 * Time: 下午8:40

 */
public class Recv {
    private final static String QUEUE_NAME = "hello";

    public static void main(String[] argv)
            throws java.io.IOException,
            java.lang.InterruptedException {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("10.63.0.183");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

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


        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(QUEUE_NAME, true, consumer);

        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" [x] Received '" + message + "'");
        }
    }
}
package cn.ac.ict.rabbitmq;

import backtype.storm.utils.Utils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 
 * User: qibaoyuan
 * Date: 13-7-24
 * Time: 下午8:38
 */
public class Send {

    private final static String QUEUE_NAME = "hello";

    public static void main(String[] argv)
            throws java.io.IOException {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("10.63.0.183");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();


        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        for (int i = 0; i < 100; i++) {
            String message = "Hello World!" + System.currentTimeMillis();
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");
            Utils.sleep(20);
        }

        channel.close();
        connection.close();

    }
}


發佈了409 篇原創文章 · 獲贊 21 · 訪問量 85萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章