发布于2021-06-08 11:22 阅读(654) 评论(0) 点赞(17) 收藏(3)
这篇文章主要熟悉一下rabbitmq客户端的核心类ConnectionFactory,主要会从几个方面入手,成员变量整理和核心方法整理,得出它们核心能力,做一些实践验证
field | default(默认) | 含义 |
---|---|---|
virtualHost | / | 虚拟主机概念(隔离消息能力) |
port | -1 | 端口号(默认端口号非ssl为5672,ssl为5671) |
host | localhost | 主机地址 |
requestedChannelMax | 2047 | 最大通道数,2047 因为它是服务器端的 2048 减去通道 0,每个连接用于协商和错误通信 |
requestedFrameMax | 0 | 默认是0,表示请求帧大小没有任何限制 |
requestedHeartbeat | 60 | 默认心跳检测间隔是60s |
connectionTimeout | 60000 | 默认连接超时是60s |
handshakeTimeout | 10000 | 默认握手超时是10s |
shutdownTimeout | 10000 | 默认关闭超时是10s,如果是0则无限等待 |
_clientProperties | 默认 | rabbitmq客户端自带属性,客户端能力 |
socketFactory | null | socket工厂类(jdk自带的) |
saslConfig | 简单验证和安全层配置 | 默认机制为PLAIN |
sharedExecutor | 共享执行器 | ExecutorService (jdk自带) |
threadFactory | 线程工厂类 | new DefaultThreadFactory()(jdk自带) |
shutdownExecutor | 关闭执行器 | ExecutorService (jdk自带) |
heartbeatExecutor | 心跳执行器 | ScheduledExecutorService |
socketConf | socket配置 | 默认是非Nagle算法,也就是无延迟,数据包立即发送(有人就发车,不需要坐满),这里使用jdk1.8 函数特性 |
exceptionHandler | 异常处理器 | 默认异常处理 |
credentialsProvider | 默认username=guest, password=guest | 验证用户名和密码 |
automaticRecovery | true | 是否自动恢复(由于各种原因断开了) |
topologyRecovery | true | 拓扑恢复 |
topologyRecoveryExecutor | null | 拓扑恢复执行器 |
networkRecoveryInterval | 5000 | 单位是毫秒,就是5s |
recoveryDelayHandler | null | 恢复延迟处理器 |
metricsCollector | NoOpMetricsCollector | rabbitmq消息监控收集器 |
nio | false | 是否为nio |
frameHandlerFactory | null | 帧处理器工厂类(socket处理) |
nioParams | new NioParams | nio一些默认参数 |
sslContextFactory | null | ssl上下文工厂类 |
channelRpcTimeout | 10min | 默认通道超时时间为10分钟 |
channelShouldCheckRpcResponseType | false | 是否需要检查返回rpc调用结果类型 |
errorOnWriteListener | null | 监听发生io写异常,并传播异常 |
workPoolTimeout | -1 | 工作线程池排队超时时间,默认没有超时时间 |
topologyRecoveryFilter | null | topology恢复过滤 |
connectionRecoveryTriggeringCondition | null | 条件自动恢复连接(Predicate) |
topologyRecoveryRetryHandler | null | 重试处理拓扑恢复处理器 |
trafficListener | TrafficListener.NO_OP | 命令流量监控,一般用于debug |
以上是所有成员变量,我们以时间轴进行整理,主要分一下几块(如图所示)
连接和握手相关成员变量
发送数据和处理数据成员变量
异常逻辑处理和监听成员变量
发生异常之后进行恢复和恢复策略成员变量
关闭连接成员变量
心跳检测成员变量
指标收集器(收集客户与服务端交互整个过程)
com.rabbitmq.client.ConnectionFactory#portOrDefault (获取端口号)
com.rabbitmq.client.ConnectionFactory#setCredentialsProvider (设置凭证)
com.rabbitmq.client.ConnectionFactory#setUri(java.net.URI) (解析uri)
com.rabbitmq.client.ConnectionFactory#isSSL (判断是否为ssl)
com.rabbitmq.client.ConnectionFactory#useSslProtocol() (使用ssl进行连接)
com.rabbitmq.client.ConnectionFactory#createFrameHandlerFactory (创建数据帧处理工厂类)
com.rabbitmq.client.ConnectionFactory#newConnection(创建连接对象)
com.rabbitmq.client.ConnectionFactory#load (加载连接rabbit属性)
点开看一下发现是自定义的函数,也就jdk1.8引入的
@FunctionalInterface
public interface SocketConfigurator {
/**
* Provides a hook to insert custom configuration of the sockets
* used to connect to an AMQP server before they connect.
*/
void configure(Socket socket) throws IOException;
/**
* Returns a composed configurator that performs, in sequence, this
* operation followed by the {@code after} operation.
*
* @param after the operation to perform after this operation
* @return a composed configurator that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default SocketConfigurator andThen(SocketConfigurator after) {
Objects.requireNonNull(after);
return t -> {
configure(t);
after.configure(t);
};
}
}
总结
研究一下他们是怎么用的,我们是否可以模仿学习一波
SocketConfigurator有一个配套SocketConfigurators抽象类,里面有Builder构造器设计模式
看一下使用例子,链式编程,看起来很舒服,这个是我们模仿典范实例代码
this.socketConf = SocketConfigurators.builder().defaultConfigurator().enableHostnameVerification().build();
看一下定义SocketConfigurator函数实例,就是设置一个属性不延迟通信
public static final SocketConfigurator DISABLE_NAGLE_ALGORITHM = socket -> socket.setTcpNoDelay(true);
这样做不需要定义一个方法,一行搞定,而且还可以利用函数的andThen,组合函数
写一个例子,支持支付方式(现在有现金、优惠券,微信支付)
看一下核心类,其实和SocketConfigurators基本类似
package com.jack.function.arithmetic;
/**
* 是否支持一些支付方式
* @author liangchen
* @date 2021/6/4
*/
public abstract class PayConfigs {
// 使用优惠券
public static final PayConfig USE_COUPON = pay -> pay.setUseCoupon(true);
// 使用现金
public static final PayConfig USE_CASH = pay -> pay.setCash(true);
// 使用微信
public static final PayConfig USE_WEIXIN = pay -> pay.setWeixin(true);
public static Builder builder(){
return new Builder();
}
public static class Builder {
// 定义空
private PayConfig configurator = pay -> {
};
public Builder useCoupon(){
configurator = configurator.andThen(USE_COUPON);
return this;
}
public Builder useCash(){
configurator = configurator.andThen(USE_CASH);
return this;
}
public Builder useWeixin(){
configurator = configurator.andThen(USE_WEIXIN);
return this;
}
public PayConfig build() {
return configurator;
}
}
}
使用的main方法
/**
* @author liangchen
* @date 2021/6/4
*/
public class TestFunction {
public static void main(String[] args) {
// 具体我们定义支持cash,coupon支付
PayConfig build = PayConfigs.builder().useCash().useCoupon().build();
SupportPayMethod supportPayMethod = new SupportPayMethod();
build.execute(supportPayMethod);
System.out.println(supportPayMethod);
}
}
输出结果
SupportPayMethod{useCoupon=true, cash=true, weixin=false}
详情代码参考github示例代码, 小伙伴你们尝试在工作使用这种方式。
###3.1.1、socketConf学习总结
作者:java王侯
链接:http://www.javaheidong.com/blog/article/219478/6f974e320df41f331d99/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!