什么主机为RabbitMQ发送测试设置

时间:2014-06-09 04:04:46

标签: rabbitmq

所以我无法理解如何使用rabbitmq。我有以下Send.java类我正在使用RabbitMQ发送消息:

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

public class Send {
    private final static String QUEUE_NAME = "hello";

    public static void main(String[] argv) throws java.io.IOException {
        // create a connection to the server
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("WHAT DO I PUT HERE"); // <==========================What do I put here??
        Connection connection = factory.newConnection();
        // next we create a channel, which is where most of the API
        // for getting things done resides
        Channel channel = connection.createChannel();

        // to send we must declare a queue for us to send to; then
        // we can publish a message to the queue:
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello World!";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");

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

我真的不明白我可以设置主机以使其作为Java应用程序运行。谁能解释一下?谢谢!

1 个答案:

答案 0 :(得分:0)

这真的是不言自明的......这个方法设置了用于连接的默认主机。 如果您在本地计算机上使用RabbitMQ,可以这样设置:

factory.setHost("localhost");

或者:

factory.setHost("127.0.0.1");