1. Create mq connection
ConnectionFactory chooses the no-parameter construction method, which will call some default parameter values in ConnectionFactory, or you can set the value yourself
ConnectionFactory factory = new ConnectionFactory();
try {
//Custom parameter value
factory.setHost("192.168.1.1");
factory.setPort(5672);
factory.setUserName("userName");
factory.setPassword("password");
ConnectionFactory factory =factory.newConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2. Create a channel (use channel to receive and send messages),
//创建chanle
Channel channel = connection.createChannel();
3. Declare a queue
/**
* Queue name
* Whether the queue is persistent or not, the queue is persistent to the database, even if the mq service hangs, the message is still
saved Whether the current queue
* is automatically deleted, executed when the last connection is disconnected
* other parameters
*/
channel.queueDeclare(queueName, true, false, false, null);
4. Queue binding switch
channel.queueBind(queueName,exchangeName,queueName);
5. The producer sends a message
String sendStr = "Producer sends message";
// switch name, routing key, message attribute fields ( such as message header information, etc. ), sender
channel.basicPublish("Switch Name", "Queue Name", null, sendStr.getBytes("UTF-8"));