【实例简介】
【实例截图】
【核心代码】
package com.rabbitmq.test;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class ProducerTest {
private final static String QUEUE_NAME = "hello";
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");//因为两个进程在同一个机器上
Connection connection = null;
Channel channel = null;
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
System.out.println(" [Producer] Sent '" message "'");
channel.close();
connection.close();
}
}