把rabbitmq服务器搭建起来之后,就可以写例子来测试rabbitmq了,先写一个发送端的,也叫producer,这个例子是官网上抄录下来的,此处用的是eclipse IDE,只需导入rabbitmq client的几个jar包即可。

package producer;
import java.io.IOException;
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 args[]) throws IOException
{
ConnectionFactory cf = new ConnectionFactory();
cf.setHost("127.0.0.1");
Connection connection = cf.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "hello world";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" send " + message);
channel.close();
connection.close();
}
}