rabbitmq 及一般性 http api 调用

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.5</version>
</dependency>

一般使用说明 

(41条消息) org.apache.http.client.HttpClient使用方法_weixin_30555753的博客-CSDN博客

rabbitmq需要用户名及密码验证

解决方法

(41条消息) [原创]RabbitMQ之业务场景(五):采用httpApi动态操作RabbitMQ(JAVA版)_Java技术干货的博客-CSDN博客

// 发送http请求数据
		// 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // 设置BasicAuth
        CredentialsProvider provider = new BasicCredentialsProvider();
        // Create the authentication scope
        AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
        // Create credential pair,在此处填写用户名和密码
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
        // Inject the credentials
        provider.setCredentials(scope, credentials);
        // Set the default credentials provider
        httpClientBuilder.setDefaultCredentialsProvider(provider);
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

完整代码段


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;

import java.io.IOException;

public class b {
    public static void  main(String[] args){

        // 获取队列名称
        String host = "127.0.0.1";
        String username = "guest";
        String password = "guest";
        // 根据RabbitMQ提供的队列信息, 每天定时删除动态创建的队列。
        String url = "http://"+host+":15672/api/queues";
        String result = null;
        try {
            result = Get(url, username, password);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(result);
    }

    public static String Get(String url, String username, String password) throws IOException{
        // 发送http请求数据
        // 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // 设置BasicAuth
        CredentialsProvider provider = new BasicCredentialsProvider();
        // Create the authentication scope
        AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
        // Create credential pair,在此处填写用户名和密码
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
        // Inject the credentials
        provider.setCredentials(scope, credentials);
        // Set the default credentials provider
        httpClientBuilder.setDefaultCredentialsProvider(provider);
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

        String result = "";
        HttpGet httpGet = null;
        HttpResponse httpResponse = null;
        HttpEntity entity = null;
        httpGet = new HttpGet(url);
        try {
            httpResponse = closeableHttpClient.execute(httpGet);
            entity = httpResponse.getEntity();
            if( entity != null ){
                result = EntityUtils.toString(entity);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭连接
        closeableHttpClient.close();

        return result;
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java调用RabbitMQ API,你可以使用RabbitMQ Java客户端库。下面是一个简单的示例代码: 首先,确保你已经在你的项目中添加了RabbitMQ Java客户端库的依赖项。你可以在Maven或Gradle中添加以下依赖: Maven: ```xml <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.11.0</version> </dependency> ``` Gradle: ```groovy implementation 'com.rabbitmq:amqp-client:5.11.0' ``` 接下来,你可以使用以下代码创建一个RabbitMQ连接,并发送消息到一个队列: ```java import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import java.io.IOException; import java.util.concurrent.TimeoutException; public class RabbitMQExample { private final static String QUEUE_NAME = "my_queue"; private final static String MESSAGE = "Hello, RabbitMQ!"; public static void main(String[] args) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); // RabbitMQ服务器的主机名或IP地址 try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) { channel.queueDeclare(QUEUE_NAME, false, false, false, null); channel.basicPublish("", QUEUE_NAME, null, MESSAGE.getBytes()); System.out.println("Sent message: " + MESSAGE); } catch (IOException | TimeoutException e) { e.printStackTrace(); } } } ``` 上面的代码中,我们创建了一个名为 "my_queue" 的队列,并发送了一个消息 "Hello, RabbitMQ!" 到该队列。 请注意,上面的代码只是一个简单的示例,你可以根据你的需求进行更详细的配置和处理。你可以查阅RabbitMQ Java客户端库的文档,了解更多关于连接、通道、队列等操作的详细信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值