
package org.apache.rocketmq.client.impl;
/**
* 消息发送的三种方式
*/
public enum CommunicationMode {
// 同步发送
SYNC,
// 异步发送
ASYNC,
// 单向发送
ONEWAY,
}/**
* 同步模式下内部尝试发送消息的最大次数
*/
private int retryTimesWhenSendFailed = 2
/**
* 异步模式下内部尝试发送消息的最大次数
*/
private int retryTimesWhenSendAsyncFailed = 2;/**
* 默认不启用Broker故障延迟机制。
*/
private boolean sendLatencyFaultEnable = false;brokerAddrTable 中查找当前brokerName是否存在在本地的缓存中/* Broker Name */ /* brokerId */ /* address */
private final ConcurrentMap<String, HashMap<Long, String>> brokerAddrTable;tryToFindTopicPublishInfo 函数尝试查找主题发布信息topicPublishInfoTable 缓存中根据topic名称查找是否存在TopicPublishInfo 为value到 topicPublishInfoTable ,然后更新到 NameServer 。MessageQueue 不为空 直接返回路由信息/* topic */
private final ConcurrentMap<String, TopicPublishInfo> topicPublishInfoTableisAvailable如果是不可用的,从失败的brokeName列表中通过 pickOneAtLeast 函数选择一个可用的broker。拿到brokerName之后,再根据brokerName反查这个队列的写队列数selectOneMessageQueue 函数选出一个消息队列/**
* 失败的broker列表
*/
private final LatencyFaultTolerance < String > latencyFaultTolerance;selectOneMessageQueue() 函数对当前访问的次数取绝对值,然后与消息队列的大小取模得到一个下标,然后从 messageQueueList 中根据下标取出 MessageQueue 如果上一次选择的执行发送消息失败的broker名称不为空,会遍历消息队列,对当前访问的次数取绝对值,然后与消息队列的大小取模得到一个下标后,拿着下标获取对应的 brokerName 并且判断当前的 brokerName 是否与上一次发送消息失败的 brokerName 相等,MessageQueue 返出去。MessageQueue 信息/**
* 该主题队列的消息队列
*/
private List < MessageQueue > messageQueueList = new ArrayList < MessageQueue > ();
[
{
"brokerName": "broker-a",
"queueId": 0
},
{
"brokerName": "broker-a",
"queueId": 1
}, {
"brokerName": "broker-b",
"queueId": 0
}, {
"brokerName": "broker-b",
"queueId": 1
}, {
"brokerName": "broker-c",
"queueId": 0
}, {
"brokerName": "broker-c",
"queueId": 1
}
]开启故障延迟机制中的可用依据是:检查时间是否到达了下次可使用的时间点如果没有该机制,如果broker宕机,由于路由算法中的消息队列是按broker排序的,顺序选择,如果上一次根据路由算法选择的是宕机的broker的第一个队列,那么随后的下次选择的是宕机broker的第二个队列,消息发送很有可能会失败,再次引发重试,带来不必要的性能损耗。selectOneMessageQueue()也可以看成是兜底策略-轮询算法
makeSureStateOK 函数检查服务状态是否正常checkMessage 函数校验 Message 与 DefaultMQProducer 是否符合发送的规则findBrokerAddressInPublish 函数去nameserver拉取brokerVIPChannel 函数校验是否使用了vip管道,如果使用了管道在原来的基础上把 端口-2 Message 的实例信息Message 的 body 信息进行压缩SendMessageContext,构造请求头Topid 类型是否属于重试类型消息(这里可以看看下列注释)CommunicationMode 枚举类型判断当前是什么发送方式processSendResponse 函数处理同步返回的参数,如果参数为0,说明发送成功。最后封装 SendResult 返回第四步中,如果在nameserver拉取不到,说明服务宕机了。第五步中,vip的管道配置从配置文件中的com.rocketmq.sendMessageWithVIPChannel得知第六步中,批量信息不支持压缩第十步中,如果是重试消息,通过获取自定义重试次数,在请求头区分特别处理第十一步中,因为这里介绍的是同步发送,就只写同步发送流程了,异步,单向会在下面段落体现出来第十二步中,通过配置文件中org.apache.rocketmq.client.sendSmartMsg得知字段是否简化压缩
SendCallback)ExecutorService 新增一个异步任务进行发送(可看下列注释,可看源码区)makeSureStateOK 函数检查服务状态是否正常checkMessage 函数校验 Message 与 DefaultMQProducer 是否符合发送的规则findBrokerAddressInPublish 函数去nameserver拉取brokerVIPChannel 函数校验是否使用了vip管道,如果使用了管道在原来的基础上把 端口-2 Message 的实例信息Message 的 body 信息进行压缩SendMessageContext,构造请求头Topid 类型是否属于重试类型消息(这里可以看看下列注释)CommunicationMode 枚举类型判断当前是什么发送方式processSendResponse 函数处理并且利用委托 remotingClient.invokeAsync 等待返回的SendResult 结构体updateFaultItem 函数记录当前 不可以时间/可用时间 时间第一步中 借助 java.util.concurrent.ExecutorService ,实现一个线程池达到可以让任务在后台执行。第十五步中 RemotingClient的invokeAsync函数


从 invokeOnway进入后writeAndFlush 创建通道时,通过 ReentrantLock 对nameSeverChannel加锁,超时时长为3秒
/**
* 内核同步发送
* @param msg
* @param mq
* @return
* @throws MQClientException
* @throws RemotingException
* @throws MQBrokerException
* @throws InterruptedException
*/
public SendResult send(Message msg, MessageQueue mq) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
return send(msg, mq, this.defaultMQProducer.getSendMsgTimeout());
}
/**
* 内核同步发送下的 send子函数
* @param msg
* @param mq
* @param timeout
* @return
* @throws MQClientException
* @throws RemotingException
* @throws MQBrokerException
* @throws InterruptedException
*/
public SendResult send(Message msg, MessageQueue mq, long timeout) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
long beginStartTime = System.currentTimeMillis();
this.makeSureStateOK();
Validators.checkMessage(msg, this.defaultMQProducer);
if (!msg.getTopic().equals(mq.getTopic())) {
// 消息的主题不等于mq的主题
throw new MQClientException("message's topic not equal mq's topic", null);
}
long costTime = System.currentTimeMillis() - beginStartTime;
if (timeout < costTime) {
throw new RemotingTooMuchRequestException("call timeout");
}
return this.sendKernelImpl(msg, mq, CommunicationMode.SYNC, null, null, timeout);
}/**
* 内核异步
*
* @param msg
* @param mq
* @param sendCallback
* @throws MQClientException
* @throws RemotingException
* @throws InterruptedException
*/
public void send(Message msg, MessageQueue mq, SendCallback sendCallback) throws MQClientException, RemotingException, InterruptedException {
send(msg, mq, sendCallback, this.defaultMQProducer.getSendMsgTimeout());
}
/**
* 内核异步发送下的 send 子函数
* @param msg
* @param mq
* @param sendCallback
* @param timeout the <code>sendCallback</code> will be invoked at most time
* @throws MQClientException
* @throws RemotingException
* @throws InterruptedException
*/
@Deprecated
public void send(final Message msg, final MessageQueue mq, final SendCallback sendCallback, final long timeout) throws MQClientException, RemotingException, InterruptedException {
final long beginStartTime = System.currentTimeMillis();
ExecutorService executor = this.getAsyncSenderExecutor();
try {
executor.submit(new Runnable() {
@Override
public void run() {
try {
makeSureStateOK();
Validators.checkMessage(msg, defaultMQProducer);
if (!msg.getTopic().equals(mq.getTopic())) {
throw new MQClientException("message's topic not equal mq's topic", null);
}
long costTime = System.currentTimeMillis() - beginStartTime;
if (timeout > costTime) {
try {
sendKernelImpl(msg, mq, CommunicationMode.ASYNC, sendCallback, null, timeout - costTime);
} catch (MQBrokerException e) {
throw new MQClientException("unknown exception", e);
}
} else {
sendCallback.onException(new RemotingTooMuchRequestException("call timeout"));
}
} catch (Exception e) {
sendCallback.onException(e);
}
}
});
} catch (RejectedExecutionException e) {
throw new MQClientException("executor rejected ", e);
}
}/**
* 内核单向发送
*/
public void sendOneway(Message msg, MessageQueue mq) throws MQClientException, RemotingException, InterruptedException {
this.makeSureStateOK();
Validators.checkMessage(msg, this.defaultMQProducer);
try {
this.sendKernelImpl(msg, mq, CommunicationMode.ONEWAY, null, null, this.defaultMQProducer.getSendMsgTimeout());
} catch (MQBrokerException e) {
throw new MQClientException("unknown exception", e);
}
}updateFaultItem 时间记录处理。writeAndFlush)有关后端方面的问题,非常欢迎大家咨询我,我们在群内一起讨论! 我们下期再见!欢迎『点赞』、『在看』、『转发』三连支持一下,下次见~我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送
我的工作要求我为某些测试自动生成电子邮件。我一直在四处寻找,但未能找到可以快速实现的合理解决方案。它需要在outlook而不是其他邮件服务器中,因为我们有一些奇怪的身份验证规则,我们需要保存草稿而不是仅仅发送邮件的选项。显然win32ole可以做到这一点,但我找不到任何相当简单的例子。 最佳答案 假设存储了Outlook凭据并且您设置为自动登录到Outlook,WIN32OLE可以很好地完成此操作:require'win32ole'outlook=WIN32OLE.new('Outlook.Application')message=
我正在使用Ruby,我正在与一个网络端点通信,该端点在发送消息本身之前需要格式化“header”。header中的第一个字段必须是消息长度,它被定义为网络字节顺序中的2二进制字节消息长度。比如我的消息长度是1024。如何将1024表示为二进制双字节? 最佳答案 Ruby(以及Perl和Python等)中字节整理的标准工具是pack和unpack。ruby的packisinArray.您的长度应该是两个字节长,并且按网络字节顺序排列,这听起来像是n格式说明符的工作:n|Integer|16-bitunsigned,network(bi
如果我在模型中设置验证消息validates:name,:presence=>{:message=>'Thenamecantbeblank.'}我如何让该消息显示在闪光警报中,这是我迄今为止尝试过的方法defcreate@message=Message.new(params[:message])if@message.valid?ContactMailer.send_mail(@message).deliverredirect_to(root_path,:notice=>"Thanksforyourmessage,Iwillbeintouchsoon")elseflash[:error]
RSpec似乎按顺序匹配方法接收的消息。我不确定如何使以下代码工作:allow(a).toreceive(:f)expect(a).toreceive(:f).with(2)a.f(1)a.f(2)a.f(3)我问的原因是a.f的一些调用是由我的代码的上层控制的,所以我不能对这些方法调用添加期望。 最佳答案 RSpecspy是测试这种情况的一种方式。要监视一个方法,用allowstub,除了方法名称之外没有任何约束,调用该方法,然后expect确切的方法调用。例如:allow(a).toreceive(:f)a.f(2)a.f(1)
s=Socket.new(Socket::AF_INET,Socket::SOCK_STREAM,0)s.connect(Socket.pack_sockaddr_in('port','hostname'))ssl=OpenSSL::SSL::SSLSocket.new(s,sslcert)ssl.connect从这里开始,如果ssl连接和底层套接字仍然是ESTABLISHED,或者它是否在默认值7200之后进入CLOSE_WAIT,我想检查一个线程几秒钟甚至更糟的是在实际上不需要.write()或.read()的情况下关闭。是用select()、IO.select()还是其他方法完成
我以为它们存储在cookie中-但不,检查cookie没有任何结果。session也不存储它们。那么,我在哪里可以找到它们?我需要这个来直接设置它们(而不是通过flashhash)。 最佳答案 它们存储在inyoursessionstore.自rails2.0以来的默认设置是cookie存储,但请检查config/initializers/session_store.rb以检查您是否使用默认设置以外的东西。 关于ruby-on-rails-闪存消息存储在哪里?,我们在StackOverf
我正在尝试在ruby脚本中连接到服务器https://www.xpiron.com/schedule。但是,当我尝试连接时:require'open-uri'doc=open('https://www.xpiron.com/schedule')我收到以下错误消息:OpenSSL::SSL::SSLError:SSL_connectreturned=1errno=0state=SSLv2/v3readserverhelloA:sslv3alertunexpectedmessagefrom/usr/local/lib/ruby/1.9.1/net/http.rb:678:in`conn
我想知道我应该如何着手这个项目。我需要每周向人们发送一次电子邮件。但是,这必须在每周的特定时间自动生成并发送。编码有多难?我需要知道是否有任何书籍可以提供帮助,或者你们中的任何人是否可以指导我。它必须使用rubyonrails进行编程。因此有一个网络服务和数据库集成。干杯 最佳答案 为什么这么复杂?您只需安排工作。您可以使用Delayed::Job例如。Delayed::Job让您可以使用run_at符号在特定时间安排作业,如下所示:Delayed::Job.enqueue(SendEmailJob.new(...),:run_