java代码实现rabbitMQ请求

本文介绍如何使用Java客户端连接RabbitMQ并发送消息。包括客户端下载、配置及核心代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、下载rabbitMQ的客户端,下载地址 http://www.rabbitmq.com/download.html

2、解压,并将后缀为.jar(可以不用包含junit.jar)的拷贝出来放入java的项目。

import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Send {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.11.188"); // mq ip
        factory.setPort(5672);// 端口号
        factory.setUsername("abc"); // rabbitMQ登录用户名
        factory.setPassword("abc123"); // rabbitMQ登录密码
        factory.setVirtualHost("hdware.adjust.host");
        Connection connection = null;
        connection = factory.newConnection();
        Channel channel = connection.createChannel();
        String sendStr = "需要发送的文本";
        // 发送
        channel.basicPublish("", "gz.hdwareinfo.queue", null,
                sendStr.getBytes("utf-8"));
        System.out.println("已发送" + sendStr);
        channel.close();
        connection.close();
    }
}

 

转载于:https://www.cnblogs.com/milanmi/p/4651783.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值