SpringBoot整合Rabbitmq之===RabbitMQ的HelloWorld模型(无废话版)--------------------------------------------(二)

 

代码地址:https://github.com/heidaodageshiwo/ZqRabbitMq

 

 

 

1.添加虚拟机(/ems) 与用户  用户名:ems   密码:123   

 

 

 

 

pom.xml

 <dependency>
      <groupId>com.rabbitmq</groupId>
      <artifactId>amqp-client</artifactId>
      <version>5.7.2</version>
    </dependency>

 

工具类:

package org.example;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * java类简单作用描述
 *
 * @ProjectName: rabbitmq
 * @Package: org.example
 * @ClassName: RabbitMQUtils
 * @Description: java类作用描述
 * @Author: zhangq
 * @CreateDate: 2020-12-10 17:12
 * @UpdateUser: zhangq
 * @UpdateDate: 2020-12-10 17:12
 * @UpdateRemark: The modified content
 * @Version: 1.0 *
 */
public class RabbitMQUtils {

  private static ConnectionFactory connectionFactory;

  static {
    connectionFactory = new ConnectionFactory();
    connectionFactory.setHost("192.168.56.200");
    connectionFactory.setPort(5672);
    connectionFactory.setVirtualHost("/ems");
    connectionFactory.setUsername("ems");
    connectionFactory.setPassword("123");
  }

  public static Connection getConnection() {
    try {
      return connectionFactory.newConnection();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (TimeoutException e) {
      e.printStackTrace();
    }
    return null;
  }
  //关闭连接
  public static void closeConnectionFactory(Channel channel,Connection connection){
    if(channel!=null){
      try {
        channel.close();
      } catch (IOException e) {
        e.printStackTrace();
      } catch (TimeoutException e) {
        e.printStackTrace();
      }
    }
    if(connection!=null){
      try {
        connection.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

测试发送消息:

package org.example.Helloword;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
import org.example.RabbitMQUtils;
import org.junit.Test;

/**
 * java类简单作用描述
 *
 * @ProjectName: rabbitmq
 * @Package: org.example.Helloword
 * @ClassName: Provider
 * @Description: java类作用描述
 * @Author: zhangq
 * @CreateDate: 2020-12-10 17:21
 * @UpdateUser: zhangq
 * @UpdateDate: 2020-12-10 17:21
 * @UpdateRemark: The modified content
 * @Version: 1.0 *
 */
public class Provider {

  @Test
  public void sendmessage() throws IOException {
    //获取连接对象
    Connection connection= RabbitMQUtils.getConnection();
    //获取连接中通道
    Channel channel = connection.createChannel();
    //通道绑定对应消息队列
    //参数1 队列名称如果队列不存在则自动创建
    //参数2 用来定义队列特性是否要持久化 true持久化
    //参数3 是否是独占队列 true 独占
    //参数4 消费者消费完消息是否自动删除队列 true自动删除
    //参数5 额外参数
    channel.queueDeclare("hello",false,false,false,null);
    //发布消息
    //参数1 交换机名称 参数2队列名称 参数3参数 参数4 消息的具体内容
    channel.basicPublish("","hello",null,"hello rabbitmq".getBytes());

    RabbitMQUtils.closeConnectionFactory(channel, connection);


  }


  public static void main(String[] args) throws IOException {
    Connection connection = RabbitMQUtils.getConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare("hello",false,false,false,null);
    channel.basicConsume("hello",true, new DefaultConsumer(channel){
      @Override
      public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
          byte[] body) throws IOException {
//        super.handleDelivery(consumerTag, envelope, properties, body);
        System.out.println("=========="+new String(body));
      }
    });




  }


}

运行通过:

 

 

运行:消费者

 

 

消费完消息没有了:

 

 

channel.queueDeclare("hello",false,false,false,null);
// channel.queueDeclare("hello",true,false,false,null);
//发布消息
//参数1 交换机名称 参数2队列名称 参数3参数 参数4 消息的具体内容
channel.basicPublish("","hello",null,"张强测试".getBytes());
//   channel.basicPublish("","hello", MessageProperties.PERSISTENT_TEXT_PLAIN,"张强测试".getBytes());

 

MessageProperties.PERSISTENT_TEXT_PLAIN :持久化消息

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RabbitMQ-c是一个C语言的客户端库,用于与RabbitMQ通信。以下是RabbitMQ-c的使用教程: 1. 下载和安装RabbitMQ-c库 可以从RabbitMQ-c的官方网站下载RabbitMQ-c库,下载完后解压缩即可。接着,打开终端窗口,进入解压缩后的目录,输入如下命令进行安装: ``` $ mkdir build && cd build $ cmake .. && make && sudo make install ``` 2. 创建连接并声明队列 首先,我们需要创建一个连接对象,并声明需要使用的队列。这可以通过以下代码实现: ``` amqp_connection_state_t conn; conn = amqp_new_connection(); amqp_socket_t *socket = amqp_tcp_socket_new(conn); amqp_socket_open(socket, "localhost", 5672); amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"); amqp_channel_open(conn, 1); amqp_queue_declare(conn, 1, amqp_cstring_bytes("hello"), 0, 0, 0, 1, amqp_empty_table); ``` 3. 发送和接收消息 发送和接收消息需要使用basic_publish和basic_consume方法。以下是发送和接收消息的示例代码: ``` // 发送消息 char message[] = "Hello World!"; amqp_bytes_t message_bytes = amqp_cstring_bytes(message); amqp_basic_publish(conn, 1, amqp_empty_bytes, amqp_cstring_bytes("hello"), 0, 0, NULL, message_bytes); // 接收消息 amqp_basic_consume(conn, 1, amqp_cstring_bytes("hello"), amqp_empty_bytes, 0, 1, 0, amqp_empty_table); amqp_frame_t frame; int result = amqp_simple_wait_frame(conn, &frame); if (result < 0) { return -1; } if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD) { return -1; } amqp_message_t message; result = amqp_read_message(conn, frame.channel, &message, NULL); if (result < 0) { return -1; } printf("Received message: %.*s\n", (int)message.body.len, (char*)message.body.bytes); amqp_destroy_message(&message); ``` 4. 关闭连接 在使用完RabbitMQ-c之后,需要关闭连接。可以通过以下代码实现: ``` amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS); amqp_connection_close(conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(conn); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值