import com.rabbitmq.client.*;
import java.io.IOException;
public class TestResiver {
private static final StringQUEUE_NAME=”hello”;
public static void main(String[] args)throws IOException, InterruptedException {
//创建来拿介工场
ConnectionFactory factory =new ConnectionFactory();
factory.setHost(“192.168.155.33”);
//端口
factory.setPort(5672);
//设置账号信息,用户名、密码、vhost
factory.setVirtualHost(“/ghy”);
factory.setUsername(“ghy”);
factory.setPassword(“ghy”);
//得到connection 对象
Connection connection =factory.newConnection();
Channel channel =connection.createChannel();
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
//接收消息
//声明消费者
QueueingConsumer queueingConsumer =new QueueingConsumer(channel);
//指定消费者
channel.basicConsume(QUEUE_NAME,true,queueingConsumer);
//开始接收消息
while (true){
QueueingConsumer.Delivery delivery =queueingConsumer.nextDelivery();
byte []s= delivery.getBody();
System.out.println(“消费者接收到了”+s);
}
}
}