rabbitmq生產者代碼,以及過程參數含義:


首先pom依賴:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloudparent</artifactId>
        <groupId>com.cxy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>rabbitMqConsumer</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.4.3</version>
        </dependency>
    </dependencies>

</project>

生產者代碼:

package com.cxy.producer;

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

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/***
 * @ClassName: Producer1
 * @Description:
 * @Auther: cxy
 * @Date: 2019/3/24:11:38
 * @version : V1.0
 */
public class Producer1 {

    private  static  final  String Queue="helloworld";

    public static void main(String[] args) {
        ConnectionFactory connectionFactory= new ConnectionFactory();
        //設置連接地址
        connectionFactory.setHost("192.168.230.134");
        //設置端口
        connectionFactory.setPort(5672);
        //設置密碼用戶名
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        //設置虛擬機,每個虛擬機相當於一個小的mq
        connectionFactory.setVirtualHost("/");
        Connection connection =null;
        try {
            //建立連接
            connection = connectionFactory.newConnection();
            //建立通道,生產着和消費者都是在通道中完成
            Channel channel = connection.createChannel();
            /*
            queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
                                 Map<String, Object> arguments)
             參數1,聲明隊列
             參數2 是否持久化
             參數3 是否排他,是否獨戰連接,隊列只允許該鏈接中訪問,如果連接關閉,隊列也就刪除了
             參數4:是否自動刪除,如果將此參數設置true,那么就變成零時隊列
             參數5 :擴展參數,例如存活時間
           */
            channel.queueDeclare(Queue,true,false,false,null);
            //所有的隊列需要制定默認的交換機
            /* basicPublish(String exchange, String routingKey, boolean mandatory, boolean immediate, BasicProperties props, byte[] body)
            throws IOException;
            參數1: 交換機,如果使用默認交換機,那么就為空,不可以設置為null
            參數二:路由key.交換機根據key來將消息發送到制定搞得隊列,如果私用默認交互機,就應該設置為隊列名稱

            */
            String message="hello maxchen";
            channel.basicPublish(null,Queue,null,message.getBytes());
            System.out.println("send xiaoxi");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }
}

運行為報錯L:

Exception in thread "main" java.lang.IllegalStateException: Invalid configuration: 'exchange' must be non-null.
    at com.rabbitmq.client.impl.AMQImpl$Basic$Publish.<init>(AMQImpl.java:3195)
    at com.rabbitmq.client.AMQP$Basic$Publish$Builder.build(AMQP.java:1219)
    at com.rabbitmq.client.impl.ChannelN.basicPublish(ChannelN.java:702)
    at com.rabbitmq.client.impl.ChannelN.basicPublish(ChannelN.java:679)
    at com.rabbitmq.client.impl.ChannelN.basicPublish(ChannelN.java:669)
    at com.rabbitmq.client.impl.recovery.AutorecoveringChannel.basicPublish(AutorecoveringChannel.java:192)
    at com.cxy.producer.Producer1.main(Producer1.java:56)

處理方法:

再看管控台:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM